freepeople性欧美熟妇, 色戒完整版无删减158分钟hd, 无码精品国产vα在线观看DVD, 丰满少妇伦精品无码专区在线观看,艾栗栗与纹身男宾馆3p50分钟,国产AV片在线观看,黑人与美女高潮,18岁女RAPPERDISSSUBS,国产手机在机看影片

正文內(nèi)容

java程序設(shè)計(jì)模式程序設(shè)計(jì)-文庫(kù)吧資料

2024-09-04 17:53本頁(yè)面
  

【正文】 74. //換一套 Android 的皮膚 75. (new AndroidSkinFactory())。 67. } 68. } 69. public class SkinDemo{ 70. public static void main(String[] args){ 71. //顯示一套 IOS 皮膚 72. Skin skin = new Skin(new IOSSkinFactory())。 58. } 59. } 60. //Android 皮膚工廠 61. class AndroidSkinFactory implements SkinFactory{ 62. public Style getStyle(){ 63. return new AndroidStyle()。 50. } 51. //IOS 皮膚工廠 52. class IOSSkinFactory implements SkinFactory{ 53. public Style getStyle(){ 54. return new IOSStyle()。 44. } 45. } 46. //抽象皮膚工廠 47. interface SkinFactory{ 48. public Style getStyle()。 33. } 34. //IOS color 35. class IOSColor implements Color{ 36. public void showColor(){ 37. (“This is IOS color”)。 22. } 23. } 24. //Android style 25. class AndroidStyle implements Style{ 26. public void showStyle(){ 27. (“This is Android style”)。 12. } 13. } 14. //軟件 Style 15. interface Style(){ 16. public void showStyle()。 4. public Skin(SkinFactory factory){ 5. setSkinFactory(factory)。 34. } 35. } 圖形工廠負(fù)責(zé)具體圖形的對(duì)象實(shí)例化工作,圖形使用者使用時(shí)不需要關(guān)心圖形對(duì)象的具體產(chǎn)生過(guò)程。 30. } 31. public static void main(String[] args){ 32. draw((“Circle”))。 15. } 16. } 17. //圖形工廠 18. class ShapeFactory{ 19. public static Shape createShape(String name) throws InstantiationException, 20. IllegalAccessException, 21. ClassNotFoundException 22. { 23. //使用 java 的反射機(jī)制來(lái)產(chǎn)生對(duì)象實(shí)例 24. return (Shape)(name).newInstance()。 4. } 5. //圓形 6. class Circle implements Shape{ 7. public void draw(){ 8. (“Circle is drawing”)。 (1).簡(jiǎn)單工廠模式: 又叫靜態(tài)工廠模式,簡(jiǎn)單工廠只包括一個(gè)抽象產(chǎn)品類(lèi)(該類(lèi)可以是接口,也可以是具體的類(lèi)),所有需要的產(chǎn)品類(lèi)都是該抽象產(chǎn)品類(lèi)的子類(lèi)。 (2).Strategy 策略模式重點(diǎn)在于根據(jù)需求直接采用設(shè)定的策略,即根據(jù)場(chǎng)景選擇合適的處理算法,而不需要改變狀態(tài)。 40. (new StrategyTwo(testText1))。 38. String testText2 = This is a test text!!rn Oh! Line Returnrn。 30. } 31. } 32. public class TextCharChange { 33. public static void replace(TextStrategy strategy) { 34. ()。 28. String result = (“n, rn))。 19. } 20. } 21. //替換算法 2:將文本中 n替換為 rn 22. class StrategyTwo extends TextStrategy { 23. public StrategyTwo(String text) { 24. super(text)。 17. String result = (rn, n))。 9. } 10. //替換算法 1:將文本中 rn替換為 n 11. class StrategyOne extends TextStrategy { 12. public StrategyOne(String text) { 13. super(text)。 4. 5. public TextStrategy(String text) { 6. = text。 (4).可以為容器或其子容器實(shí)現(xiàn)不同的迭代方法或多個(gè)迭代方法。 (2).隱藏容器的實(shí)現(xiàn)細(xì)節(jié)。 Iterator 模式的優(yōu)點(diǎn): (1).實(shí)現(xiàn)功能分離,簡(jiǎn)化容器接口。 25. } 26. } 27. //不支持 remove 操作 28. public void remove(){ 29. throw new UnsupportedOperationException( 30. Alternating MyIterator does not support remove())。 19. } 20. public boolean hasNext() { 21. if (position = ()) { 22. return false。 17. position++。 12. public Itr(ArrayList list) { 13. = list。 7. } 8. //作為內(nèi)部類(lèi)的具體迭代器角色 9. private class Itr implements Iterator { 10. ArrayList myList。 11. } 12. //java 集合框架的根接口,該接口繼承了容器迭代化接口,因此 java 中的集合都可以被迭代 13. public interface CollectionE extends IterableE 自定義迭代器,以 ArrayList 為自定義迭代容器的底層數(shù)據(jù)結(jié)構(gòu),實(shí)現(xiàn)自定義迭代器的代碼如下: [java] view plaincopy 1. public class MyIterator implements Iterable { 2. //存放數(shù)據(jù)的集合 3. private ArrayList list。 5. void remove()。 Java 集合框架中迭代設(shè)計(jì)模式的應(yīng)用: [java] view plaincopy 1. //迭代器 ,該接口提供了迭代遍歷的通用方法 2. public interface Iterator { 3. boolean hasNext()。 3) 容器角色( Container):容器角色負(fù)責(zé)提供創(chuàng)建具體迭代器角色的接口。 迭代器模式由以下角色組成: 1) 迭代器角色( Iterator):迭代器角色負(fù)責(zé)定義訪問(wèn)和遍歷元素的接口。該方法雖然是省事,卻又將容器的內(nèi)部細(xì)節(jié)暴露無(wú)遺。 程序?qū)θ萜鲗?duì)象的訪問(wèn)必然涉及到遍歷算法,不同的容器遍歷算法是不同的,List, Stack 和 Set 等等常用容器遍歷元素的算法各不相同。 (2).Proxy 代理模式的目的是控制客戶(hù)端對(duì)目標(biāo)程序的訪問(wèn),而 State 狀態(tài)模式是為了根據(jù)條件動(dòng)態(tài)改變目標(biāo)程序。 71. (sp)。 69. (sp)。 65. ()。 62. private void run(ServiceProvider sp){ 63. ()。 56. //…… 57. ()。 50. //…… 51. ()。 44. //…… 45. ()。 36. } 37. //狀態(tài)更改 38. public void changeState(State newState){ 39. state = newState。 29. } 30. } 31. //服務(wù)提供者 32. class ServiceProvider{ 33. private State state。 23. } 24. public void operation2(){ 25. (“()”)。 14. } 15. public void operation3(){ 16. (“()”)。 6. } 7. //狀態(tài)實(shí)現(xiàn)類(lèi) 1 8. class implementation1 implements State{ 9. public void operation1(){ 10. (“()”)。 4. void operation2()。 28. } 29. } State 狀態(tài)設(shè)計(jì)模式中,狀態(tài)自動(dòng)切換并傳播,不需要再改動(dòng)標(biāo)識(shí),代碼顯得非常優(yōu)雅。 26. ()。 22. } 23. public static void main(String[] args){ 24. Creature creature = new Creature()。 17. public void greet(){ 18. ()。 9. } 10. } 11. private class Prince implements State{ 12. public String response(){ 13. return “Darling!”。 19. } 20. } 上面例子代碼中 greet()方法在執(zhí)行具體操作之前必須要判斷一下標(biāo)識(shí),代碼顯得笨拙繁瑣,使用簡(jiǎn)單 State狀態(tài)模式改寫(xiě)上面代碼如下: [java] view plaincopy 1. public class Creature{ 2. //狀態(tài)接口 3. private interface State{ 4. String response()。 17. ()。 13. } 14. public static void main(String[] args){ 15. Creature creat
點(diǎn)擊復(fù)制文檔內(nèi)容
環(huán)評(píng)公示相關(guān)推薦
文庫(kù)吧 www.dybbs8.com
備案圖鄂ICP備17016276號(hào)-1