组合模式

组合模式

起男 1,066 2021-05-12

组合模式

需求

编写一个程序展示学校院系结构:要在一个页面中展示出学校的院系组成,一个学校有多少个学院,一个学院有多少个系

传统方式

//学校
public class University{}
//学院
public class College extends University{}
//系
public class Department extends College{}

问题

  • 将学院看作是学校的子类,系是学院的子类,这样实际上是站在组织大小来进行分层的
  • 实际上我们的要求是:在一个页面展示出学校的院系组成,一个学校有多个学院,一个学院有多个系,因此这种方案,不能很好的实现管理的操作,如果对学院、系的增删改遍历等

解决方案:把学校、院、系都看做是组织结构,他们之间没有继承的关系,而是一个树型结构,可以更好的实现管理操作->组合模式

组合模式

  • 组合模式,又叫部分整体模式,它创建了对象组的树型结构,将对象组合成树状结构以表示“整体-部分”的层次关系
  • 组合模式依据树形结构来组合对象,用来表示部分以及整体层次
  • 这种类型的设计模式属于结构型模式
  • 组合模式使得用户对单个对象和组合对象的访问具有一致性,即:组合能让客户以一致的方式处理个别对象以及组合对象

解决问题

  • 当我们要处理的对象可以生成一棵树型结构,而我们要对树上的节点和叶子节点进行操作时,它能够提供一致的方式,而不用考虑它是节点还是叶子
//组织抽象 可以是类、抽象类或接口
@Data
public abstract class OrganizationComponent{
    private String name; //名字
    
    public OrganizationComponent(String name){
        this.name = name;
    }
    
    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> organizationComponents = new ArrayList();
    
    //构造
    public University(String name){
        super(name);
    }
    
    @Override
    protected void add(OrganizationComponent organizationComponent){
        organizationComponents.add(organizationComponent);
    }
    
    @Override
    protected void remove(OrganizationComponent organizationComponent){
        organizationComponents.remove(organizationComponent);
    }
    
    @Override
    protected void print(){
        //输出学校名称
        System.out.println(getName());
        //输出学院名称
        for(OrganizationComponent organizationComponent : organizationComponents){
            organizationComponent.print();
        }
    }
}
//学院 非叶子节点
public class College extends OrganizationComponent{
    
    //学院列表
    List<OrganizationComponent> organizationComponents = new ArrayList();
    
    //构造
    public College(String name){
        super(name);
    }
    
    @Override
    protected void add(OrganizationComponent organizationComponent){
        organizationComponents.add(organizationComponent);
    }
    
    @Override
    protected void remove(OrganizationComponent organizationComponent){
        organizationComponents.remove(organizationComponent);
    }
    
    @Override
    protected void print(){
        //输出学院名称
        System.out.println(getName());
        //输出系名称
        for(OrganizationComponent organizationComponent : organizationComponents){
            organizationComponent.print();
        }
    }
}
//系 叶子节点
public class Department extends OrganizationComponent{
    
    //构造
    public Department(String name){
        super(name);
    }
    
    @Override
    protected void print(){
        //输出系名称
        System.out.println(getName());
    }
}
//使用
//学校
OrganizationComponent university = new University("清华大小");
//学院
OrganizationComponent college = new College("计算机学院");
//系
college.add(new Department("软件工程"));

university.add(college);
//输出
university.print();

总结

  • 组合模式简化客户端操作。客户端只需要面对一致的对象而不用考虑整体部分或者节点叶子的问题
  • 具有较强的扩展性。当我们要更改组合对象时,我们只需要调整内部的层次关系,客户端不用做出任何改动
  • 方便创建出复杂的层次结构。客户端不用理会组合里面的组成细节,通过添加节点或者叶子从而创建出复杂的树型结构
  • 需要遍历组织机构,或者处理的对象具有树形结构时,非常适合使用组合模式
  • 要求较高的抽象性,如果节点和叶子有很多差异性的话,比如很多方法和属性都不一样,不适合使用组合模式