設計模式之八 — 裝飾模式(Decorator)

來源:互聯網
上載者:User

【1】基本概念

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

【2】簡單分析

          我們先來看下該設計模式的UML結構圖

是Decorator 模式的結構圖,讓我們可以進行更方便的描述:

Component是定義一個對象介面,可以給這些對象動態地添加職責。

ConcreteComponent是定義了一個具體的對象,也可以給這個對象添加一些職責。

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

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

【3】如何用Java語音來實現該設計模式

假設情景:某人裝扮自己形象,穿衣服,褲子,鞋子,戴帽子等來把自己給封裝起來,需要把所需的功能按正確的順序串聯起來進行控制,我們應該如何設計才能做到呢?如下,先看下代碼結構圖:


3.1 先建立一個介面類:Component.java 

package com.andyidea.patterns.component;public interface Component {void show();}

3.2 建立一個具體的 ConcreteComponent 來實現 Component 介面:Person.java

package com.andyidea.patterns.concretecomponent;import com.andyidea.patterns.component.Component;public class Person implements Component{private String name;public String getName() {return name;}public void setName(String name) {this.name = name;}public Person(String name){this.name = name;}@Overridepublic void show() {System.out.println("裝扮的" + name);}}

3.3 建立裝飾類 Decorator 實現 Component 介面

package com.andyidea.patterns.decorator;import com.andyidea.patterns.component.Component;public class Decorator implements Component{private Component mComponent;public void decoratorObj(Component component){mComponent = component;}@Overridepublic void show() {if(mComponent != null){mComponent.show();}}}

3.4 分別建立具體的裝飾類:Jeans.java , Pelisse.java, Sandal.java ...等等,分別繼承 Decorator.java 類:

package com.andyidea.patterns.concretedecorator;import com.andyidea.patterns.decorator.Decorator;/** 牛仔褲 */public class Jeans extends Decorator {@Overridepublic void show(){System.out.println("穿牛仔褲");super.show();}}

其餘類類似,在這裡就省略了。

3.5 用戶端測試類別:

package com.andyidea.patterns;import com.andyidea.patterns.concretecomponent.Person;import com.andyidea.patterns.concretedecorator.Jeans;import com.andyidea.patterns.concretedecorator.Sandal;import com.andyidea.patterns.concretedecorator.TShirt;/** * 裝飾模式測試用戶端 * @author Andy.Chen * */public class DecoratorClient {public static void main(String[] args) {System.out.println("Welcome to Andy.Chen Blog!" +"\n"            +"Decorator Patterns." +"\n");Person mPerson = new Person("Andy");Sandal mSandal = new Sandal();Jeans mJeans = new Jeans();TShirt mShirt = new TShirt();mShirt.decoratorObj(mPerson);mJeans.decoratorObj(mShirt);mSandal.decoratorObj(mJeans);mSandal.show();}}

【4】測試顯示輸出的結果如下:

Welcome to Andy.Chen Blog!Decorator Patterns.穿涼鞋穿牛仔褲穿T-Shirt裝扮的Andy

【5】總結:Decorator模式有以下的優缺點:

1.比靜態繼承更靈活與對象的靜態繼承相比,Decorator模式提供了更加靈活的向對象添加職責的方式,可以使用添加和分離的方法,用裝飾在運行時刻增加和刪除職責。使用繼承機制增加職責需要建立一個新的子類,如果需要為原來所有的子類都添加功能的話,每個子類都需要重寫,增加系統的複雜度,此外可以為一個特定的Component類提供多個Decorator,這種混合匹配是適用繼承很難做到的。2.避免在階層高層的類有太多的特徵,Decorator模式提供了一種“即用即付”的方法來添加職責,他並不試圖在一個複雜的可訂製的類中支援所有可預見的特徵,相反可以定義一個簡單的類,並且用Decorator類給他逐漸的添加功能,可以從簡單的組件組合出複雜的功能。3.Decorator
與它的Component不一樣 Decorator是一個透明的封裝,如果我們從對象標識的觀點出發,一個被裝飾了的組件與這個組件是有差別的,因此使用裝飾時不應該以來對象標識。4.產生許多小對象,採用Decorator模式進行系統設計往往會產生許多看上去類似的小對象,這些對象僅僅在他們相互串連的方式上有所不同。

註:本文為Andy.Chen原創,歡迎大家轉載,轉載請大家註明出處,謝謝!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.