装饰者模式(Decorator Pattern) 具体解释

本文地址: http://blog.csdn.net/caroline_wendy/article/details/26707033

装饰者模式(Decorator Pattern):动态地将责任附加到对象上. 若要扩展功能, 装饰者提供了比继承更有弹性的替代方案.

用法:

1. 首先创建组件(Component)父类, 全部类,详细组件(Concrete Component)装饰者(Decorator)都属于这一类型,
能够进行扩展;

能够是抽象类(abstract class), 也能够是接口(interface);

代码:

  1. /**
  2. * @time 2014年5月23日
  3. */
  4. package decorator;
  5.  
  6. /**
  7. * @author C.L.Wang
  8. *
  9. */
  10. public abstract class Beverage {
  11. String description = "Unkown Beverage";
  12.  
  13. public String getDescription() {
  14. return description;
  15. }
  16.  
  17. public abstract double cost();
  18. }

2. 装饰者(Decorator)类父类, 继承组件(component)父类, 可是不要实现接口函数, 由它的继承类(详细的装饰者)去实现,

能够是抽象类(abstract class),
也能够是接口(interface);

代码:

  1. /**
  2. * @time 2014年5月23日
  3. */
  4. package decorator;
  5.  
  6. /**
  7. * @author C.L.Wang
  8. *
  9. */
  10. public abstract class CondimentDecorator extends Beverage {
  11. public abstract String getDescription();
  12.  
  13. }

3. 详细组件(Concrete Component), 即装饰者(Decorator)须要装饰的基础, 继承组件(Component)父类;

代码:

  1. /**
  2. * @time 2014年5月23日
  3. */
  4. package decorator;
  5.  
  6. /**
  7. * @author C.L.Wang
  8. *
  9. */
  10. public class DarkRoast extends Beverage {
  11.  
  12. public DarkRoast() {
  13. description = "Dark Roast";
  14. }
  15.  
  16. /* (non-Javadoc)
  17. * @see decorator.Beverage#cost()
  18. */
  19. @Override
  20. public double cost() {
  21. // TODO Auto-generated method stub
  22. return 0.99;
  23. }
  24.  
  25. }
  26.  
  27. /**
  28. * @time 2014年5月23日
  29. */
  30. package decorator;
  31.  
  32. /**
  33. * @author C.L.Wang
  34. *
  35. */
  36. public class Decat extends Beverage {
  37.  
  38. public Decat() {
  39. description = "Decat";
  40. }
  41.  
  42. /* (non-Javadoc)
  43. * @see decorator.Beverage#cost()
  44. */
  45. @Override
  46. public double cost() {
  47. // TODO Auto-generated method stub
  48. return 1.05;
  49. }
  50.  
  51. }
  52.  
  53. /**
  54. * @time 2014年5月23日
  55. */
  56. package decorator;
  57.  
  58. /**
  59. * @author C.L.Wang
  60. *
  61. */
  62. public class Espresso extends Beverage {
  63.  
  64. public Espresso() {
  65. description = "Espresso";
  66. }
  67.  
  68. /* (non-Javadoc)
  69. * @see decorator.Beverage#cost()
  70. */
  71. @Override
  72. public double cost() {
  73. // TODO Auto-generated method stub
  74. return 1.99;
  75. }
  76.  
  77. }
  78.  
  79. /**
  80. * @time 2014年5月23日
  81. */
  82. package decorator;
  83.  
  84. /**
  85. * @author C.L.Wang
  86. *
  87. */
  88. public class HouseBlend extends Beverage {
  89.  
  90. public HouseBlend() {
  91. description = "House Blend Coffee";
  92. }
  93.  
  94. /* (non-Javadoc)
  95. * @see decorator.Beverage#cost()
  96. */
  97. @Override
  98. public double cost() {
  99. // TODO Auto-generated method stub
  100. return .89;
  101. }
  102.  
  103. }

4. 装饰者(Decorator)类继承装饰者父类, 实现组件父类的接口装饰者父类的接口.

代码:

  1. /**
  2. * @time 2014年5月23日
  3. */
  4. package decorator;
  5.  
  6. /**
  7. * @author C.L.Wang
  8. *
  9. */
  10. public class Milk extends CondimentDecorator {
  11.  
  12. Beverage beverage;
  13.  
  14. public Milk(Beverage beverage) {
  15. this.beverage = beverage;
  16. }
  17.  
  18. /* (non-Javadoc)
  19. * @see decorator.CondimentDecorator#getDescription()
  20. */
  21. @Override
  22. public String getDescription() {
  23. // TODO Auto-generated method stub
  24. return beverage.getDescription() + ", Milk";
  25. }
  26.  
  27. /* (non-Javadoc)
  28. * @see decorator.Beverage#cost()
  29. */
  30. @Override
  31. public double cost() {
  32. // TODO Auto-generated method stub
  33. return 0.10 + beverage.cost();
  34. }
  35.  
  36. }
  37.  
  38. /**
  39. * @time 2014年5月23日
  40. */
  41. package decorator;
  42.  
  43. /**
  44. * @author C.L.Wang
  45. *
  46. */
  47. public class Mocha extends CondimentDecorator {
  48.  
  49. Beverage beverage;
  50.  
  51. public Mocha(Beverage beverage) {
  52. this.beverage = beverage;
  53. }
  54.  
  55. /* (non-Javadoc)
  56. * @see decorator.CondimentDecorator#getDescription()
  57. */
  58. @Override
  59. public String getDescription() {
  60. // TODO Auto-generated method stub
  61. return beverage.getDescription() + ", Mocha";
  62. }
  63.  
  64. @Override
  65. public double cost() {
  66. // TODO Auto-generated method stub
  67. return 0.20+beverage.cost();
  68. }
  69.  
  70. }
  71.  
  72. /**
  73. * @time 2014年5月23日
  74. */
  75. package decorator;
  76.  
  77. /**
  78. * @author C.L.Wang
  79. *
  80. */
  81. public class Soy extends CondimentDecorator {
  82.  
  83. Beverage beverage;
  84.  
  85. public Soy(Beverage beverage) {
  86. this.beverage = beverage;
  87. }
  88.  
  89. /* (non-Javadoc)
  90. * @see decorator.CondimentDecorator#getDescription()
  91. */
  92. @Override
  93. public String getDescription() {
  94. // TODO Auto-generated method stub
  95. return beverage.getDescription() + ", Soy";
  96. }
  97.  
  98. /* (non-Javadoc)
  99. * @see decorator.Beverage#cost()
  100. */
  101. @Override
  102. public double cost() {
  103. // TODO Auto-generated method stub
  104. return 0.15 + beverage.cost();
  105. }
  106.  
  107. }
  108.  
  109. /**
  110. * @time 2014年5月23日
  111. */
  112. package decorator;
  113.  
  114. /**
  115. * @author C.L.Wang
  116. *
  117. */
  118. public class Whip extends CondimentDecorator {
  119.  
  120. Beverage beverage;
  121.  
  122. public Whip(Beverage beverage) {
  123. this.beverage = beverage;
  124. }
  125.  
  126. /* (non-Javadoc)
  127. * @see decorator.CondimentDecorator#getDescription()
  128. */
  129. @Override
  130. public String getDescription() {
  131. // TODO Auto-generated method stub
  132. return beverage.getDescription() + ", Whip";
  133. }
  134.  
  135. /* (non-Javadoc)
  136. * @see decorator.Beverage#cost()
  137. */
  138. @Override
  139. public double cost() {
  140. // TODO Auto-generated method stub
  141. return 0.10 + beverage.cost();
  142. }
  143.  
  144. }

5. 測试, 创建详细组件(Concrete Component), 再一层一层加入装饰者(Decorator)类, 能够实现动态的组合;

代码:

  1. /**
  2. * @time 2014年5月23日
  3. */
  4. package decorator;
  5.  
  6. /**
  7. * @author C.L.Wang
  8. *
  9. */
  10. public class StarbuzzCoffee {
  11.  
  12. /**
  13. * @param args
  14. */
  15. public static void main(String[] args) {
  16. // TODO Auto-generated method stub
  17. Beverage beverage = new Espresso();
  18. System.out.println(beverage.getDescription() +
  19. " $" + beverage.cost());
  20.  
  21. Beverage beverage2 = new DarkRoast();
  22. beverage2 = new Mocha(beverage2);
  23. beverage2 = new Mocha(beverage2);
  24. beverage2 = new Whip(beverage2);
  25. System.out.println(beverage2.getDescription() +
  26. " $" + beverage2.cost());
  27.  
  28. Beverage beverage3 = new HouseBlend();
  29. beverage3 = new Soy(beverage3);
  30. beverage3 = new Mocha(beverage3);
  31. beverage3 = new Whip(beverage3);
  32. System.out.println(beverage3.getDescription() +
  33. " $" + beverage3.cost());
  34. }
  35.  
  36. }

6. 输出:

  1. Espresso $1.99
  2. Dark Roast, Mocha, Mocha, Whip $1.49
  3. House Blend Coffee, Soy, Mocha, Whip $1.34

面向对象的原则:

对扩展开发, 对改动关闭.

设计模式 - 装饰者模式(Decorator Pattern) 具体解释的更多相关文章

  1. 浅谈设计模式--装饰者模式(Decorator Pattern)

    挖了设计模式这个坑,得继续填上.继续设计模式之路.这次讨论的模式,是 装饰者模式(Decorator Pattern) 装饰者模式,有时也叫包装者(Wrapper),主要用于静态或动态地为一个特定的对 ...

  2. 设计模式 - 装饰者模式(Decorator Pattern) Java的IO类 用法

    装饰者模式(Decorator Pattern) Java的IO类 用法 本文地址: http://blog.csdn.net/caroline_wendy/article/details/26716 ...

  3. C#设计模式——装饰者模式(Decorator Pattern)

    一.例子在软件开发中,我们往往会想要给某一类对象增加不同的功能.比如要给汽车增加ESP.天窗或者定速巡航.如果利用继承来实现,就需要定义无数的类,Car,ESPCar,CCSCar,SunRoofCa ...

  4. 设计模式学习--装饰者模式(Decorator Pattern)

    概念: 装饰者模式(Decorator Pattern): 动态地将功能添加到对象,相比生成子类更灵活,更富有弹性. 解决方案: 装饰者模式的重点是对象的类型,装饰者对象必须有着相同的接口,也也就是有 ...

  5. 大话设计模式--装饰者模式 Decorator -- C++实现实例

    1.装饰者模式 Decorator 动态地给一个对象添加一个额外的职责, 就添加功能来说, 装饰模式比生成子类更为灵活. 每个装饰对象的实现和如何使用这个对象分离,  每个装饰对象只关心自己的功能,不 ...

  6. 23种设计模式之装饰器模式(Decorator Pattern)

    装饰器模式(Decorator Pattern) 允许向一个现有的对象添加新的功能,同时又不改变其结构.这种类型的设计模式属于结构型模式,它是作为现有的类的一个包装. 这种模式创建了一个装饰类,用来包 ...

  7. 设计模式(三):“花瓶+鲜花”中的装饰者模式(Decorator Pattern)

    在前两篇博客中详细的介绍了"策略模式"和“观察者模式”,今天我们就通过花瓶与鲜花的例子来类比一下“装饰模式”(Decorator Pattern).在“装饰模式”中很好的提现了开放 ...

  8. C#设计模式之装饰者模式(Decorator Pattern)

    1.概述 装饰者模式,英文名叫做Decorator Pattern.装饰模式是在不必改变原类文件和使用继承的情况下,动态地扩展一个对象的功能.它是通过创建一个包装对象,也就是装饰来包裹真实的对象. 2 ...

  9. Android设计模式之中的一个个样例让你彻底明确装饰者模式(Decorator Pattern)

    导读 这篇文章中我不会使用概念性文字来说明装饰者模式.由于通常概念性的问题都非常抽象.非常难懂.使得读者非常难明确究竟为什么要使用这样的设计模式.我们设计模式的诞生,肯定是前辈们在设计程序的时候遇到了 ...

随机推荐

  1. ASP.NET MVC中URL末尾斜杠的实现

    在网站的SEO优化中,通常都会涉及到URL结尾斜杠的问题. http://blog.sina.com.cn/s/blog_828e7ce40100srj1.html http://www.dengyo ...

  2. Android开发之漫漫长途 Ⅴ——Activity的显示之ViewRootImpl的PreMeasure、WindowLayout、EndMeasure、Layout、Draw

    该文章是一个系列文章,是本人在Android开发的漫漫长途上的一点感想和记录,我会尽量按照先易后难的顺序进行编写该系列.该系列引用了<Android开发艺术探索>以及<深入理解And ...

  3. canvas画布实现手写签名效果

    最近项目中涉及到移动端手写签名的功能需求,将实现代码记录于此,供小伙伴们参考指摘哦~ HTML代码: <!--手写区--> <div class="mSign_signMa ...

  4. 创建并在项目中调用SQLSERVER存储过程的简单示例

    使用SQLSERVER存储过程可以很大的提高程序运行速度,简化编程维护难度,现已得到广泛应用.创建存储过程 和数据表一样,在使用之前需要创建存储过程,它的简明语法是: 引用: Create PROC ...

  5. ES6中class关键字

    1 .介绍 //定义类 class Point { constructor(x, y) { this.x = x; this.y = y; } toString() { return '(' + th ...

  6. pt-query-digest

    pt-query-digest默认查询时间分布 # Query_time distribution # 1us # 10us ##################################### ...

  7. iOS 远程推送消息解析及逻辑处理

    关于远程推送的相关配置网上已经有足够多的教程,这里就不复述了.这里讲述当客户端收到推送消息后,应怎样对其进行相应的逻辑处理. 工程的AppDelegate.m文件里提供了如下方法: //当应用程序启动 ...

  8. C# 处理Word自动生成报告 二、数据源例子

    还是以学生.语文.数学.分数为例吧, 感觉这个和helloworld都有一拼了. 造一张表如下, 整张报表就围绕这个表转圈了, 顺便说下就是名字如有雷同纯属巧合 新建个存储过程 ALTER PROCE ...

  9. css中的float和position

    1.float <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...

  10. VS2015远程调试

    原文链接 VS2015远程调试   在PayPal支付时,PayPal回调函数一直报错,本地没有外网IP,没有办法在本地调试,需要远程调试: 1.找到远程调试的文件夹: 找到对应的服务器的型号:64位 ...