Decorator pattern
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 coffeepublic 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 classreturn 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 superclassreturn 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 typec = 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, Sprinkles3. 優缺點(http://tianli.blog.51cto.com/190322/35287/)
Decorator模式有以下的優缺點: 1.??????比靜態繼承更靈活?與對象的靜態繼承相比,Decorator模式提供了更加靈活的向對象添加職責的方式,可以使用添加和分離的方法,用裝飾在運行時刻增加和刪除職責。使用繼承機制增加職責需要創建一個新的子 ? ? ? ? ? ?類,如果需要為原來所有的子類都添加功能的話,每個子類都需要重寫,增加系統的復雜度,此外可以為一個特定的Component類提供多個Decorator,這種混合匹配是適用繼承很難做到的。 2.??????避免在層次結構高層的類有太多的特征,Decorator模式提供了一種“即用即付”的方法來添加職責,他并不試圖在一個復雜的可訂制的類中支持所有可預見的特征,相反可以定義一個簡單的類,并且用Decorator ? ? ? ? ? ?類給他逐漸的添加功能,可以從簡單的部件組合出復雜的功能。 3.??????Decorator?與它的Component不一樣?Decorator是一個透明的包裝,如果我們從對象標識的觀點出發,一個被裝飾了的組件與這個組件是有差別的,因此使用裝飾時不應該以來對象標識。 4.??????產生許多小對象,采用Decorator模式進行系統設計往往會產生許多看上去類似的小對象,這些對象僅僅在他們相互連接的方式上有所不同。?
?
轉載于:https://www.cnblogs.com/davidwang456/p/3844770.html
總結
以上是生活随笔為你收集整理的Decorator pattern的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: spring-data-redis工程
- 下一篇: Adapter pattern