Decorator_pattern-裝飾器模式

解決問題

動態(tài)地為對象添加功能,這是相對于繼承而言的,繼承是在定義類的時候擴展功能,而Decorator_pattern 可以在運行時,動態(tài)地為對象添加功能。

應(yīng)用場景

需要為對象添加一系列功能,但需要添加的功能只有在運行的過程才能知道。比如wikipedia 上舉的咖啡的例子,客人要的咖啡可能需要添加糖、milk等等,在客戶點餐之前,你無法確定需要加哪些東西;另一方面,也要求添加的功能之前應(yīng)該是相互獨立的,不應(yīng)該有先后順序關(guān)系,否則會出問題。

原理圖UML

image

Component 實體對象與修飾器的公共接口

Component1指的是實體對象,即要進(jìn)行修飾的對象

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

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 事件 家里有用了兩年多不到三年的家用中央空調(diào),品牌:格力。 一直都很正常的使用,直至出現(xiàn) 故障E3 。E3故障...
    一葉也知秋閱讀 378評論 0 0

友情鏈接更多精彩內(nèi)容