設計模式(十一)裝飾器模式

版權聲明:轉載必須註明本文轉自曉_晨的博客:http://blog.csdn.net/niunai112

目錄

導航

設計模式之六大設計原則
設計模式(一)單例模式
設計模式(二)工廠模式
設計模式(三)策略模式
設計模式(四)適配器模式
設計模式(五)享元模式
設計模式(六)建造者模式
設計模式(七)原型模式
設計模式(八)橋接模式
設計模式(九)外觀模式
設計模式(十)組合模式
設計模式(十一)裝飾器模式
設計模式(十二)代理模式
設計模式(十三)迭代器模式
設計模式(十四)觀察者模式
設計模式(十五)中介者模式
設計模式(十六)命令模式
設計模式(十七)狀態模式
設計模式(十八)訪問者模式
設計模式(十九)責任鏈模式
設計模式(二十)解釋器模式
設計模式(二十一)備忘錄模式
設計模式(二十二)模板模式
設計模式總結篇(爲什麼要學習設計模式,學習設計模式的好處)

前言

一個詮釋了對擴展開發,對修改關閉原則的設計模式,一個類需要新增行爲時,使用裝飾器模式,可以在不改變原類的基礎上,完成行爲的增加。

例子

遠古時候的人是沒穿衣服的,但是到了後來,社會形成,物質條件好了後,人們會開始穿衣服出門。可以把穿衣服寫成一個裝飾器模型。

/***
 *
 *@Author ChenjunWang
 *@Description:
 *@Date: Created in 17:25 2018/3/30
 *@Modified By:
 *
 */
public interface IHuman {
    void show();
}

/***
 *
 *@Author ChenjunWang
 *@Description:
 *@Date: Created in 17:40 2018/3/30
 *@Modified By:
 *
 */
public class Human implements IHuman {
    @Override
    public void show() {
        System.out.println("一個沒穿衣服的人。");
    }
}

/***
 *
 *@Author ChenjunWang
 *@Description:
 *@Date: Created in 18:31 2018/3/30
 *@Modified By:
 *
 */
public abstract class HumanDecorator implements IHuman {
    protected IHuman human;

    public HumanDecorator(IHuman human){

        this.human = human;
    }
    @Override
    public void show() {

        human.show();
    }
}
/***
 *
 *@Author ChenjunWang
 *@Description:
 *@Date: Created in 17:53 2018/3/30
 *@Modified By:
 *
 */
public class WearHuman extends HumanDecorator{

    public WearHuman(IHuman human){

        super(human);
    }
    public void wear(){
        System.out.println("穿上了衣服");
    }
    public void show(){
        human.show();
        wear();
    }

}

public class Test {
    public static void main(String[] args) {
        Human human = new Human();
        System.out.println("-------------------遠古時候的人,出去的時候---------------------");
        human.show();
        System.out.println("-------------------後來有衣服的時候,出去的時候---------------------");
        WearHuman wearHuman = new WearHuman(human);
        wearHuman.show();
    }
}
運行結果如下
--------------------

-------------------遠古時候的人,出去的時候---------------------
一個沒穿衣服的人。
-------------------後來有衣服的時候,出去的時候---------------------
一個沒穿衣服的人。
穿上了衣服

裝飾器模式是爲了解決,當現有的接口無法滿足業務需求時的補救的模式,是一個非常靈活的模式,它能在爲一個方法的前後進行包裝,不僅如此,它能無限進行包裝,當業務再次發生變動時,能很快的進行調整,比如後來,人們的物質生活水平提高後,人們出門會帶飾品。只需要增加一個飾品的裝飾類就可以了。

/***
 *
 *@Author ChenjunWang
 *@Description:
 *@Date: Created in 18:27 2018/3/30
 *@Modified By:
 *
 */
public class JewelleryHuman extends HumanDecorator {

    public JewelleryHuman(IHuman human){

        super(human);
    }
    public void wearJewl(){
        System.out.println("帶上飾品");
    }
    @Override
    public void show() {

        human.show();
        wearJewl();

    }
}


/***
 *
 *@Author ChenjunWang
 *@Description:
 *@Date: Created in 18:15 2018/3/30
 *@Modified By:
 *
 */
public class Test {
    public static void main(String[] args) {

        Human human = new Human();
        System.out.println("-------------------遠古時候的人,出去的時候---------------------");
        human.show();
        System.out.println("-------------------後來有衣服的時候,出去的時候---------------------");
        WearHuman wearHuman = new WearHuman(human);
        wearHuman.show();

        System.out.println("-------------------再後來物質條件好的時候,出去的時候---------------------");
        JewelleryHuman jewelleryHuman = new JewelleryHuman(wearHuman);
        jewelleryHuman.show();
    }
}
運行結果如下
--------------------
-------------------遠古時候的人,出去的時候---------------------
一個沒穿衣服的人。
-------------------後來有衣服的時候,出去的時候---------------------
一個沒穿衣服的人。
穿上了衣服
-------------------再後來物質條件好的時候,出去的時候---------------------
一個沒穿衣服的人。
穿上了衣服
帶上飾品

總結

優點

(1)擴展對象功能,比繼承靈活,不會導致類個數急劇增加
(2)可以根據業務快速的在原有類上進行調整。

缺點

(1)利用裝飾器模式,常常造成設計中有大量的小類,數量實在太多,可能會造成使用此API程序員的困擾。

Git地址

本篇實例Github地址:https://github.com/stackisok/Design-Pattern/tree/master/src/decorator

回到最上方


有什麼不懂或者不對的地方,歡迎留言。
喜歡LZ文章的小夥伴們,可以關注一波,也可以留言,LZ會回你們的。
覺得寫得不錯的小夥伴,歡迎轉載,但請附上原文地址,謝謝^_^!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章