设计模式-组合模式

在一个页面中展示出学校的院系组成,一个学校有多个学院,一个学院有多个系


1.传统方案

将学院看做是学校的子类,系是学院的子类

  • 不能很好实现的管理的操作,比如对学院、系的添加,删除,遍历等

2.组合模式

2.1 介绍
  • 组合模式又叫部分整体模式,它创建了对象组的树形结构,将对象组合成树状结构以表示“整体-部分”的层次关系
  • 组合能让客户以一致的方式处理个别对象(叶子节点)以及组合对象(非叶子节点)
2.2 原理
  • Component:组合中对象声明接口/抽象类,实现所有类共有的接口默认行为,用于访问和管理Component的子部件
  • Leaf:在组合中表示叶子节点(即下图中的Department)
  • Composite:非叶子节点(即College和University),用于存储子部件,实现接口的相关操作

在这里插入图片描述

2.3 代码实现
// Component
public abstract class OrganizationComponent {
    private String name;  // 名字
    private String des;  // 说明

    protected void add(OrganizationComponent organizationComponent) {
        // 默认实现(如果作为抽象方法的话,叶子节点必须要实现,但实际不需要)
        throw new UnsupportedOperationException();
    }

    protected void remove(OrganizationComponent organizationComponent) {
        // 默认实现(如果作为抽象方法的话,叶子节点必须要实现,但实际不需要)
        throw new UnsupportedOperationException();
    }

    public OrganizationComponent(String name, String des) {
        super();
        this.name = name;
        this.des = des;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDes() {
        return des;
    }

    public void setDes(String des) {
        this.des = des;
    }

    // 子类都需要实现(叶子节点也需要实现,所以作为抽象方法)
    protected abstract void print();
}
// Composite, 可以管理College
public class University extends OrganizationComponent {
    // List中存放College
    List<OrganizationComponent> organizationComponents = new ArrayList<>();

    public University(String name, String des) {
        super(name, des);
    }

    // 重写add
    @Override
    protected void add(OrganizationComponent organizationComponent) {
        organizationComponents.add(organizationComponent);
    }

    // 重写remove
    @Override
    protected void remove(OrganizationComponent organizationComponent) {
        organizationComponents.remove(organizationComponent);
    }

    @Override
    public String getName() {
        return super.getName();
    }

    @Override
    public String getDes() {
        return super.getDes();
    }

    // print方法,即输出University包含的学院
    @Override
    protected void print() {
        System.out.println("--------------" + getName() + "--------------");
        // 遍历organizationComponents
        for (OrganizationComponent organizationComponent : organizationComponents) {
            organizationComponent.print();
        }
    }
}
// Composite, 可以管理Department
public class College extends OrganizationComponent {
    // List中存放Department
    List<OrganizationComponent> organizationComponents = new ArrayList<>();

    public College(String name, String des) {
        super(name, des);
    }

    // 重写add
    @Override
    protected void add(OrganizationComponent organizationComponent) {
        // 实际业务中College的add和University的add不一定完全一样
        organizationComponents.add(organizationComponent);
    }

    // 重写remove
    @Override
    protected void remove(OrganizationComponent organizationComponent) {
        organizationComponents.remove(organizationComponent);
    }

    @Override
    public String getName() {
        return super.getName();
    }

    @Override
    public String getDes() {
        return super.getDes();
    }

    // print方法,即输出University包含的学院
    @Override
    protected void print() {
        System.out.println("--------------" + getName() + "--------------");
        // 遍历organizationComponents
        for (OrganizationComponent organizationComponent : organizationComponents) {
            organizationComponent.print();
        }
    }
}
// Leaf
public class Department extends OrganizationComponent {
    public Department(String name, String des) {
        super(name, des);
    }

    @Override
    public String getName() {
        return super.getName();
    }

    @Override
    public String getDes() {
        return super.getDes();
    }

    // 不需要重写add和remove
    @Override
    protected void print() {
        System.out.println(getName());
    }
}
public class Client {
    public static void main(String[] args) {
        // 从大到小创建对象
        // 创建学校
        OrganizationComponent university = new University("清华大学", " 中国顶级大学");

        // 创建学院
        OrganizationComponent computerCollege = new College("计算机学院", " 计算机学院");
        OrganizationComponent infoEngineercollege = new College("信息工程学院", " 信息工程学院");

        // 创建各个学院下面的系(专业)
        computerCollege.add(new Department("软件工程", " 软件工程不错"));
        computerCollege.add(new Department("网络工程", " 网络工程不错"));
        computerCollege.add(new Department("计算机科学与技术", " 计算机科学与技术是老牌的专业"));

        infoEngineercollege.add(new Department("通信工程", " 通信工程不好学"));
        infoEngineercollege.add(new Department("信息工程", " 信息工程好学"));

        // 将学院加入到学校
        university.add(computerCollege);
        university.add(infoEngineercollege);

        university.print();
        // infoEngineercollege.print();
    }
}

3.注意事项和细节

  • 简化客户端操作:客户端面对的一致的对象(不管是叶子节点还是非叶子节点)
  • 具有较强的扩展性,方便创建出复杂的层次结构
  • 需要遍历组织机构或处理的对象具有树形结构时, 非常适合使用组合模式
  • 如果非叶子节点和叶子节点差异性较大(如很多方法和属性都不一样),则不适合使用组合模式

参考

https://www.bilibili.com/video/BV1G4411c7N4?p=77-80


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值