当前位置: 首页 > news >正文

设计模式——组合模式(Composite Pattern)+ Spring相关源码

文章目录

  • 一、组合模式定义
  • 二、例子
    • 2.1 菜鸟教程例子
      • 2.1.1 创建 Employee 类,该类带有 Employee 对象的列表。
      • 2.1.2 使用 Employee 类来创建和打印员工的层次结构。
    • 2.2 JDK源码——java.awt.Container
    • 2.3 Spring源码——CompositeCacheManager
  • 三、其他设计模式

一、组合模式定义

类型: 结构型模式
介绍: 一种树形结构。它又叫部分整体模式,是用于把一组相似的对象当作一个单一的对象。
目的: 组合模式依据树形结构来组合对象,用来表示部分以及整体层次。

二、例子

2.1 菜鸟教程例子

2.1.1 创建 Employee 类,该类带有 Employee 对象的列表。

public class Employee {private String name;private String dept;private int salary;private List<Employee> subordinates;//构造函数public Employee(String name,String dept, int sal) {this.name = name;this.dept = dept;this.salary = sal;subordinates = new ArrayList<Employee>();}public void add(Employee e) {subordinates.add(e);}public void remove(Employee e) {subordinates.remove(e);}public List<Employee> getSubordinates(){return subordinates;}public String toString(){return ("Employee :[ Name : "+ name +", dept : "+ dept + ", salary :"+ salary+" ]");}   
}

2.1.2 使用 Employee 类来创建和打印员工的层次结构。

public class CompositePatternDemo {public static void main(String[] args) {Employee CEO = new Employee("John","CEO", 30000);Employee headSales = new Employee("Robert","Head Sales", 20000);Employee headMarketing = new Employee("Michel","Head Marketing", 20000);Employee clerk1 = new Employee("Laura","Marketing", 10000);Employee clerk2 = new Employee("Bob","Marketing", 10000);Employee salesExecutive1 = new Employee("Richard","Sales", 10000);Employee salesExecutive2 = new Employee("Rob","Sales", 10000);CEO.add(headSales);CEO.add(headMarketing);headSales.add(salesExecutive1);headSales.add(salesExecutive2);headMarketing.add(clerk1);headMarketing.add(clerk2);//打印该组织的所有员工System.out.println(CEO); for (Employee headEmployee : CEO.getSubordinates()) {System.out.println(headEmployee);for (Employee employee : headEmployee.getSubordinates()) {System.out.println(employee);}}        }
}

2.2 JDK源码——java.awt.Container

public class Container extends Component {private java.util.List<Component> component = new ArrayList<>();public Component getComponent(int n) {try {return component.get(n);} catch (IndexOutOfBoundsException z) {throw new ArrayIndexOutOfBoundsException("No such child: " + n);}}public Component add(Component comp) {addImpl(comp, null, -1);return comp;}}

2.3 Spring源码——CompositeCacheManager

public class CompositeCacheManager implements CacheManager, InitializingBean {private final List<CacheManager> cacheManagers = new ArrayList();@Nullablepublic Cache getCache(String name) {Iterator var2 = this.cacheManagers.iterator();Cache cache;do {if (!var2.hasNext()) {return null;}CacheManager cacheManager = (CacheManager)var2.next();cache = cacheManager.getCache(name);} while(cache == null);return cache;}
}

三、其他设计模式

创建型模式
结构型模式

  • 1、设计模式——装饰器模式(Decorator Pattern)+ Spring相关源码

行为型模式

  • 1、设计模式——访问者模式(Visitor Pattern)+ Spring相关源码
  • 2、设计模式——中介者模式(Mediator Pattern)+ JDK相关源码
  • 3、设计模式——策略模式(Strategy Pattern)+ Spring相关源码
  • 4、设计模式——状态模式(State Pattern)
  • 5、设计模式——命令模式(Command Pattern)+ Spring相关源码
  • 6、设计模式——观察者模式(Observer Pattern)+ Spring相关源码
  • 7、设计模式——备忘录模式(Memento Pattern)
  • 8、设计模式——模板方法模式(Template Pattern)+ Spring相关源码
  • 9、设计模式——迭代器模式(Iterator Pattern)+ Spring相关源码
  • 10、设计模式——责任链模式(Chain of Responsibility Pattern)+ Spring相关源码
  • 11、设计模式——解释器模式(Interpreter Pattern)+ Spring相关源码
http://www.lryc.cn/news/224243.html

相关文章:

  • 大语言模型-LLM简介
  • 创建多层级行索引,创建多层级行索引的DataFrameMultiIndex.from_product()
  • 用尽可能简单易懂的代码做个时间轴(时间线)
  • STM32笔记—定时器
  • 【力扣:1504】统计全1子矩阵
  • 排序算法之-选择
  • 机器学习模板代码(期末考试复习)自用存档
  • 使用sizeof()和strlen()去计算【数组】和【指针】的大小
  • viple进阶4:打印空心三角形
  • Oauth2.0的内容
  • npm 下载包失败解决方案
  • C语言---插入排序、希尔排序、冒泡排序、选择排序、快速排序简单介绍
  • 撸视频号收益这个副业靠谱吗?
  • 2、数组、Map+HashMap、Set+Hashset、Char和Character类、String类和Char类、Math类
  • ESP8266 WiFi模块快速入门指南
  • 微信小程序将后端返回的图片文件流解析显示到页面
  • 网络基础(1)
  • flink的AggregateFunction,merge方法作用范围
  • Day25力扣打卡
  • SpringCloud - OpenFeign 参数传递和响应处理(全网最详细)
  • Postgresql数据类型-布尔类型
  • SPASS-交叉表分析
  • 用Python的requests库来模拟爬取地图商铺信息
  • 使用EvoMap/Three.js模拟无人机灯光秀
  • 11.9存储器实验总结(单ram,双ram,FIFO)
  • linux(ubuntu)安装并使用scrcpy
  • linux rsyslog安装配置
  • 美国Embarcadero公司正式发布2023 RAD Studio Delphi C++ Builder 12 Athens
  • 树莓派4B的测试记录(CPU、FFMPEG)
  • 物联网AI MicroPython学习之语法 二进制与ASCII转换