Decorator pattern

1. 定義,來自wiki(http://en.wikipedia.org/wiki/Decorator_pattern)app

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:ide

  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 coffee
    public 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 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";
    }
}

測試類this

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());
    }
 
}

輸出結果spa

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模式進行系統設計每每會產生許多看上去相似的小對象,這些對象僅僅在他們相互鏈接的方式上有所不一樣。
相關文章
相關標籤/搜索