国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

java設計模式系列之裝飾者模式

2019-11-26 14:35:44
字體:
來源:轉載
供稿:網友

何為裝飾者模式 (Decorator)?
動態地給一個對象添加一些額外的職責。就增加功能來說,Decorator 模式相比生成子類更為靈活。
一、結構

Component : 定義一個對象接口,可以給這些對象動態地添加職責。

interface Component {  public void operation();}ConcreteComponent : 實現 Component 定義的接口。class ConcreteComponent implements Component {  @Override  public void operation() {    System.out.println("初始行為");  }}

Decorator : 裝飾抽象類,繼承了 Component, 從外類來擴展 Component 類的功能,但對于 Component 來說,是無需知道 Decorator 的存在的。

class Decorator implements Component {  // 持有一個 Component 對象,和 Component 形成聚合關系  protected Component component;    // 傳入要進一步修飾的對象  public Decorator(Component component) {    this.component = component;  }    @Override  // 調用要修飾對象的原方法  public void operation() {    component.operation();  }}

ConcreteDecorator : 具體的裝飾對象,起到給 Component 添加職責的功能。

class ConcreteDecoratorA extends Decorator {  private String addedState = "新屬性1";    public ConcreteDecoratorA(Component component) {    super(component);  }    public void operation() {    super.operation();    System.out.println("添加屬性: " + addedState);  }}class ConcreteDecoratorB extends Decorator {  public ConcreteDecoratorB(Component component) {    super(component);  }  public void operation() {    super.operation();    AddedBehavior();  }    public void AddedBehavior() {    System.out.println("添加行為");  }}

測試代碼

public class DecoratorPattern {  public static void main(String[] args) {    Component component = new ConcreteComponent();    component.operation();        System.out.println("======================================");    Decorator decoratorA = new ConcreteDecoratorA(component);    decoratorA.operation();        System.out.println("======================================");    Decorator decoratorB = new ConcreteDecoratorB(decoratorA);    decoratorB.operation();  }}

運行結果

初始行為======================================初始行為添加屬性: 新屬性1======================================初始行為添加屬性: 新屬性1添加行為

 二、應用場景

1、需要動態的、透明的為一個對象添加職責,即不影響其他對象。
2、需要動態的給一個對象添加功能,這些功能可以再動態的撤銷。
3、需要增加由一些基本功能的排列組合而產生的非常大量的功能,從而使繼承關系變的不現實。
4、當不能采用生成子類的方法進行擴充時。一種情況是,可能有大量獨立的擴展,為支持每一種組合將產生大量的子類,使得子類數目呈爆炸性增長。另一種情況可能是因為類定義被隱藏,或類定義不能用于生成子類。

三、要點

1、裝飾對象和真實對象有相同的接口。這樣客戶端對象就能以和真實對象相同的方式和裝飾對象交互。

2、裝飾對象包含一個真實對象的引用(reference)。

3、裝飾對象接受所有來自客戶端的請求。它把這些請求轉發給真實的對象。

4、裝飾對象可以在轉發這些請求以前或以后增加一些附加功能。這樣就確保了在運行時,不用修改給定對象的結構就可以在外部增加附加的功能。在面向對象的設計中,通常是通過繼承來實現對給定類的功能擴展。

以上就是關于java裝飾者模式的相關內容介紹,希望對大家的學習有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 大理市| 张家川| 马边| 敦煌市| 河北区| 湖南省| 邵东县| 铁岭县| 庆安县| 博客| 搜索| 景东| 绵阳市| 错那县| 海伦市| 郎溪县| 尼木县| 区。| 巴彦淖尔市| 东莞市| 拜城县| 乌兰浩特市| 松溪县| 奉化市| 永吉县| 裕民县| 马尔康县| 黑水县| 建平县| 嘉峪关市| 郑州市| 东宁县| 宝鸡市| 盐亭县| 舟山市| 宜宾市| 玛曲县| 华安县| 叙永县| 黄平县| 若尔盖县|