首页 > 代码库 > Decorator pattern

Decorator pattern

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 decoratorpublic abstract class Coffee {    public abstract double getCost(); // Returns the cost of the coffee    public abstract String getIngredients(); // Returns the ingredients of the coffee} // Extension of a simple coffee without any extra ingredientspublic class SimpleCoffee extends Coffee {    public double getCost() {        return 1;    }     public String getIngredients() {        return "Coffee";    }}
// Abstract decorator class - note that it extends Coffee abstract classpublic 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 class        return 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 superclass        return 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 type    c = new Sprinkles(c);    System.out.println("Cost: " + c.getCost() + "; Ingredients: " + c.getIngredients());    } }

输出结果

Cost: 1.0; Ingredients: CoffeeCost: 1.5; Ingredients: Coffee, MilkCost: 1.7; Ingredients: Coffee, Milk, SprinklesCost: 2.4; Ingredients: Coffee, Milk, Sprinkles, WhipCost: 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模式进行系统设计往往会产生许多看上去类似的小对象,这些对象仅仅在他们相互连接的方式上有所不同。