基本介绍:
1、组合模式,又叫部分整体模式,它创建了对象组的树形结构,将对象组合成树状结构以表示“整体-部分”的层次关系
2、组合模式依据树形结构来组合对象,用来表示部分以及整体层次
3、这种类型的设计模式属于结构型模式
4、组合模式使得用户对单个对象和组合对象的访问具有一致性,即组合能让客户以一致的方式处理个别对象以及组合对象
组合模式类图:
角色说明:
1、Component:组合中对象声明接口,在适当情况下,实现所有类共有的接口默认行为,用于访问和管理Component子部件,Component可以是抽象类或接口
2、Leaf:在组合中表示叶子节点,叶子节点没有叶子节点
3、Composite:非叶子节点,用于存储子部件,在Component接口中实现子部件的相关操作,比如增加(add)、删除等。
组合模式解决这样的问题,当我们的要处理的对象可以生成一颗树形结构,而我们要对树上的节点和叶子进行操作时,它能够提供一致的方式,而不用考虑它是节点还是叶子。
代码示例:
/*** 抽象类*/@Datapublic abstract class OrganizationComponent {private String name;private String des;public OrganizationComponent(String name, String des){super();this.name = name;this.des = des;}protected void add(OrganizationComponent organizationComponent){//默认实现throw new UnsupportedOperationException();}protected void remove(OrganizationComponent organizationComponent){//默认实现throw new UnsupportedOperationException();}protected abstract void print();}/*** 实现类大学*/public class University extends OrganizationComponent {List<OrganizationComponent> organizationComponentList = new ArrayList<>();public University(String name, String des) {super(name, des);}@Overrideprotected void add(OrganizationComponent organizationComponent) {organizationComponentList.add(organizationComponent);}@Overrideprotected void remove(OrganizationComponent organizationComponent) {organizationComponentList.remove(organizationComponent);}@Overrideprotected void print() {System.out.println("<"+ getName() +">");for (OrganizationComponent organizationComponent : organizationComponentList) {organizationComponent.print();}}}/*** 实现类学院*/public class College extends OrganizationComponent {List<OrganizationComponent> organizationComponentList = new ArrayList<>();public College(String name, String des) {super(name, des);}@Overrideprotected void add(OrganizationComponent organizationComponent) {organizationComponentList.add(organizationComponent);}@Overrideprotected void remove(OrganizationComponent organizationComponent) {organizationComponentList.remove(organizationComponent);}@Overrideprotected void print() {System.out.println("--"+ getName() +"--");for (OrganizationComponent organizationComponent : organizationComponentList) {organizationComponent.print();}}}/*** 实现类专业*/public class Department extends OrganizationComponent {List<OrganizationComponent> organizationComponentList = new ArrayList<>();public Department(String name, String des) {super(name, des);}@Overrideprotected void print() {System.out.println(getName());}}/*** 调用*/public class Client {public static void main(String[] args) {OrganizationComponent university = new University("清华大学", "国内一流大学");OrganizationComponent computerCollege = new College("计算机学院", "计算机学院不错");OrganizationComponent informationCollege = new College("信息学院", "信息学院不错");computerCollege.add(new Department("计算机1专业","计算机1专业不错"));computerCollege.add(new Department("计算机2专业","计算机2专业不错"));informationCollege.add(new Department("信息1专业","信息1专业不错"));informationCollege.add(new Department("信息2专业","信息2专业不错"));university.add(computerCollege);university.add(informationCollege);university.print();}}
打印信息如下:
<清华大学>--计算机学院--计算机1专业计算机2专业--信息学院--信息1专业信息2专业
组合模式的注意事项:
- 简化客户端操作。客户端只需要面对一致的对象而不用考虑整体部分或者节点叶子的问题
- 具有较强的扩展性。当要更改组合对象时,只需要调整内部的层次关系,客户端不用做出任何改动
- 方便创建出复杂的层次结构。客户端不用理会组合里面的组成细节,容易添加节点或者叶子从而创建出复杂的树形结构
- 需要遍历组织机构或者处理的对象具有树形结构时,非常适合使用组合模式
- 要求较高的抽象性,如果节点和叶子有很多差异性的话,比如很多方法和属性都不一样,不适合使用组合模式
