Decorator_pattern-装饰器模式

333 阅读1分钟

解决问题

动态地为对象添加功能,这是相对于继承而言的,继承是在定义类的时候扩展功能,而Decorator_pattern 可以在运行时,动态地为对象添加功能。

应用场景

需要为对象添加一系列功能,但需要添加的功能只有在运行的过程才能知道。比如wikipedia 上举的咖啡的例子,客人要的咖啡可能需要添加糖、milk等等,在客户点餐之前,你无法确定需要加哪些东西;另一方面,也要求添加的功能之前应该是相互独立的,不应该有先后顺序关系,否则会出问题。

原理图UML

Component 实体对象与修饰器的公共接口

Component1指的是实体对象,即要进行修饰的对象

Decorator 用来增增被修饰对象的行为

示例

还是用wikipedia上的例子吧, 比较容易理解

Component

// The interface Coffee defines the functionality of Coffee implemented by decorator 
public interface Coffee { 
    public double getCost(); // Returns the cost of the coffee 
    public String getIngredients(); // Returns the ingredients of the coffee 
}

ConcreteComponent

 
// Extension of a simple coffee without any extra ingredients 
public class SimpleCoffee implements Coffee { 
    @Override 
    public double getCost() { 
        return 1; 
    } 
 
    @Override 
    public String getIngredients() { 
        return "Coffee"; 
    } 
}
decorator

// Abstract decorator class - note that it implements Coffee interface 
public abstract class CoffeeDecorator implements Coffee { 
    protected final Coffee decoratedCoffee; 
 
    public CoffeeDecorator(Coffee c) { 
        this.decoratedCoffee = c; 
    } 
 
    public double getCost() { // Implementing methods of the interface 
        return decoratedCoffee.getCost(); 
    } 
 
    public String getIngredients() { 
        return decoratedCoffee.getIngredients(); 
    } 
} 
 
// Decorator WithMilk mixes milk into coffee. 
// Note it extends CoffeeDecorator. 
class WithMilk extends CoffeeDecorator { 
    public WithMilk(Coffee c) { 
        super(c); 
    } 
 
    public double getCost() { // Overriding methods defined in the abstract superclass 
        return super.getCost() + 0.5; 
    } 
 
    public String getIngredients() { 
        return super.getIngredients() + ", Milk"; 
    } 
} 
 
// Decorator WithSprinkles mixes sprinkles onto coffee. 
// Note it extends CoffeeDecorator. 
class WithSprinkles extends CoffeeDecorator { 
    public WithSprinkles(Coffee c) { 
        super(c); 
    } 
 
    public double getCost() { 
        return super.getCost() + 0.2; 
    } 
 
    public String getIngredients() { 
        return super.getIngredients() + ", Sprinkles"; 
    } 
} 
public class Main { 
    public static void printInfo(Coffee c) { 
        System.out.println("Cost: " + c.getCost() + "; Ingredients: " + c.getIngredients()); 
    } 
 
    public static void main(String[] args) { 
        Coffee c = new SimpleCoffee(); 
        printInfo(c); 
 
        c = new WithMilk(c); 
        printInfo(c); 
 
        c = new WithSprinkles(c); 
        printInfo(c); 
    } 
}

参考

https://en.wikipedia.org/wiki/Decorator_pattern