組合模式

描述

????組合模式颤难,將對象組合成樹形結構以表示“部分-整體”的層次結構矾麻,組合模式使得用戶對單個對象和組合對象的使用具有一致性提岔。

簡介

安全式組合模式

透明式組合模式

????安全模式的組合模式要求管理聚集的方法只出現(xiàn)在樹枝構件類中匪傍,而不出現(xiàn)在樹葉構件類中。
????透明式的組合模式要求所有的具體構件類观挎,不論樹枝構件還是樹葉構件琴儿,均符合一個固定接口。

角色

  • 抽象構件(Component)角色:這是一個抽象角色嘁捷,它給參加組合的對象定義出公共的接口及其默認行為造成,可以用來管理所有的子對象。組合對象通常把它所包含的子對象當做類型為Component的對象雄嚣。在安全式的組合模式里晒屎,構件角色并不定義出管理子對象的方法,這一定義由樹枝構件對象給出缓升。
  • 樹葉構件(Leaf)角色:樹葉對象是沒有下級子對象的對象鼓鲁,定義出參加組合的原始對象的行為。
  • 樹枝構件(Composite)角色:代表參加組合的有下級子對象的對象港谊。樹枝構件類給出所有的管理子對象的方法骇吭,如add()、delete()以及get()封锉。

優(yōu)缺點

優(yōu)點

  • 組合模式使得客戶端代碼可以一致地處理對象和對象容器绵跷,無需關系處理的單個對象膘螟,還是組合的對象容器。
  • 可以更容易地往組合對象中加入新的構件碾局。
  • 將”客戶代碼與復雜的對象容器結構“解耦荆残。

缺點

  • 使得設計更加復雜【坏保客戶端需要花更多時間理清類之間的層次關系内斯。
  • 在使用組合模式時,其葉子和樹枝的聲明都是實現(xiàn)類像啼,而不是接口俘闯,違反了依賴倒置原則。

使用場景

  • 當想表達對象的部分-整體的層次結構時忽冻。(部分真朗、整體場景,如樹形菜單僧诚,文件遮婶、文件夾的管理。)
  • 希望用戶忽略組合對象與單個對象的不同湖笨,用戶將統(tǒng)一地使用組合結構中的所有對象時旗扑。

示例

安全式組合模式

public interface Component {
    /**
     *  輸出組建自身的名稱
     */
    void operation(String name);
}
public class Composite implements Component {
    /**
     * 組合對象的名字
     */
    private String name;
    /**
     * 用來存儲組合對象中包含的子組件對象
     */
    private List<Component> componentList = new ArrayList<>();

    /**
     * 傳入組合對象的名稱
     *
     * @param name 組合對象的名稱
     */
    public Composite(String name) {
        this.name = name;
    }

    /**
     * 聚集管理方法,增加一個子構件對象
     *
     * @param component 子構件對象
     */
    public void addComponent(Component component) {
        this.addComponent(component);
    }

    /**
     * 聚集管理方法慈省,返回所有子構件對象
     */
    public List<Component> getComponent() {
        return componentList;
    }
    /**
     * 輸出對象的自身結構
     * @param preStr 前綴臀防,主要是按照層級拼接空格,實現(xiàn)向后縮進
     */
    /**
     * 聚集管理方法边败,刪除一個子構件對象
     *
     * @param index 子構件對象的下標
     */
    public void removeComponent(int index) {
        componentList.remove(index);
    }


    /**
     * 輸出組建自身的名稱
     */
    @Override
    public void operation(String name) {
        System.out.println(this.name);
        if (this.componentList != null) {
            for (Component c : componentList) {
                c.operation(name);
            }
        }
    }
}

public class Leaf implements Component {
    private String name;

    public Leaf(String name) {
        this.name = name;
    }

    @Override
    public void operation(String name) {
        System.out.println(name + "-" + this.name);
    }
}

public class Client {
    public static void main(String[] args) {
        Composite root = new Composite("我的電腦");
        Composite c1 = new Composite("C盤");
        Composite c2 = new Composite("D盤");

        Leaf leaf1 = new Leaf("文件1");
        Leaf leaf2 = new Leaf("文件2");
        Leaf leaf3 = new Leaf("文件3");
        Leaf leaf4 = new Leaf("文件4");

        root.addComponent(c1);
        root.addComponent(c2);
        c1.addComponent(leaf1);
        c1.addComponent(leaf2);
        c2.addComponent(leaf3);
        c2.addComponent(leaf4);

        root.operation("test");
    }
}

透明式組合模式

public abstract class Component {

    public abstract void operation(String name);

    public void addComponent(Component component) {
        throw new UnsupportedOperationException("葉子節(jié)點不支持此功能");
    }

    public List<Component> getComponent() {
        throw new UnsupportedOperationException("葉子節(jié)點不支持此功能");
    }

    public void removeComponent(int index) {
        throw new UnsupportedOperationException("葉子節(jié)點不支持此功能");
    }

}
public class Composite extends Component {
    /**
     * 組合對象的名字
     */
    private String name;
    /**
     * 用來存儲組合對象中包含的子組件對象
     */
    private List<Component> componentList = new ArrayList<>();

    /**
     * 傳入組合對象的名稱
     *
     * @param name 組合對象的名稱
     */
    public Composite(String name) {
        this.name = name;
    }

    /**
     * 聚集管理方法袱衷,增加一個子構件對象
     *
     * @param component 子構件對象
     */
    @Override
    public void addComponent(Component component) {
        this.addComponent(component);
    }

    /**
     * 聚集管理方法,返回所有子構件對象
     */
    @Override
    public List<Component> getComponent() {
        return componentList;
    }
    /**
     * 輸出對象的自身結構
     * @param preStr 前綴笑窜,主要是按照層級拼接空格祟昭,實現(xiàn)向后縮進
     */
    /**
     * 聚集管理方法,刪除一個子構件對象
     *
     * @param index 子構件對象的下標
     */
    @Override
    public void removeComponent(int index) {
        componentList.remove(index);
    }

    /**
     * 輸出組建自身的名稱
     */
    @Override
    public void operation(String name) {
        System.out.println(this.name);
        if (this.componentList != null) {
            for (Component c : componentList) {
                c.operation(name);
            }
        }
    }
}
public class Leaf extends Component {
    private String name;

    public Leaf(String name) {
        this.name = name;
    }

    @Override
    public void operation(String name) {
        System.out.println(name + "-" + this.name);
    }

}
public class Client {
    public static void main(String[] args) {
        Composite root = new Composite("我的電腦");
        Composite c1 = new Composite("C盤");
        Composite c2 = new Composite("D盤");

        Component leaf1 = new Leaf("文件1");
        Component leaf2 = new Leaf("文件2");
        Component leaf3 = new Leaf("文件3");
        Component leaf4 = new Leaf("文件4");

        root.addComponent(c1);
        root.addComponent(c2);
        c1.addComponent(leaf1);
        c1.addComponent(leaf2);
        c2.addComponent(leaf3);
        c2.addComponent(leaf4);

        root.operation("test");
    }
}
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末怖侦,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子谜叹,更是在濱河造成了極大的恐慌匾寝,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,651評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件荷腊,死亡現(xiàn)場離奇詭異艳悔,居然都是意外死亡,警方通過查閱死者的電腦和手機女仰,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,468評論 3 392
  • 文/潘曉璐 我一進店門猜年,熙熙樓的掌柜王于貴愁眉苦臉地迎上來抡锈,“玉大人,你說我怎么就攤上這事乔外〈踩” “怎么了?”我有些...
    開封第一講書人閱讀 162,931評論 0 353
  • 文/不壞的土叔 我叫張陵杨幼,是天一觀的道長。 經常有香客問我,道長捣卤,這世上最難降的妖魔是什么耕肩? 我笑而不...
    開封第一講書人閱讀 58,218評論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮欲逃,結果婚禮上找蜜,老公的妹妹穿的比我還像新娘。我一直安慰自己稳析,他們只是感情好洗做,可當我...
    茶點故事閱讀 67,234評論 6 388
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著迈着,像睡著了一般竭望。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上裕菠,一...
    開封第一講書人閱讀 51,198評論 1 299
  • 那天咬清,我揣著相機與錄音,去河邊找鬼奴潘。 笑死旧烧,一個胖子當著我的面吹牛,可吹牛的內容都是我干的画髓。 我是一名探鬼主播掘剪,決...
    沈念sama閱讀 40,084評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼奈虾!你這毒婦竟也來了夺谁?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 38,926評論 0 274
  • 序言:老撾萬榮一對情侶失蹤肉微,失蹤者是張志新(化名)和其女友劉穎匾鸥,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體碉纳,經...
    沈念sama閱讀 45,341評論 1 311
  • 正文 獨居荒郊野嶺守林人離奇死亡勿负,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,563評論 2 333
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了劳曹。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片奴愉。...
    茶點故事閱讀 39,731評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡琅摩,死狀恐怖,靈堂內的尸體忽然破棺而出锭硼,到底是詐尸還是另有隱情房资,我是刑警寧澤,帶...
    沈念sama閱讀 35,430評論 5 343
  • 正文 年R本政府宣布账忘,位于F島的核電站志膀,受9級特大地震影響,放射性物質發(fā)生泄漏鳖擒。R本人自食惡果不足惜溉浙,卻給世界環(huán)境...
    茶點故事閱讀 41,036評論 3 326
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望蒋荚。 院中可真熱鬧戳稽,春花似錦、人聲如沸期升。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,676評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽播赁。三九已至颂郎,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間容为,已是汗流浹背乓序。 一陣腳步聲響...
    開封第一講書人閱讀 32,829評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留坎背,地道東北人替劈。 一個月前我還...
    沈念sama閱讀 47,743評論 2 368
  • 正文 我出身青樓,卻偏偏與公主長得像得滤,于是被迫代替她去往敵國和親陨献。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,629評論 2 354

推薦閱讀更多精彩內容