1. 定义,来自wiki(http://en.wikipedia.org/wiki/Decorator_pattern)

The decorator pattern can be used to extend (decorate) the functionality of a certain object statically, or in some cases at run-time, independently of other instances of the same class, provided some groundwork is done at design time. This is achieved by designing a new decoratorclass that wraps the original class. This wrapping could be achieved by the following sequence of steps:

  1. Subclass the original "Component" class into a "Decorator" class (see UML diagram);
  2. In the Decorator class, add a Component pointer as a field;
  3. Pass a Component to the Decorator constructor to initialize the Component pointer;
  4. In the Decorator class, redirect all "Component" methods to the "Component" pointer; and
  5. In the ConcreteDecorator class, override any Component method(s) whose behavior needs to be modified.

2. 实例,来自wiki(http://en.wikipedia.org/wiki/Decorator_pattern)

// The abstract Coffee class defines the functionality of Coffee implemented by decorator
public abstract class Coffee {public abstract double getCost(); // Returns the cost of the coffeepublic abstract String getIngredients(); // Returns the ingredients of the coffee
}// Extension of a simple coffee without any extra ingredients
public class SimpleCoffee extends Coffee {public double getCost() {return 1;}public String getIngredients() {return "Coffee";}
}

// Abstract decorator class - note that it extends Coffee abstract class
public abstract class CoffeeDecorator extends Coffee {protected final Coffee decoratedCoffee;protected String ingredientSeparator = ", ";public CoffeeDecorator (Coffee decoratedCoffee) {this.decoratedCoffee = decoratedCoffee;}public double getCost() { // Implementing methods of the abstract classreturn decoratedCoffee.getCost();}public String getIngredients() {return decoratedCoffee.getIngredients();}
}

// Decorator Milk that mixes milk with coffee.
// Note it extends CoffeeDecorator.
class Milk extends CoffeeDecorator {public Milk (Coffee decoratedCoffee) {super(decoratedCoffee);}public double getCost() { // Overriding methods defined in the abstract superclassreturn super.getCost() + 0.5;}public String getIngredients() {return super.getIngredients() + ingredientSeparator + "Milk";}
}// Decorator Whip that mixes whip with coffee.
// Note it extends CoffeeDecorator.
class Whip extends CoffeeDecorator {public Whip (Coffee decoratedCoffee) {super(decoratedCoffee);}public double getCost() {return super.getCost() + 0.7;}public String getIngredients() {return super.getIngredients() + ingredientSeparator + "Whip";}
}// Decorator Sprinkles that mixes sprinkles with coffee.
// Note it extends CoffeeDecorator.
class Sprinkles extends CoffeeDecorator {public Sprinkles (Coffee decoratedCoffee) {super(decoratedCoffee);}public double getCost() {return super.getCost() + 0.2;}public String getIngredients() {return super.getIngredients() + ingredientSeparator + "Sprinkles";}
}

测试类

public class Main {public static final void main(String[] args) {Coffee c = new SimpleCoffee();System.out.println("Cost: " + c.getCost() + "; Ingredients: " + c.getIngredients());c = new Milk(c);System.out.println("Cost: " + c.getCost() + "; Ingredients: " + c.getIngredients());c = new Sprinkles(c);System.out.println("Cost: " + c.getCost() + "; Ingredients: " + c.getIngredients());c = new Whip(c);System.out.println("Cost: " + c.getCost() + "; Ingredients: " + c.getIngredients());// Note that you can also stack more than one decorator of the same typec = new Sprinkles(c);System.out.println("Cost: " + c.getCost() + "; Ingredients: " + c.getIngredients());}}

输出结果

Cost: 1.0; Ingredients: Coffee
Cost: 1.5; Ingredients: Coffee, Milk
Cost: 1.7; Ingredients: Coffee, Milk, Sprinkles
Cost: 2.4; Ingredients: Coffee, Milk, Sprinkles, Whip
Cost: 2.6; Ingredients: Coffee, Milk, Sprinkles, Whip, Sprinkles

3. 优缺点(http://tianli.blog.51cto.com/190322/35287/)

Decorator模式有以下的优缺点:
1.      比静态继承更灵活 与对象的静态继承相比,Decorator模式提供了更加灵活的向对象添加职责的方式,可以使用添加和分离的方法,用装饰在运行时刻增加和删除职责。使用继承机制增加职责需要创建一个新的子            类,如果需要为原来所有的子类都添加功能的话,每个子类都需要重写,增加系统的复杂度,此外可以为一个特定的Component类提供多个Decorator,这种混合匹配是适用继承很难做到的。
2.      避免在层次结构高层的类有太多的特征,Decorator模式提供了一种“即用即付”的方法来添加职责,他并不试图在一个复杂的可订制的类中支持所有可预见的特征,相反可以定义一个简单的类,并且用Decorator            类给他逐渐的添加功能,可以从简单的部件组合出复杂的功能。
3.      Decorator 与它的Component不一样 Decorator是一个透明的包装,如果我们从对象标识的观点出发,一个被装饰了的组件与这个组件是有差别的,因此使用装饰时不应该以来对象标识。
4.      产生许多小对象,采用Decorator模式进行系统设计往往会产生许多看上去类似的小对象,这些对象仅仅在他们相互连接的方式上有所不同。

转载于:https://www.cnblogs.com/davidwang456/p/3844770.html

Decorator pattern相关推荐

  1. 装饰模式(Decorator Pattern)

    装饰模式(Decorator Pattern) 一句话                                                                 继承一个抽象类, ...

  2. Laravel学习笔记之Decorator Pattern

    说明:Laravel中Middleware的实现主要利用了Decorator Pattern的设计,本文主要先学习下Decorator Pattern如何实现,为后面学习Middleware的设计做个 ...

  3. 极速理解设计模式系列:23.装饰器模式(Decorator Pattern)

    五个角色:部件(Component).具体部件(ConcreteComponent).装饰抽象类(Decorator).具体装饰对象(ConcreteDecorator).客户端(Client) 部件 ...

  4. Decorator Pattern - C# 3.0 Design Patterns

    Decorator Pattern属于Structural Patterns 介绍: Decorator pattern的作用是提供一种方式动态的给一个对象添加新的职责或状态,被装饰的对象并不知道被& ...

  5. 二十四种设计模式:装饰模式(Decorator Pattern)

    装饰模式(Decorator Pattern) 介绍 动态地给一个对象添加一些额外的职责.就扩展功能而言,它比生成子类方式更为灵活. 示例 有一个Message实体类,某个对象对它的操作有Insert ...

  6. 装饰器模式(Decorator Pattern)

    23种设计模式完整教程 介绍 装饰者模式(Decorator Pattern)是指在不改变原有对象的基础之上,将功能附加到对 象上,提供了比继承更有弹性的替代方案(扩展原有对象的功能),属于结构型模式 ...

  7. C++实现装饰者模式Decorator Pattern

    设计原则5:类应该对扩展开放,对修改关闭. 目标:允许类容易扩展,在不修改现有代码的情况下,就可搭配新的行为. 好处:设计具有弹性可以应对改变,可以接受新的功能来应对改变的需求. 遇到的问题:类数量爆 ...

  8. Java设计模式 Design Pattern:包装模式 Decorator Pattern

    意图 Attach additional responsibilities to an object dynamically. 为一个对象动态的添加职责. Decorators provide a f ...

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

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

最新文章

  1. python tcp服务器 多线程_Python中的多线程TCP服务器
  2. hdu2155 小黑的镇魂曲(dp)
  3. I2C总线之(二)---时序
  4. lisp画靶子 visual_基于VisualLISP的AutoCAD绘图命令的二次开发_沈良翼
  5. android studio 配置国内镜像
  6. java 不允许默认构造_java – 如何使用ObjectMapper去除/序列化不可变对象而不使用默认构造函数?...
  7. python 条件概率_使用Pymc3的条件概率
  8. TCP/IP:TCP SYN Flood攻击原理与实现
  9. 慕课网上的星级评分--学习视频后模仿实现
  10. CPU调度算法——FCFS算法/SJF算法/优先级调度算法/RR算法
  11. java 基础知识3
  12. java mysql 多表查询_MySQL必备知识多表查询
  13. CMOS图像传感器基本原理
  14. Crazy Number
  15. 迅雷服务器有多少硬盘,迅雷真的比BT还伤硬盘吗?
  16. Centos7 glibc库升级到2.23
  17. 搭建手机文件服务器,普通用户的低成本家庭文件服务器(伪NAS)的搭建(手机备份篇)...
  18. Spark SQL 在SparkStreaming中的运用
  19. pycharm中python代码格式化方法
  20. pandas 数据结构--DataFrame

热门文章

  1. 测试晶面间距软件_超逼真动图解析常用15大分析测试仪器,必收藏!SEM, 红外,紫外,核磁,质谱,TEM,ICP等...
  2. html应用缓存,HTML5应用缓存
  3. lex编译dos命令_微软新的命令行工具:Windows Terminal
  4. as3 访问远程计算机,Flash AS3中数据发送与接收
  5. linux last 命令年份,【帝联运维课堂】(第七十二期)Linux下last命令如何显示年份...
  6. 苹果cms的php.ini,苹果cms安装及配置详细教程
  7. C程序背后的故事--头文件、库文件的查找
  8. win10 配置 maven_home 一会儿成功一会儿失败_在macbook上运行移动硬盘里的win10和macos...
  9. bagging 与boosting
  10. 7. Leetcode 611. 有效三角形的个数 (数组-双向双指针)