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:
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"; } }
测试类
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: 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/)