日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

Decorator pattern

發(fā)布時(shí)間:2025/4/5 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Decorator pattern 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

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:

  • Subclass the original "Component" class into a "Decorator" class (see UML diagram);
  • In the Decorator class, add a Component pointer as a field;
  • Pass a Component to the Decorator constructor to initialize the Component pointer;
  • In the Decorator class, redirect all "Component" methods to the "Component" pointer; and
  • In the ConcreteDecorator class, override any Component method(s) whose behavior needs to be modified.
  • ?

    2. 實(shí)例,來自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";} }

    測(cè)試類

    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());}}

    輸出結(jié)果

    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, Sprinkles

    3. 優(yōu)缺點(diǎn)(http://tianli.blog.51cto.com/190322/35287/)

    Decorator模式有以下的優(yōu)缺點(diǎn): 1.??????比靜態(tài)繼承更靈活?與對(duì)象的靜態(tài)繼承相比,Decorator模式提供了更加靈活的向?qū)ο筇砑勇氊?zé)的方式,可以使用添加和分離的方法,用裝飾在運(yùn)行時(shí)刻增加和刪除職責(zé)。使用繼承機(jī)制增加職責(zé)需要?jiǎng)?chuàng)建一個(gè)新的子 ? ? ? ? ? ?類,如果需要為原來所有的子類都添加功能的話,每個(gè)子類都需要重寫,增加系統(tǒng)的復(fù)雜度,此外可以為一個(gè)特定的Component類提供多個(gè)Decorator,這種混合匹配是適用繼承很難做到的。 2.??????避免在層次結(jié)構(gòu)高層的類有太多的特征,Decorator模式提供了一種“即用即付”的方法來添加職責(zé),他并不試圖在一個(gè)復(fù)雜的可訂制的類中支持所有可預(yù)見的特征,相反可以定義一個(gè)簡單的類,并且用Decorator ? ? ? ? ? ?類給他逐漸的添加功能,可以從簡單的部件組合出復(fù)雜的功能。 3.??????Decorator?與它的Component不一樣?Decorator是一個(gè)透明的包裝,如果我們從對(duì)象標(biāo)識(shí)的觀點(diǎn)出發(fā),一個(gè)被裝飾了的組件與這個(gè)組件是有差別的,因此使用裝飾時(shí)不應(yīng)該以來對(duì)象標(biāo)識(shí)。 4.??????產(chǎn)生許多小對(duì)象,采用Decorator模式進(jìn)行系統(tǒng)設(shè)計(jì)往往會(huì)產(chǎn)生許多看上去類似的小對(duì)象,這些對(duì)象僅僅在他們相互連接的方式上有所不同。

    ?

    ?

    轉(zhuǎn)載于:https://www.cnblogs.com/davidwang456/p/3844770.html

    總結(jié)

    以上是生活随笔為你收集整理的Decorator pattern的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。