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

设计模式(15)组合模式

一、介绍:

1、定义:组合多个对象形成树形结构以表示“整体-部分”的关系的层次结构。组合模式对叶子节点和容器节点的处理具有一致性,又称为整体-部分模式。

2、优缺点:

优点:

(1)高层模块调用简单:组合模式使得客户端代码可以一致地处理单个对象和组合对象,无须关心自己处理的是单个对象,还是组合对象,这简化了客户端代码。

(2)节点自由增加:更容易在组合体内加入新的对象,客户端不会因为加入了新的对象而更改源代码。

缺点:

(1)在使用组合模式时,其叶子和树枝的声明都是实现类,而不是接口,违反了依赖倒置原则。

(2)设计较复杂,客户端需要花更多时间理清类之间的层次关系。

(3)不容易限制容器中的构件。

3、组成:

(1)抽象构件(Component)角色:它的主要作用是为树叶构件和树枝构件声明公共接口,并实现它们的默认行为。在透明式的组合模式中抽象构件还声明访问和管理子类的接口;在安全式的组合模式中不声明访问和管理子类的接口,管理工作由树枝构件完成。(总的抽象类或接口,定义一些通用的方法,比如新增、删除)。

(2)树枝构件(Composite)角色 / 中间构件:是组合中的分支节点对象,它有子节点,用于继承和实现抽象构件。它的主要作用是存储和管理子部件,通常包含 Add()、Remove()、GetChild() 等方法。

(3)树叶构件(Leaf)角色:是组合中的叶节点对象,它没有子节点,用于继承或实现抽象构件。

// 定义抽象构件
public abstract class Component {protected String name;public Component(String name) {this.name = name;}public abstract void add(Component component);public abstract void remove(Component component);public abstract void display();
}// 定义叶子构件
public class Leaf extends Component {public Leaf(String name) {super(name);}@Overridepublic void add(Component component) {System.out.println("Cannot add to a leaf");}@Overridepublic void remove(Component component) {System.out.println("Cannot remove from a leaf");}@Overridepublic void display() {System.out.println("Leaf: " + name);}
}// 定义容器构件
public class Composite extends Component {private List<Component> children = new ArrayList<>();public Composite(String name) {super(name);}@Overridepublic void add(Component component) {children.add(component);}@Overridepublic void remove(Component component) {children.remove(component);}@Overridepublic void display() {System.out.println("Composite: " + name);for (Component component : children) {component.display();}}
}// 客户端代码
public class Client {public static void main(String[] args) {Component root = new Composite("root");Component leaf1 = new Leaf("leaf1");Component leaf2 = new Leaf("leaf2");Component composite1 = new Composite("composite1");Component leaf3 = new Leaf("leaf3");Component composite2 = new Composite("composite2");root.add(leaf1);root.add(leaf2);root.add(composite1);composite1.add(leaf3);composite1.add(composite2);root.display();}
}

4、应用场景:部分、整体场景,如树形菜单,文件、文件夹的管理。

二、demo:

1、菜单:

(1)数据库model

public class MenuDTO {private String menuName;private String menuCode;private String parentMenuCode;public MenuDTO(String menuName,String menuCode,String parentMenuCode){this.menuCode = menuCode;this.menuName = menuName;this.parentMenuCode = parentMenuCode;}/**省略所有set、get芳芳*/
}

 抽象构件Component

public abstract class MenuComponent extends MenuDTO {MenuComponent(String menuName, String menuCode,String parentMenuCode) {super(menuName, menuCode,parentMenuCode);}void addMenu(MenuComponent component){}void removeMenu(MenuComponent component){}
}

(2)树枝构件(Composite): 

public class MenuVO extends MenuComponent {private List<MenuComponent> children = new ArrayList<>();MenuVO(String menuName, String menuCode,String parentMenuCode) {super(menuName, menuCode,parentMenuCode);}@Overridevoid addMenu(MenuComponent component) {children.add(component);}@Overridevoid removeMenu(MenuComponent component) {}
}

(3)树叶

public class MenuLeaf extends MenuComponent {MenuLeaf(String menuName, String menuCode,String parentMenuCode) {super(menuName, menuCode,parentMenuCode);}@Overridevoid addMenu(MenuComponent component) {super.addMenu(component);}@Overridevoid removeMenu(MenuComponent component) {super.removeMenu(component);}
}

 客户端:

public class Test {public static void main(String args[]) {MenuComponent menuVOS = listMenus();System.out.println(menuVOS);}public static MenuComponent listMenus(){//模拟数据库查询,查询所有一级菜单(menu_type = 1)、二级菜单(menu_type = 2)List<MenuDTO> firstMenus = new ArrayList<>();MenuDTO menuDTO = new MenuDTO("菜单1","cd1","root");firstMenus.add(menuDTO);menuDTO = new MenuDTO("菜单2","cd2","root");firstMenus.add(menuDTO);menuDTO = new MenuDTO("菜单3","cd3","root");firstMenus.add(menuDTO);List<MenuDTO> secondMenus = new ArrayList<>();menuDTO = new MenuDTO("菜单1-1","cd1-1","cd1");secondMenus.add(menuDTO);menuDTO = new MenuDTO("菜单1-2","cd1-2","cd1");secondMenus.add(menuDTO);menuDTO = new MenuDTO("菜单2-1","cd2-1","cd2");secondMenus.add(menuDTO);Map<String, List<MenuDTO>> childMenuMap = secondMenus.stream().collect(Collectors.groupingBy(MenuDTO::getParentMenuCode));/**实现* 根节点* 菜单1  菜单2 菜单3*菜单1-1 菜单1-2 菜单2-1* *///1、定义根节点MenuComponent root = new MenuVO("根节点","root",null);//2、处理菜单层级for(MenuDTO  firstMenu : firstMenus){//二级菜单MenuComponent firstMenuVO = new MenuVO(firstMenu.getMenuName(),firstMenu.getMenuCode(),firstMenu.getParentMenuCode());//三级菜单List<MenuDTO> secondMenuVOs = childMenuMap.get(firstMenu.getMenuCode());if(!CollectionUtils.isEmpty(secondMenuVOs)){for(MenuDTO secondMenu : secondMenuVOs){MenuComponent secondMenuVO = new MenuVO(secondMenu.getMenuName(),secondMenu.getMenuCode(),secondMenu.getParentMenuCode());firstMenuVO.addMenu(secondMenuVO);}}root.addMenu(firstMenuVO);}return root;}
}

运行main方法 

2、文件夹:

(1)抽象构件Component

public abstract class FileComponent {//文件名称protected String name;//文件的层级 1 一级目录 2 二级目录 ...protected Integer level;//文件的类型 1 文件夹 2文件protected Integer type;//添加子文件/文件夹public abstract void add(FileComponent fileComponent);//移除子文件/文件夹public abstract void remove(FileComponent fileComponent);//获取指定的子文件/文件夹public abstract FileComponent getChild(int index);//打印子 子文件/子文件夹 名称的方法public abstract void print();
}

(2)树枝构件(Composite)

public class FileFolder extends FileComponent{//文件夹可以有多个子文件夹或者子文件private  List<FileComponent> fileComponentList;public FileFolder(String name, Integer level, Integer type) {this.name = name;this.level = level;this.type = type;this.fileComponentList = new ArrayList<>();}@Overridepublic void add(FileComponent fileComponent) {fileComponentList.add(fileComponent);}@Overridepublic void remove(FileComponent fileComponent) {fileComponentList.remove(fileComponent);}@Overridepublic FileComponent getChild(int index) {return fileComponentList.get(index);}@Overridepublic void print() {//打印菜单名称for (int i = 0; i < level; i++) {System.out.print("\t");}System.out.println(name);//打印子菜单或者子菜单项名称for (FileComponent component : fileComponentList) {component.print();}}
}

(3)树叶构件(Leaf)

public class FileItem extends FileComponent{public FileItem(String name, Integer level, Integer type) {this.name = name;this.level = level;this.type = type;}@Overridepublic void add(FileComponent fileComponent) {}@Overridepublic void remove(FileComponent fileComponent) {}@Overridepublic FileComponent getChild(int index) {return null;}@Overridepublic void print() {//打印文件的名称for (int i = 0; i < level; i++) {System.out.print("\t");}System.out.println(name);}
}

客户端:

public class Test {public static void main(String[] args) {//定义根目录FileComponent rootComponent = new FileFolder("我是根目录",1,1);//定义二级文件夹FileComponent secondLevelComponent = new FileFolder("我是二级目录",2,1);//定义文件FileComponent file = new FileItem("我是文件",3,2);//向根目录添加二级目录rootComponent.add(secondLevelComponent);//向二级目录添加文件secondLevelComponent.add(file);//打印rootComponent.print();}
}

相关文章:

设计模式(15)组合模式

一、介绍&#xff1a; 1、定义&#xff1a;组合多个对象形成树形结构以表示“整体-部分”的关系的层次结构。组合模式对叶子节点和容器节点的处理具有一致性&#xff0c;又称为整体-部分模式。 2、优缺点&#xff1a; 优点&#xff1a; &#xff08;1&#xff09;高层模块调…...

小黑子—spring:第一章

spring入门1.0 一 小黑子对spring基础进行概述1.1 spring导论1.2 传统Javaweb开发困惑及解决方法1.3 三大的思想提出1.3.1 IOC入门案例1.3.2 DI入门案例 1.4 框架概念1.5 初识spring1.5.1 Spring Framework 1.6 BeanFactory快速入门1.7 ApplicationContext快速入门1.8 BeanFact…...

【面向对象】理解面向对象编程中的封装性

封装性是面向对象编程中的重要概念之一&#xff0c;它允许开发者将数据和方法组合成一个独立的单元&#xff0c;并通过定义访问权限来控制对这个单元的访问。本文将深入探讨面向对象编程中的封装性&#xff0c;包括封装的概念、实现封装的方式以及封装的好处。通过详细的解释和…...

ES修改字段类型详解

一、需求概述 ES修改字段类型是指在已有的索引中&#xff0c;通过特定的操作方式将某个字段的类型修改为其它类型。当ES在建立索引的时候&#xff0c;已经确定好了每个字段的类型&#xff0c;而如果在建立后发现类型不符需求&#xff0c;就需要修改字段类型。 二、修改字段类…...

C++基础:函数模板

为了代码重用&#xff0c;代码必须是通用的&#xff1b;通用的代码就必须不受数据类型的限制。那么我们可以把数据类型改为一个设计参数&#xff0c;这种类型的程序设计称为参数化程序设计&#xff0c;软件模板有模板构造&#xff0c;包括函数模板和类模板。 函数模板可以用来…...

Facebook账号被封?那是因为没做对这些事

Facebook是全球最大的社交媒体平台之一&#xff0c;拥有数十亿的全球用户。它的主要产品包括Facebook&#xff08;面向个人用户的社交媒体平台&#xff09;、Instagram、WhatsApp和Messenger。同时他也是美国数字广告市场的主要参与者之一&#xff0c;其广告平台吸引了数百万广…...

虚拟机kafka

一、kafka安装 &#xff08;1&#xff09;解压 &#xff08;2&#xff09;修改名字为kafka212 &#xff08;3&#xff09;进入/opt/soft/kafka212/config目录&#xff0c;配置文件server.properties 21 broker.id0 36 advertised.listenersPLAINTEXT://192.168.91.11:9092 …...

软考 系统架构设计师系列知识点之设计模式(3)

接前一篇文章&#xff1a;软考 系统架构设计师系列知识点之设计模式&#xff08;2&#xff09; 所属章节&#xff1a; 老版&#xff08;第一版&#xff09;教材 第7章. 设计模式 第2节. 设计模式实例 2. 结构型模式 结构型模式控制了应用程序较大部分之间的关系。它将以不同…...

217. 存在重复元素、Leetcode的Python实现

博客主页&#xff1a;&#x1f3c6;看看是李XX还是李歘歘 &#x1f3c6; &#x1f33a;每天分享一些包括但不限于计算机基础、算法等相关的知识点&#x1f33a; &#x1f497;点关注不迷路&#xff0c;总有一些&#x1f4d6;知识点&#x1f4d6;是你想要的&#x1f497; ⛽️今…...

49.Redis缓存设计与性能优化

缓存与数据库双写不一致小概率事件 //线程1 写数据库stock 5 ---------------》更新缓存 //线程2 写数据库stock 4 -----》更新缓存 //线程1 ------》写数据库stock 10 -----》删除缓存 //线程2 ---------------------------------------------------------------------…...

IDEA常用的一些插件

1、CodeGlance 代码迷你缩放图插件&#xff0c;可以快速拖动代码&#xff0c;和VScode一样 2、Codota 代码提示工具&#xff0c;扫描你的代码后&#xff0c;根据你的敲击完美提示。 Codota基于数百万个开源Java程序和您的上下文来完成代码行&#xff0c;从而帮助您以更少的…...

基于定容积法标准容器容积标定中的电动针阀自动化解决方案

摘要&#xff1a;在目前的六氟化硫气体精密计量中普遍采用重量法和定容法两种技术&#xff0c;本文分析了重量法中存在的问题以及定容法的优势&#xff0c;同时也指出定容法在实际应用中还存在自动化水平较低的问题。为了提高定容法精密计量过程中的自动化水平&#xff0c;本文…...

26 行为型模式-命令模式

1 命令模式介绍 2 命令模式原理 3 命令模式实现 模拟酒店后厨的出餐流程,来对命令模式进行一个演示,命令模式角色的角色与案例中角色的对应关系如下: 服务员: 即调用者角色,由她来发起命令. 厨师: 接收者,真正执行命令的对象. 订单: 命令中包含订单 /*** 订单类**/ public cl…...

一个Entity Framework Core的性能优化案例

概要 本文提供一个EF Core的优化案例&#xff0c;主要介绍一些EF Core常用的优化方法&#xff0c;以及在优化过程中&#xff0c;出现性能反复的时候的解决方法&#xff0c;并澄清一些对优化概念的误解&#xff0c;例如AsNoTracking并不包治百病。 本文使用的是Dotnet 6.0和EF…...

【Python 千题 —— 基础篇】列表排序

题目描述 题目描述 给定一个包含无序数字的列表&#xff0c;请将列表中的数字按从小到大的顺序排列&#xff0c;并输出排序后的列表。 输入描述 输入一个包含无序数字的列表。 输出描述 程序将对列表中的数字进行排序&#xff0c;并输出排序后的列表。 示例 示例 ① 1…...

leetcode26:删除有序数组中的重复项

leetcode26&#xff1a;删除有序数组中的重复项 方案一&#xff1a;依次遍历&#xff0c;如果不符合条件则冒泡交换到最后一个位置。o(n^2),结果超时 #include <algorithm> #include <iostream>using namespace std; class Solution { public:int removeDuplicat…...

[FSCTF 2023] web题解

文章目录 源码&#xff01;启动!webshell是啥捏细狗2.0ez_php1Hello,youEZ_eval巴巴托斯&#xff01; 源码&#xff01;启动! 打开题目&#xff0c;发现右键被禁了 直接ctrlu查看源码得到flag webshell是啥捏 源码 <?php highlight_file(__FILE__); $&#x1f600;&qu…...

linux查看内存的方式

1、显示内存状态:free -h  以合适的单位显示内存使用情况&#xff0c;最大为三位数&#xff0c;自动计算对应的单位值。单位有&#xff1a; B bytes K kilos M megas G gigas T teras $free -htotal used free shared buff/cache available Me…...

Python 编写 Flink 应用程序经验记录(Flink1.17.1)

目录 官方API文档 提交作业到集群运行 官方示例 环境 编写一个 Flink Python Table API 程序 执行一个 Flink Python Table API 程序 实例处理Kafka后入库到Mysql 下载依赖 flink-kafka jar 读取kafka数据 写入mysql数据 flink-mysql jar 官方API文档 https://nigh…...

如何 通过使用优先级提示,来控制所有网页资源加载顺序

当你打开浏览器的网络标签时&#xff0c;你会看到大量的活动。资源正在下载&#xff0c;信息正在提交&#xff0c;事件正在记录&#xff0c;等等。 由于有太多的活动&#xff0c;有效地管理这些流量的优先级变得至关重要。带宽争用是真实存在的&#xff0c;当所有请求同时触发时…...

css实现圆环展示百分比,根据值动态展示所占比例

代码如下 <view class""><view class"circle-chart"><view v-if"!!num" class"pie-item" :style"{background: conic-gradient(var(--one-color) 0%,#E9E6F1 ${num}%),}"></view><view v-else …...

盘古信息PCB行业解决方案:以全域场景重构,激活智造新未来

一、破局&#xff1a;PCB行业的时代之问 在数字经济蓬勃发展的浪潮中&#xff0c;PCB&#xff08;印制电路板&#xff09;作为 “电子产品之母”&#xff0c;其重要性愈发凸显。随着 5G、人工智能等新兴技术的加速渗透&#xff0c;PCB行业面临着前所未有的挑战与机遇。产品迭代…...

基于Flask实现的医疗保险欺诈识别监测模型

基于Flask实现的医疗保险欺诈识别监测模型 项目截图 项目简介 社会医疗保险是国家通过立法形式强制实施&#xff0c;由雇主和个人按一定比例缴纳保险费&#xff0c;建立社会医疗保险基金&#xff0c;支付雇员医疗费用的一种医疗保险制度&#xff0c; 它是促进社会文明和进步的…...

Spring Boot+Neo4j知识图谱实战:3步搭建智能关系网络!

一、引言 在数据驱动的背景下&#xff0c;知识图谱凭借其高效的信息组织能力&#xff0c;正逐步成为各行业应用的关键技术。本文聚焦 Spring Boot与Neo4j图数据库的技术结合&#xff0c;探讨知识图谱开发的实现细节&#xff0c;帮助读者掌握该技术栈在实际项目中的落地方法。 …...

Matlab | matlab常用命令总结

常用命令 一、 基础操作与环境二、 矩阵与数组操作(核心)三、 绘图与可视化四、 编程与控制流五、 符号计算 (Symbolic Math Toolbox)六、 文件与数据 I/O七、 常用函数类别重要提示这是一份 MATLAB 常用命令和功能的总结,涵盖了基础操作、矩阵运算、绘图、编程和文件处理等…...

是否存在路径(FIFOBB算法)

题目描述 一个具有 n 个顶点e条边的无向图&#xff0c;该图顶点的编号依次为0到n-1且不存在顶点与自身相连的边。请使用FIFOBB算法编写程序&#xff0c;确定是否存在从顶点 source到顶点 destination的路径。 输入 第一行两个整数&#xff0c;分别表示n 和 e 的值&#xff08;1…...

dify打造数据可视化图表

一、概述 在日常工作和学习中&#xff0c;我们经常需要和数据打交道。无论是分析报告、项目展示&#xff0c;还是简单的数据洞察&#xff0c;一个清晰直观的图表&#xff0c;往往能胜过千言万语。 一款能让数据可视化变得超级简单的 MCP Server&#xff0c;由蚂蚁集团 AntV 团队…...

Springboot社区养老保险系统小程序

一、前言 随着我国经济迅速发展&#xff0c;人们对手机的需求越来越大&#xff0c;各种手机软件也都在被广泛应用&#xff0c;但是对于手机进行数据信息管理&#xff0c;对于手机的各种软件也是备受用户的喜爱&#xff0c;社区养老保险系统小程序被用户普遍使用&#xff0c;为方…...

处理vxe-table 表尾数据是单独一个接口,表格tableData数据更新后,需要点击两下,表尾才是正确的

修改bug思路&#xff1a; 分别把 tabledata 和 表尾相关数据 console.log() 发现 更新数据先后顺序不对 settimeout延迟查询表格接口 ——测试可行 升级↑&#xff1a;async await 等接口返回后再开始下一个接口查询 ________________________________________________________…...

【无标题】路径问题的革命性重构:基于二维拓扑收缩色动力学模型的零点隧穿理论

路径问题的革命性重构&#xff1a;基于二维拓扑收缩色动力学模型的零点隧穿理论 一、传统路径模型的根本缺陷 在经典正方形路径问题中&#xff08;图1&#xff09;&#xff1a; mermaid graph LR A((A)) --- B((B)) B --- C((C)) C --- D((D)) D --- A A -.- C[无直接路径] B -…...