java设计模式之组合模式

本文介绍了组合模式(CompositePattern),一种结构型设计模式,用于创建对象的树形结构表示层次关系。通过大学和学院的实例展示了如何使用组合模式,并强调了其实用性、扩展性和注意事项,如简化客户端操作和抽象性要求高。
摘要由CSDN通过智能技术生成

组合模式(Composite Pattern)

基本介绍

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

原理类图

原理类图

说明:

  1. Component:这时组合中对象声明接口,在适当情况下,实现所有类共有的接口默认行为,用于访问和管理Component子部件,Component可以是抽象类或者接口。
  2. Leaf:在组合中表示叶子节点,叶子节点没有子节点。
  3. Composite: 非叶子节点,用于存储子部件,在Component接口中实现,子部件的相关操作,比如增加(add) 删除。

应用实例

要求:展示一个学校院系结构,一个学校有多少个学院,一个学院有多少个系。

类图分析:
类图分析
代码如下:

public abstract class OrganizationComponent {
    //名称
    private String name;
    //描述
    private String des;

    public OrganizationComponent(String name,String des){
        this.name = name;
        this.des = des;
    }
    //打印
    protected abstract void print();

    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 void add(OrganizationComponent organizationComponent){

    }

    protected void remove(OrganizationComponent organizationComponent){

    }
}

public class University extends OrganizationComponent {
    public University(String name, String des) {
        super(name, des);
    }
    List<OrganizationComponent> organizationComponents = new ArrayList<>();

    @Override
    protected void print() {
        for (OrganizationComponent item :organizationComponents){
            System.out.println(item.getName());
            item.print();
        }
    }

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

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

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

    @Override
    public String getDes() {
        return super.getDes();
    }
}
public class College extends OrganizationComponent{
    public College(String name, String des) {
        super(name, des);
    }
    List<OrganizationComponent> organizationComponents = new ArrayList<>();
    @Override
    protected void print() {
        for (OrganizationComponent item: organizationComponents
             ) {
//            System.out.println(item.getName());
            item.print();
        }
    }

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

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

    @Override
    protected void add(OrganizationComponent organizationComponent) {
        this.organizationComponents.add(organizationComponent);
    }

    @Override
    protected void remove(OrganizationComponent organizationComponent) {
        this.organizationComponents.remove(organizationComponent);
    }
}
public class Department extends OrganizationComponent{
    public Department(String name, String des) {
        super(name, des);
    }

    @Override
    protected void print() {
        System.out.println(getName());
    }

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

    @Override
    public String getDes() {
        return super.getDes();
    }
}
    public static void main(String[] args) {

        OrganizationComponent university = new University("清华大学","顶级大学");

        OrganizationComponent computerCollege = new College("计算器学院","计算器学院");
        OrganizationComponent infoCollege = new College("信息工程学院","信息工程学院");

        computerCollege.add(new Department("软件工程","软件工程不错"));
        computerCollege.add(new Department("网络工程","网络工程不错"));

        infoCollege.add(new Department("通信工程","通讯专业不好"));

        university.add(computerCollege);
        university.add(infoCollege);

        infoCollege.print();
        computerCollege.print();
        university.print();
    }

组合模式的注意事项

  1. 简化客户端操作。客户端只需要面对一致的对象而不用考虑整体部分或者叶子的问题。
  2. 具有较强的扩展性。当我们要更改组合对象时,我们只需要调整内部的层次关系,客户端不需要做改动。
  3. 方便创建出复杂的层次结构。客户端不用理会组合里面的组成细节,容易添加节点或者叶子从而创建出复杂的树形结构。
  4. 需要遍历组织结构,或者处理的对象具有树形结构时,非常适合组合模式。
  5. 要求较高的抽象性,如果节点和叶子有很多差异性的话,比如很多方法和属性都不一样,不适合组合模式。
  • 8
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值