日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Decorator pattern

發布時間:2025/4/5 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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:

  • 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. 實例,來自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, Sprinkles

    3. 優缺點(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的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

    主站蜘蛛池模板: 激情福利在线 | 精品少妇人妻av一区二区 | 自拍偷拍国产视频 | 久久精品亚洲无码 | 国产精品久久久久久久久久久久久久 | 国产传媒一区二区三区 | 亚洲精品高潮 | 五个女闺蜜把我玩到尿失禁 | 国外亚洲成av人片在线观看 | 影音先锋在线中文字幕 | 97视频人人| wwwxxx在线观看 | 欧美精产国品一二三区 | 久久久久久久久免费视频 | 中文字幕一区二区三区免费看 | 国产精品搬运 | 毛片久久久久久 | 女同动漫免费观看高清完整版在线观看 | 视频这里只有精品 | 精品少妇人妻av免费久久久 | 欧美特级黄色 | 国产一级片 | 综合网在线视频 | 国产激情小视频 | 97香蕉久久夜色精品国产 | 18禁免费无码无遮挡不卡网站 | 国产无遮挡免费观看视频网站 | 色天天综合 | 精品丰满人妻无套内射 | 国产999视频| 亚洲视频二 | 动漫一区二区 | 青草草在线观看 | 两个女人互添下身爱爱 | 97人人模人人爽人人少妇 | 国产精品免费久久久 | 不卡国产在线 | 麻豆国产在线播放 | a∨鲁丝一区鲁丝二区鲁丝三区 | 91久久精品视频 | 老师张开让我了一夜av | 大陆一级黄色片 | 中文字幕亚洲一区二区三区五十路 | 日韩经典在线 | 国产毛片久久久久久久 | 久久99久久99精品免观看粉嫩 | 国产精品久久久久久人 | 国产成人精品免费视频 | 国产精品无码无卡无需播放器 | 中文字幕av专区 | 亚洲精品高潮久久久久久久 | 午夜激情婷婷 | 无遮挡国产 | 一本大道久久a久久精二百 琪琪色在线视频 | 综合中文字幕 | 亚洲欧美影院 | av在线导航 | 不卡免费av | 无码人妻丰满熟妇区五十路 | 亚洲欧洲日产av | 亚洲毛片a| 精品在线视频一区 | 黑人一区二区三区四区五区 | 成人av在线资源 | 激情综合一区二区三区 | 亚洲欧美网 | 亚洲综合视频一区 | 99视频一区| 精品白浆 | 亚洲毛片一区二区 | 捆绑调教在线观看 | 国产青青操 | 麻豆一区在线 | 中文字幕一区二区三三 | av成人动漫 | 日韩最新中文字幕 | 日韩二级片 | 韩国明星乱淫(高h)小说 | 国产在线免费 | 公交上高潮的丁芷晴 | 日韩不卡的av | 国产精品国产精品 | 日本高清一区二区视频 | 日韩视频在线观看免费 | 国产精品久久久久9999 | 亚洲av无码一区二区三区在线观看 | 免费手机av| 直接看的av网站 | 中国av毛片 | 日韩精品一区二区三区不卡在线 | 影音先锋国产精品 | 婷婷国产在线 | 亚洲天堂网在线观看 | a级一级片 | 精品欧美一区二区在线观看 | 精品国产免费人成在线观看 | 麻豆影视在线免费观看 | 蜜桃视频久久一区免费观看入口 | 国产免费中文字幕 |