Java大数据平台开发 学习笔记(39)—— java设计模式(组合模式)知识汇总

一、前言:

组合模式的注意事项与细节:

  1. 简化了客户端的操作,客户端只需面对对象,而不需考虑部分与整体问题。
  2. 具有较强的扩展性,需要调整时,只需更改组合内部结构的层次关系(树结构),客户端无需更改。
  3. 很高的抽象性,如果节点与叶子差异性过大时,不推荐使用组合模式。

二、UML图:

在这里插入图片描述


三、组合模式:

3.1、代码实例:

Step 1) 创建 OrganizationComponent 抽象类:

public abstract class OrganizationComponent {
    private String mane;
    private String des;

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

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

    public OrganizationComponent() {
    }

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

    public String getMane() {
        return mane;
    }

    public void setMane(String mane) {
        this.mane = mane;
    }

    public String getDes() {
        return des;
    }

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

    protected abstract void print();
}

Step 2) 创建 University 实现类:

public class University extends OrganizationComponent{

    List<OrganizationComponent> organizationComponents = new ArrayList<>();

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

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

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

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

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

    @Override
    protected void print() {
        System.out.println("-------------"+ getMane()+"------------------------");
        for (OrganizationComponent o : organizationComponents){
            o.print();
        }
    }
}

Step 3) 创建 College 实现类:

public class College extends OrganizationComponent{

    List<OrganizationComponent> organizationComponents = new ArrayList<>();

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

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

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

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

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

    @Override
    protected void print() {
        System.out.println("-------------"+ getMane()+"------------------------");
        for (OrganizationComponent o : organizationComponents){
            o.print();
        }
    }
}

Step 4) 创建 Department 实现类:

public class Department extends OrganizationComponent{

    public Department() {
    }

    public Department(String mane, String des) {
        super(mane, des);
    }

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

Step 5) 创建 main 方法:

public class Client {
    public static void main(String[] args) {
        OrganizationComponent university = new University("清华大学", "中国顶级大学");

        OrganizationComponent compuyerCollege = new University("计算机学院", "计算机学院");
        OrganizationComponent infoEnginerCollege = new University("信息工程学院", "信息工程学院   ");

        compuyerCollege.add(new Department("软件工程", "软件工程不错"));
        compuyerCollege.add(new Department("网络工程", "网络工程不错"));
        compuyerCollege.add(new Department("计算机科学与技术", "计算机科学与技术是老牌专业"));

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

        university.add(compuyerCollege);
        university.add(infoEnginerCollege);

//        university.print();
//        compuyerCollege.print();
        infoEnginerCollege.print();
    }
}


• 由 ChiKong_Tam 写于 2020 年 10 月 18 日

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值