【设计模式In Java】七、组合模式

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/CL_YD/article/details/88068793

组合模式

定义

组合模式(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组件、层级菜单、目录结构等,特别是那些容器和元件可能会不断扩展的系统,如果想忽略容器和元件的差异,达到统一处理的目的,组合模式是一个很好的选择。

猜你喜欢

转载自blog.csdn.net/CL_YD/article/details/88068793