尚硅谷设计模式学习(10)---[组合模式(Composite Pattern)]

🚀🚀🚀尚硅谷传送门==>B站尚硅谷Java设计模式

❤❤❤感谢尚硅谷❤❤❤

🕐🕑🕒最近开始计划学习一下设计模式了,加油!!!



📢情景引入

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

传统思路分析

不能很好实现的管理的操作

在这里插入图片描述

🌈组合模式

把学校、院、系都看做是组织结构,他们之间没有继承的关系,而是一个树形结构,可以更好的实现管理操作。

关于组合模式::又称为部分与整体模式,将对象组合成树状结构以表示“整体-部分”的层次关系.
组合模式使得客户端对单个对象和组合对象的访问具有一致性,(组合能让客户端以一致的方式处理个别对象以及组合对象)

Component :组合中对象声明(接口/抽象类),可实现所有类的公用接口默认方法,用于访问和管理Component 子部件
Leaf : 表示叶子节点,作为被管理者
Composite : 非叶子节点,存储子部件

在这里插入图片描述

注意使用组合模式需要确保有相似的方法及属性的几个对象;比如这里的学校,学院它就有相似的添加,删除方法; 学校,学院,系 有相似的输出方法.

具有较强的扩展性。如果更改组合对象时,只需要调整内部的层次关系,客户端不用做出任何改动.

客户端只需要面对一致的对象而不用考虑整体部分或者节点叶子的问题


接着回到案例

在这里插入图片描述

作为组织管理 的抽象类OrganizationComponent

//组织管理
public abstract class OrganizationComponent {
    private String name;
    //描述;
    private String describe;

    //初始化;
    public OrganizationComponent(String name, String describe) {
        this.name = name;
        this.describe = describe;
    }

    public String getName() {
        return name;
    }

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

    public String getDescribe() {
        return describe;
    }

    public void setDescribe(String describe) {
        this.describe = describe;
    }

    //由于叶子节点可能不需要某些方法,所以就没用抽象方法;
    //添加方法;
    protected void addMethod(OrganizationComponent organizationComponent){
        //抛出不支持操作的异常;
        throw new UnsupportedOperationException();
    }

    //删除方法;
    protected void removeMethod(OrganizationComponent organizationComponent){
        throw new UnsupportedOperationException();
    }

    //输出打印方法; 大学,学院,院系都需要的;作为抽象方法;
    protected abstract void print();
}

大学University

//大学  ; 作为 Composite 管理College(学院)
public class University extends OrganizationComponent{

    //这里存放的是学院;
    List<OrganizationComponent> ogc=new ArrayList<OrganizationComponent>();

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

    //添加方法;
    @Override
    protected void addMethod(OrganizationComponent organizationComponent) {
        ogc.add(organizationComponent);
    }

    //删除方法;
    @Override
    protected void removeMethod(OrganizationComponent organizationComponent) {
        ogc.remove(organizationComponent);
    }

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

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

    //输出方法;
    @Override
    protected void print() {
        System.out.println("<<===大学====>>"+getName());
        for (OrganizationComponent organizationComponent : ogc) {
            organizationComponent.print();
        }
    }
}

学院College

//学院  管理 ==>系
public class College extends OrganizationComponent{

    //这里存放的是Department (院系)
    List<OrganizationComponent> ogc=new ArrayList<OrganizationComponent>();

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

    //添加方法;
    @Override
    protected void addMethod(OrganizationComponent organizationComponent) {
        ogc.add(organizationComponent);
    }

    //删除方法;
    @Override
    protected void removeMethod(OrganizationComponent organizationComponent) {
        ogc.remove(organizationComponent);
    }


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

    @Override
    public String getDescribe() {
        return super.getDescribe();
    }
    //输出方法;
    @Override
    protected void print() {
        System.out.println("!!!!!!!!!学院====>"+getName());
        for (OrganizationComponent organizationComponent : ogc) {
            organizationComponent.print();
        }
    }
}

Department

//院系
public class Department extends OrganizationComponent{

    public Department(String name, String describe) {
        super(name, describe);
    }

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

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

    @Override
    protected void print() {
        System.out.println("院系====>"+getName());
    }
}

模拟客户端Client

public class Client {
    public static void main(String[] args) {
        //大学
        OrganizationComponent university = new University("霍格沃兹","著名魔法学院");

        //学院;
        OrganizationComponent college1 = new College("格兰芬多","英勇无畏");
        OrganizationComponent college2 = new College("拉文克劳","智慧聪敏");

        //添加院系;
        college1.addMethod(new Department("计算机专业","计科"));
        college1.addMethod(new Department("网络工程","网络"));

        college2.addMethod(new Department("物理专业","物理"));
        college2.addMethod(new Department("通信专业","通信"));

        //添加学院;
        university.addMethod(college1);
        university.addMethod(college2);

        university.print();

    }
}

执行输出

<<===大学====>>霍格沃兹
!!!!!!!!!学院====>格兰芬多
院系====>计算机专业
院系====>网络工程
!!!!!!!!!学院====>拉文克劳
院系====>物理专业
院系====>通信专业

组合模式在HashMap中的应用

比如说这样一个案例

public class Demo {
    public static void main(String[] args) {

        Map<Integer,String> hashMap1 =new HashMap<Integer, String>();

        //这里直接存放叶子节点;
        hashMap1.put(0,"阿猫");

        Map<Integer,String> hashMap2 =new HashMap<Integer, String>();
        hashMap2.put(1,"阿喵");
        hashMap2.put(2,"阿华");
        //将第二个加到第一个map中去;
        hashMap1.putAll(hashMap2);

        System.out.println(hashMap1);
        //{0=阿猫, 1=阿喵, 2=阿华}
    }
}

进入Map接口看看;

其中的put添加方法仅为抽象方法.

在这里插入图片描述

注意到Map接口的实现类AbstractMap是个抽象类; 类似于组合模式的Component(抽象构建)

在这里插入图片描述

其中重写 put 方法时,默认实现,抛出不支持操作的异常对象.

在这里插入图片描述

AbstractMap的子类HashMap ; 类似于组合模式的Composite(中间构建)

在这里插入图片描述

实现了 put方法

在这里插入图片描述

继续;

在这里插入图片描述

存入Node节点中 ; 类似于组合模式的叶子节点 Leaf

在这里插入图片描述


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Java设计模式是一组经过实践验证的面向对象设计原则和模式,可以帮助开发人员解决常见的软件设计问题。下面是常见的23种设计模式: 1. 创建型模式(Creational Patterns): - 工厂方法模式(Factory Method Pattern) - 抽象工厂模式(Abstract Factory Pattern) - 单例模式(Singleton Pattern) - 原型模式(Prototype Pattern) - 建造者模式(Builder Pattern) 2. 结构型模式(Structural Patterns): - 适配器模式(Adapter Pattern) - 桥接模式(Bridge Pattern) - 组合模式Composite Pattern) - 装饰器模式(Decorator Pattern) - 外观模式(Facade Pattern) - 享元模式(Flyweight Pattern) - 代理模式(Proxy Pattern) 3. 行为型模式(Behavioral Patterns): - 责任链模式(Chain of Responsibility Pattern) - 命令模式(Command Pattern) - 解释器模式(Interpreter Pattern) - 迭代器模式(Iterator Pattern) - 中介者模式(Mediator Pattern) - 备忘录模式(Memento Pattern) - 观察者模式(Observer Pattern) - 状态模式(State Pattern) - 策略模式(Strategy Pattern) - 模板方法模式(Template Method Pattern) - 访问者模式(Visitor Pattern) 4. 并发型模式(Concurrency Patterns): - 保护性暂停模式(Guarded Suspension Pattern) - 生产者-消费者模式(Producer-Consumer Pattern) - 读写锁模式(Read-Write Lock Pattern) - 信号量模式(Semaphore Pattern) - 线程池模式(Thread Pool Pattern) 这些设计模式可以根据问题的特点和需求来选择使用,它们提供了一些可复用的解决方案,有助于开发高质量、可维护且易于扩展的软件系统。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小智RE0

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值