【設計模式In Java】七、組合模式

組合模式

定義

組合模式(Composite Pattern):組合多個對象形成樹形結構以表示具有“整體—部分”關係的層次結構。組合模式對單個對象(即葉子對象)和組合對象(即容器對象)的使用具有一致性,組合模式又可以稱爲“整體—部分”(Part-Whole)模式,它是一種對象結構型模式。

組合模式的關鍵是定義了一個抽象構件類,它既可以代表葉子,又可以代表容器,而客戶端針對該抽象構件類進行編程,無須知道它到底表示的是葉子還是容器,可以對其進行統一處理。同時容器對象與抽象構件類之間還建立一個聚合關聯關係,在容器對象中既可以包含葉子,也可以包含容器,以此實現遞歸組合,形成一個樹形結構。

場景

現在需要開發一個會議系統,當有部門預約會議時,需要給部門下的所有成員發送提醒郵件,但值得注意的是:部門下級可能還是部門,也可能是部門成員;以部門爲單位預約會議時,要保證每個部門的成員和子部門的成員都要收到郵件提醒。

UML類圖

在這裏插入圖片描述

代碼

composite

示例:

public class TestComposite {

    @Test
    public void test() {

        Department it = new Department("IT");
        Department rd = new Department("Research and Development");
        Department om = new Department("Operation and Maintenance");
        Department ar = new Department("Architecture");

        it.addAll(Arrays.asList(
                new Employee("Ashe", "[email protected]"),
                new Employee("Bob", "[email protected]"),
                new Employee("Candy", "[email protected]"),
                new Employee("David", "[email protected]")
        ));

        rd.addAll(Arrays.asList(
                new Employee("Eric", "[email protected]"),
                new Employee("Fizz", "[email protected]"),
                new Employee("Galio", "[email protected]"),
                new Employee("Harris", "[email protected]")
        ));

        om.addAll(Arrays.asList(
                new Employee("Iran", "[email protected]"),
                new Employee("John", "[email protected]"),
                new Employee("Kat", "[email protected]"),
                new Employee("Lucian", "[email protected]")
        ));

        ar.addAll(Arrays.asList(
                new Employee("Mondor", "[email protected]"),
                new Employee("Nox", "[email protected]"),
                new Employee("Olaf", "[email protected]"),
                new Employee("Puff", "[email protected]")
        ));

        it.addAll(Arrays.asList(rd, om, ar));

        it.sendMail();

    }
    /*
    Send mail to department IT -------------------
    Send mail to Ashe([email protected])...
    Send mail to Bob([email protected])...
    Send mail to Candy([email protected])...
    Send mail to David([email protected])...

    Send mail to department Research and Development -------------------
    Send mail to Eric([email protected])...
    Send mail to Fizz([email protected])...
    Send mail to Galio([email protected])...
    Send mail to Harris([email protected])...

    Send mail to department Operation and Maintenance -------------------
    Send mail to Iran([email protected])...
    Send mail to John([email protected])...
    Send mail to Kat([email protected])...
    Send mail to Lucian([email protected])...

    Send mail to department Architecture -------------------
    Send mail to Mondor([email protected])...
    Send mail to Nox([email protected])...
    Send mail to Olaf([email protected])...
    Send mail to Puff([email protected])...
     */
}

總結

組合模式雖然讓“容器”和“元件”分離開來,但是對它們的操作卻是非常簡單——客戶端可以一致地使用一個容器或其中單個元件,不必關心處理的是單個元件還是整個容器,簡化了客戶端代碼。同時需要添加一種容器或元件的時候,不需要修改原有代碼,遵循開閉原則。

從定義就可以看出:組合模式適用於可以用樹形結構表示數據結構的業務場景,比如UI組件、層級菜單、目錄結構等,特別是那些容器和元件可能會不斷擴展的系統,如果想忽略容器和元件的差異,達到統一處理的目的,組合模式是一個很好的選擇。

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