設計模式筆記之Decorator Pattern
阿新 • • 發佈:2019-02-11
裝飾模式: Decorator Pattern
概括: 動態地給一個物件新增一些額外的職責。單從增加功能來說,用裝飾模式比簡單使用子類繼承的方式來的靈活。
關鍵字: Has-a,
重點: 每個裝飾類都要實現定義好的實現功能的純虛裝飾函式。各個裝飾類都has-a一個擁有基本功能的類,通過對介面的實現,給has-a關係的基礎功能增加新的功能
類圖:
待裝飾的基類:
// The Coffee Interface defines the functionality of Coffee implemented by decorator public interface Coffee { publicdouble getCost(); // returns the cost of the coffee public String getIngredients(); // returns the ingredients of the coffee } // implementation of a simple coffee without any extra ingredients public class SimpleCoffee implements Coffee { public double getCost() { return 1; } publicString getIngredients() { return "Coffee"; } }
----------------------虛裝飾類:
// abstract decorator class - note that it implements Coffee interface abstract public class CoffeeDecorator implements Coffee { protectedfinal Coffee decoratedCoffee; // has-a一個待裝飾的基類 protected String ingredientSeparator =", "; public CoffeeDecorator(Coffee decoratedCoffee) { this.decoratedCoffee = decoratedCoffee; } public double getCost() { // implementing methods of the interface return decoratedCoffee.getCost(); } public String getIngredients() { return decoratedCoffee.getIngredients(); } }--------------------------------------------------------------------------------------
實際增加功能的裝飾類,即提供給使用者使用的裝飾類:
// Decorator Milk that mixes milk with coffee // note it extends CoffeeDecorator public 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"; } }
--------------------------------------------------------------------------------------
實際使用者子類:
public class Main { public static 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()); ... } }