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

正文內(nèi)容

java程序設計模式程序設計(編輯修改稿)

2024-10-02 17:53 本頁面
 

【文章內(nèi)容簡介】 )。 17. position++。 18. return obj。 19. } 20. public boolean hasNext() { 21. if (position = ()) { 22. return false。 23. } else { 24. return true。 25. } 26. } 27. //不支持 remove 操作 28. public void remove(){ 29. throw new UnsupportedOperationException( 30. Alternating MyIterator does not support remove())。 31. } 32. } 33. } 使用時, MyIterator 對象直接調(diào)用 iterator()方法就可以將自定義容器對象轉換為迭代器對象。 Iterator 模式的優(yōu)點: (1).實現(xiàn)功能分離,簡化容器接口。讓容器只實現(xiàn)本身的基本功能,把迭代功能委讓給外部類實現(xiàn),符合類的設計原則。 (2).隱藏容器的實現(xiàn)細節(jié)。 (3).為容器或其子容器提供了一個統(tǒng)一接口,一方面方便調(diào)用;另一方面使得調(diào) 用者不必關注迭代器的實現(xiàn)細節(jié)。 (4).可以為容器或其子容器實現(xiàn)不同的迭代方法或多個迭代方法。 Strategy 設計模式 Strategy 策略設計模式主 要是定義一系列的算法,把這些算法封裝成單獨的類,在運行時動態(tài)選擇需要的算法,策略模式機制如下: 策略模式例子如下: [java] view plaincopy 1. //文本替換策略 2. abstract class TextStrategy { 3. protected String text。 4. 5. public TextStrategy(String text) { 6. = text。 7. } 8. public abstract String replace()。 9. } 10. //替換算法 1:將文本中 @r@n替換為 @n 11. class StrategyOne extends TextStrategy { 12. public StrategyOne(String text) { 13. super(text)。 14. } 15. public String replace() { 16. (“StrategyOne:”)。 17. String result = (@r@n, @n))。 18. return result。 19. } 20. } 21. //替換算法 2:將文本中 @n替換為 @r@n 22. class StrategyTwo extends TextStrategy { 23. public StrategyTwo(String text) { 24. super(text)。 25. } 26. public String replace() { 27. (“StrategyTwo:”)。 28. String result = (“@n, @r@n))。 29. return result。 30. } 31. } 32. public class TextCharChange { 33. public static void replace(TextStrategy strategy) { 34. ()。 35. } 36. public static void main(String[] args){ 37. String testText1 = This is a test text!!@n Oh! Line Return!!@n。 38. String testText2 = This is a test text!!@r@n Oh! Line Return@r@n。 39. (new StrategyOne(testText2))。 40. (new StrategyTwo(testText1))。 41. } 42. } State 狀態(tài)模式和 Strategy 策略模式非常類似,但是有如下區(qū)別: (1).State 狀態(tài)模式重點在于設定狀態(tài)變化,根據(jù)狀態(tài),返回相應的響應。 (2).Strategy 策略模式重點在于根據(jù)需求直接采用設定的策略,即根據(jù)場景選擇合適的處理算法,而不需要改變狀態(tài)。 Factory 設計模式 Factory 工廠設計模式為創(chuàng)建對象提供了一種抽象,而對使用者屏蔽了對象創(chuàng)建的具體細節(jié)過程,工廠模式有三種:簡單工廠模式,抽象工廠模式和工廠方法模式。 (1).簡單工廠模式: 又叫靜態(tài)工廠模式,簡單工廠只包括一個抽象產(chǎn)品類(該類可以是接口,也可以是具體的類),所有需要的產(chǎn)品類都是該抽象產(chǎn)品類的子類。簡單工廠模式中工廠為具體產(chǎn)品工廠,產(chǎn)品為抽象產(chǎn)品,由工廠實例創(chuàng)建產(chǎn)品實例: 一個生成圓形和矩形的圖形工廠,例子如下: [java] view plaincopy 1. //圖形接口 2. interface Shape(){ 3. public void draw()。 4. } 5. //圓形 6. class Circle implements Shape{ 7. public void draw(){ 8. (“Circle is drawing”)。 9. } 10. } 11. //矩形 12. class Rectangle implements Shape{ 13. public void draw(){ 14. (“Rectangle is drawing”)。 15. } 16. } 17. //圖形工廠 18. class ShapeFactory{ 19. public static Shape createShape(String name) throws InstantiationException, 20. IllegalAccessException, 21. ClassNotFoundException 22. { 23. //使用 java 的反射機制來產(chǎn)生對象實例 24. return (Shape)(name).newInstance()。 25. } 26. } 27. public class ShapeDemo{ 28. public static void draw(Shape shape){ 29. ()。 30. } 31. public static void main(String[] args){ 32. draw((“Circle”))。 33. draw((“Rectangle”))。 34. } 35. } 圖形工廠負責具體圖形的對象實例化工作,圖形使用者使用時不需要關心圖形對象的具體產(chǎn)生過程。 (2).抽象工廠模式: 抽象工廠模式中可以包括多個抽象產(chǎn)品類,每個抽象 產(chǎn)品類可以產(chǎn)生出多個具體產(chǎn)品類,一個抽象工廠用于定義所需產(chǎn)品的組合形式,抽象工廠派生具體工廠類,這些具體工廠類就是簡單工廠模式中的工廠類,具體工廠類負責具體產(chǎn)品實例的創(chuàng)建: 以軟件皮膚為例,軟件皮膚由樣式 style 和顏色 color 組成,實現(xiàn)一套 IOS 風格的軟件皮膚,一套 Android 風格的軟件皮膚,通過抽象工廠實 現(xiàn)軟件皮膚自由切換例子如下: [java] view plaincopy 1. //軟件皮膚類 2. class Skin{ 3. private SkinFactory skinFactory。 4. public Skin(SkinFactory factory){ 5. setSkinFactory(factory)。 6. } 7. public void setSkinFactory(SkinFactory factory){ 8. = factory 9. } 10. public void showSkin(){ 11. (“Style=” + ().showStyle() + “, color=” + lor().showColor())。 12. } 13. } 14. //軟件 Style 15. interface Style(){ 16. public void showStyle()。 17. } 18. //IOS style 19. class IOSStyle implements Style{ 20. public void showStyle(){ 21. (“This is IOS style”)。 22. } 23. } 24. //Android style 25. class AndroidStyle implements Style{ 26. public void showStyle(){ 27. (“This is Android style”)。 28. } 29. } 30. //軟件 Color 31. interface Color(){ 32. public void showColor()。 33. } 34. //IOS color 35. class IOSColor implements Color{ 36. public void showColor(){ 37. (“This is IOS color”)。 38. } 39. } 40. //Android color 41. class AndroidColor implements Color{ 42. public void showColor(){ 43. (“This is Android color”)。 44. } 45. } 46. //抽象皮膚工廠 47. interface SkinFactory{ 48. public Style getStyle()。 49. public Color getColor()。 50. } 51. //IOS 皮膚工廠 52. class IOSSkinFactory implements SkinFactory{ 53. public Style getStyle(){ 54. return new IOSStyle()。 55. } 56. public Color getColor(){ 57. return new IOSColor()。 58. } 59. } 60. //Android 皮膚工廠 61. class AndroidSkinFactory implements SkinFactory{ 62. public Style getStyle(){ 63. return new AndroidStyle()。 64. } 65. public Color getColor(){ 66. return new AndroidColor()。 6
點擊復制文檔內(nèi)容
環(huán)評公示相關推薦
文庫吧 www.dybbs8.com
備案圖片鄂ICP備17016276號-1