08设计模式-结构型模式-组合模式

在这里插入图片描述

问题分析

1)将学院看作是学校的子类,系是学院的子类,这样实际上是站在组织大小来进行分层次的;
2)实际上我们的要求是:在一个页面中展示出学校的院系组成,一个学校有多个学院,一个学院有多个系,因此,这种方案不能很好实现的管理的操作,比如对学院、系的添加,删除,遍历等;
3)解决方案:把学校、院、系都看做是组织结构,他们之间没有继承的关系,而是一个树形结构,可以更好的实现管理操作。 => 组合模式

组合模式适用场景最关键的就是老大类要管理小弟类

基本介绍

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

在这里插入图片描述

注1:OrganizationComponent.java可以是抽象类/接口,也可以不是抽象类。
注2:在实际代码中,是将OrganizationComponent作为属性放到了College、University中,图中不好画出来,就画成了Department聚合到College,College聚合到University中(因为在聚合时是动态加入的)。

代码

// 抽象父类

package com.xusj.future.structural.Composite;

/**
 * @author xusj
 * <br>CreateDate 2022/7/27 0:16
 */
public abstract class OrganizationComponent {
    private String name;
    private String des;//说明

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

    //为什么不把add()方法做成抽象的?
    //答:叶子节点是不需要实现add()方法的,如果做成抽象方法的话,就会导致所有的叶子节点都必须实现add()
    protected void add(OrganizationComponent organizationComponent) {
        //默认实现
        throw new UnsupportedOperationException();
    }

    protected void remove(OrganizationComponent organizationComponent) {
        throw new UnsupportedOperationException();
    }

    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;
    }

    //print()方法:无论叶子节点还是非叶子节点都需要有,做成抽象的
    protected abstract void print();
}

// 老大类可以管小弟

package com.xusj.future.structural.Composite;

import java.util.ArrayList;
import java.util.List;

/**
 * @author xusj
 * <br>CreateDate 2022/7/27 0:17
 */
public class University extends OrganizationComponent {
    List<OrganizationComponent> organizationComponents = new ArrayList<OrganizationComponent>();

    //为什么必须实现构造器?(否则编译不通过)
    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);
    }

    //也可以重写getName()和getDes()
    @Override
    public String getName() {
        return super.getName();
    }

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

    //输出university中包含的学院
    @Override
    protected void print() {
        System.out.println("=============" + getName() + "===========");
        for (OrganizationComponent organizationComponent : organizationComponents) {
            organizationComponent.print();
        }
    }
}

// 老二类

package com.xusj.future.structural.Composite;

import java.util.ArrayList;
import java.util.List;

/**
 * @author xusj
 * <br>CreateDate 2022/7/27 0:18
 */
public class College extends OrganizationComponent {
    //College的List中存放的是department
    List<OrganizationComponent> organizationComponents = new ArrayList<OrganizationComponent>();

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

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

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

    //重写getName()和getDes()
    @Override
    public String getName() {
        return super.getName();
    }

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

    //输出university中包含的学院
    @Override
    protected void print() {
        System.out.println("=============" + getName() + "===========");
        for (OrganizationComponent organizationComponent : organizationComponents) {
            organizationComponent.print();
        }
    }
}

// 小弟类

package com.xusj.future.structural.Composite;

/**
 * @author xusj
 * <br>CreateDate 2022/7/27 0:18
 */
public class Department extends OrganizationComponent {
    public Department(String name, String des) {
        super(name, des);
    }

    //add和remove方法就不必写了,因为它是叶子节点,它就不再管理其他单位了

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

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

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

// 客户端调用

package com.xusj.future.structural.Composite;

/**
 * @author xusj
 * <br>CreateDate 2022/7/27 0:19
 */
public class Client {
    public static void main(String[] args) {
        //从大到小创建对象
        OrganizationComponent university = new University("清华大学", "中国顶级大学");
        //创建学院
        OrganizationComponent computeCollege = new College("计算机学院", "计算机");
        OrganizationComponent infoEngineCollege = new College("信息工程学院", "信息工程");
        //创建各个学院下的系(专业)
        computeCollege.add(new Department("软件工程", "实用"));
        computeCollege.add(new Department("网络工程", "不错"));
        computeCollege.add(new Department("计算机科学与技术", "老牌专业"));

        infoEngineCollege.add(new Department("通信工程", "不好学"));
        infoEngineCollege.add(new Department("信息工程", "好学"));
        //将学院加到学校
        university.add(computeCollege);
        university.add(infoEngineCollege);

        //university.print();
        //可以只打印某个学院
        computeCollege.print();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值