java程序设计模式程序设计(编辑修改稿)内容摘要:

)。 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 对象直接调用 iterator()方法就可以将自定义容器对象转换为迭代器对象。 Iterator 模式的优点: (1).实现功能分离,简化容器接口。 让容器只实现本身的基本功能,把迭代功能委让给外部类实现,符合类的设计原则。 (2).隐藏容器的实现细节。 (3).为容器或其子容器提供了一个统一接口,一方面方便调用;另一方面使得调 用者不必关注迭代器的实现细节。 (4).可以为容器或其子容器实现不同的迭代方法或多个迭代方法。 Strategy 设计模式 Strategy 策略设计模式主 要是定义一系列的算法,把这些算法封装成单独的类,在运行时动态选择需要的算法,策略模式机制如下: 策略模式例子如下: [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 状态模式和 Strategy 策略模式非常类似,但是有如下区别: (1).State 状态模式重点在于设定状态变化,根据状态,返回相应的响应。 (2).Strategy 策略模式重点在于根据需求直接采用设定的策略,即根据场景选择合适的处理算法,而不需要改变状态。 Factory 设计模式 Factory 工厂设计模式为创建对象提供了一种抽象,而对使用者屏蔽了对象创建的具体细节过程,工厂模式有三种:简单工厂模式,抽象工厂模式和工厂方法模式。 (1).简单工厂模式: 又叫静态工厂模式,简单工厂只包括一个抽象产品类(该类可以是接口,也可以是具体的类),所有需要的产品类都是该抽象产品类的子类。 简单工厂模式中工厂为具体产品工厂,产品为抽象产品,由工厂实例创建产品实例: 一个生成圆形和矩形的图形工厂,例子如下: [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 的反射机制来产生对象实例 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. } 图形工厂负责具体图形的对象实例化工作,图形使用者使用时不需要关心图形对象的具体产生过程。 (2).抽象工厂模式: 抽象工厂模式中可以包括多个抽象产品类,每个抽象 产品类可以产生出多个具体产品类,一个抽象工厂用于定义所需产品的组合形式,抽象工厂派生具体工厂类,这些具体工厂类就是简单工厂模式中的工厂类,具体工厂类负责具体产品实例的创建: 以软件皮肤为例,软件皮肤由样式 style 和颜色 color 组成,实现一套 IOS 风格的软件皮肤,一套 Android 风格的软件皮肤,通过抽象工厂实 现软件皮肤自由切换例子如下: [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。
阅读剩余 0%
本站所有文章资讯、展示的图片素材等内容均为注册用户上传(部分报媒/平媒内容转载自网络合作媒体),仅供学习参考。 用户通过本站上传、发布的任何内容的知识产权归属用户或原始著作权人所有。如有侵犯您的版权,请联系我们反馈本站将在三个工作日内改正。