Decorator_pattern-装饰器模式

解决问题

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

应用场景

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

原理图UML

image

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

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 事件 家里有用了两年多不到三年的家用中央空调,品牌:格力。 一直都很正常的使用,直至出现 故障E3 。E3故障...
    一叶也知秋阅读 2,720评论 0 0

友情链接更多精彩内容