DesignPattern_组合模式_15

2023-12-21 15:50
文章标签 组合 模式 15 designpattern

本文主要是介绍DesignPattern_组合模式_15,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章目录

      • 组合模式
        • 定义
        • 优点
        • 使用场景
      • 代码实现
        • 基础代码
        • 组合模式
      • 后话

组合模式

定义

ompose objects into tree structures to represent part-whole hierarchies.Composite lets clientstreat individual objects and compositions of objects uniformly.
将对象组合成树形结构以表示“部分-整体”的层次结构,使得用户对单个对象和组合对象的使用具有一致性。
组合模式(Composite Pattern)也叫合成模式,有时又叫做部分-整体模式(Part-Whole),主要是用来描述部分与整体的关系。
组合模式通用类图

  • Component抽象构件角色
    定义参加组合对象的共有方法和属性,可以定义一些默认的行为或属性,比如我们例子中的getInfo就封装到了抽象类中
  • Leaf叶子构件
    叶子对象,其下再也没有其他的分支,也就是遍历的最小单位。
  • Composite树枝构件
    树枝对象,它的作用是组合树枝节点和叶子节点形成一个树形结构。
优点
  • 高层模块调用简单
    一棵树形机构中的所有节点都是Component,局部和整体对调用者来说没有任何区别,也就是说,高层模块不必关心自己处理的是单个对象还是整个组合结构,简化了高层模块的代码
  • 节点自由增加
    使用了组合模式后,我们可以看看,如果想增加一个树枝节点、树叶节点是不是都很容易,只要找到它的父节点就成,非常容易扩展,符合开闭原则,对以后的维护非常有利。
使用场景
  • 维护和展示部分-整体关系的场景,如树形菜单、文件和文件夹管理。
  • 从一个整体中能够独立出部分模块或功能的场景。

代码实现

基础代码
/*** @author huangqh* @create 2020/12/21 9:38* @Notes 组合模式*/
public class Combination {
}/*** 根节点接口*/interface IRoot {//获得总经理信息public String getInfo();//总经理下边要有小兵,那要能增加小兵,比如研发部总经理,这是个树枝节点public void add(IBranch branch);//增加树叶节点public void add(ILeaf leaf);//既然能增加,那还要能够遍历,不可能总经理不知道他手下有哪些人public ArrayList getSubordinateInfo();
}/*** 根节点的实现*/
class Root implements IRoot {//保存根节点下的树枝节点和树叶节点,Subordinate的意思是下级private ArrayList subordinateList = new ArrayList();//根节点名称private String name = "";//根节点职位private String position = "";//根节点薪水private int salary = 0;//通过构造函数传递进来总经理的信息public Root(String name, String position, int salary) {this.name = name;this.position = position;this.salary = salary;}@Overridepublic String getInfo() {String info = "";info = "名称:" + this.name;info = info + "\t职位:" + this.position;info = info + "\t薪水: " + this.salary;return info;}@Overridepublic void add(IBranch branch) {this.subordinateList.add(branch);}@Overridepublic void add(ILeaf leaf) {this.subordinateList.add(leaf);}@Overridepublic ArrayList getSubordinateInfo() {return this.subordinateList;}
}/*** 其他有分支的节点接口*/
interface IBranch {//获得信息public String getInfo();//增加数据节点public void add(IBranch branch);//增加叶子节点public void add(ILeaf leaf);//获得下级信息public ArrayList getSubordinateInfo();
}/*** 分支的节点实现*/
class Branch implements IBranch {//存储子节点的信息private ArrayList subordinateList = new ArrayList();//根枝节点名称private String name = "";//根枝节点职位private String position = "";//根枝节点薪水private int salary = 0;public Branch(String name, String position, int salary) {this.name = name;this.position = position;this.salary = salary;}@Overridepublic String getInfo() {String info = "";info = "名称:" + this.name;info = info + "\t职位:" + this.position;info = info + "\t薪水: " + this.salary;return info;}@Overridepublic void add(IBranch branch) {this.subordinateList.add(branch);}@Overridepublic void add(ILeaf leaf) {this.subordinateList.add(leaf);}@Overridepublic ArrayList getSubordinateInfo() {return this.subordinateList;}
}/*** 叶子节点的接口*/
interface ILeaf {public String getInfo();
}/*** 叶子节点实现*/
class Leaf implements ILeaf {//叶子节点名称private String name = "";//叶子节点职位private String position = "";//叶子节点薪水private int salary = 0;public Leaf(String name, String position, int salary) {this.name = name;this.position = position;this.salary = salary;}@Overridepublic String getInfo() {String info = "";info = "名称:" + this.name;info = info + "\t职位:" + this.position;info = info + "\t薪水: " + this.salary;return info;}
}class Client {public static void main(String[] args) {//首先产生了一个根节点IRoot ceo = new Root("王大麻子", "总经理", 100000);//产生三个部门经理,也就是树枝节点IBranch developDep = new Branch("刘大瘸子", "研发部门经理", 10000);IBranch salesDep = new Branch("马二拐子", "销售部门经理", 20000);IBranch financeDep = new Branch("赵三驼子", "财务部经理", 30000);//再把三个小组长产生出来IBranch firstDevGroup = new Branch("杨三乜斜", "开发一组组长", 5000);IBranch secondDevGroup = new Branch("吴大棒槌", "开发二组组长", 6000);//剩下的就是我们这些小兵了,就是路人甲、路人乙ILeaf a = new Leaf("a", "开发人员", 2000);ILeaf b = new Leaf("b", "开发人员", 2000);ILeaf c = new Leaf("c", "开发人员", 2000);ILeaf d = new Leaf("d", "开发人员", 2000);ILeaf e = new Leaf("e", "开发人员", 2000);ILeaf f = new Leaf("f", "开发人员", 2000);ILeaf g = new Leaf("g", "开发人员", 2000);ILeaf h = new Leaf("h", "销售人员", 5000);ILeaf i = new Leaf("i", "销售人员", 4000);ILeaf j = new Leaf("j", "财务人员", 5000);ILeaf k = new Leaf("k", "CEO秘书", 8000);ILeaf zhengLaoLiu = new Leaf("郑老六", "研发部副总", 20000);//该产生的人都产生出来了,然后我们怎么组装这棵树// 首先是定义总经理下有三个部门经理ceo.add(developDep);ceo.add(salesDep);ceo.add(financeDep);//总经理下还有一个秘书ceo.add(k);//定义研发部门下的结构developDep.add(firstDevGroup);developDep.add(secondDevGroup);//研发部经理下还有一个副总developDep.add(zhengLaoLiu);//看看开发两个开发小组下有什么firstDevGroup.add(a);firstDevGroup.add(b);firstDevGroup.add(c);secondDevGroup.add(d);secondDevGroup.add(e);secondDevGroup.add(f);//再看销售部下的人员情况salesDep.add(h);salesDep.add(i);//最后一个财务financeDep.add(j);
//打印写完的树状结构System.out.println(ceo.getInfo());//打印出来整个树getAllSubordinateInfo(ceo.getSubordinateInfo());}private static void getAllSubordinateInfo(ArrayList subordinateList) {int length = subordinateList.size();//定义一个ArrayList长度,不要在for循环中每次计算for (int m = 0; m < length; m++) {Object s = subordinateList.get(m);if (s instanceof Leaf) {//是个叶子节点,也就是员工ILeaf employee = (ILeaf) s;System.out.println(((Leaf) s).getInfo());} else {IBranch branch = (IBranch) s;System.out.println(branch.getInfo());//再递归调用getAllSubordinateInfo(branch.getSubordinateInfo());                  }             }}}
组合模式
/*** @author huangqh* @create 2020/12/21 9:38* @Notes 组合模式*/
public class Combination {
}abstract class Corp {//公司每个人都有名称private String name = "";//公司每个人都职位private String position = "";//    公司每个人都有薪水private int salary = 0;public Corp(String _name, String _position, int _salary) {this.name = _name;this.position = _position;this.salary = _salary;}//获得员工信息public String getInfo() {String info = "";info = "姓名:" + this.name;info = info + "\t职位:" + this.position;info = info + "\t薪水:" + this.salary;return info;}
}/*** 分支的节点实现*/
class Branch extends Corp {//领导下边有哪些下级领导和小兵ArrayList<Corp> subordinateList = new ArrayList<Corp>();//构造函数是必需的public Branch(String _name, String _position, int _salary) {super(_name, _position, _salary);}//增加一个下属,可能是小头目,也可能是个小兵public void addSubordinate(Corp corp) {this.subordinateList.add(corp);}//    我有哪些下属public ArrayList<Corp> getSubordinate() {return this.subordinateList;}
}/*** 叶子节点实现*/
class Leaf extends Corp {public Leaf(String name, String position, int salary) {super(name, position, salary);}
}class Client {public static void main(String[] args) {//首先是组装一个组织机构出来Branch ceo = compositeCorpTree();//首先把CEO的信息打印出来System.out.println(ceo.getInfo());//然后是所有员工信息System.out.println(getTreeInfo(ceo));}//把整个树组装出来public static Branch compositeCorpTree() {//首先产生总经理CEOBranch root = new Branch("王大麻子", "总经理", 100000);//把三个部门经理产生出来Branch developDep = new Branch("刘大瘸子", "研发部门经理", 10000);Branch salesDep = new Branch("马二拐子", "销售部门经理", 20000);Branch financeDep = new Branch("赵三驼子", "财务部经理", 30000);
//再把三个小组长产生出来Branch firstDevGroup = new Branch("杨三乜斜", "开发一组组长", 5000);Branch secondDevGroup = new Branch("吴大棒槌", "开发二组组长", 6000);//把所有的小兵都产生出来Leaf a = new Leaf("a", "开发人员", 2000);Leaf b = new Leaf("b", "开发人员", 2000);Leaf c = new Leaf("c", "开发人员", 2000);Leaf d = new Leaf("d", "开发人员", 2000);Leaf e = new Leaf("e", "开发人员", 2000);Leaf f = new Leaf("f", "开发人员", 2000);Leaf g = new Leaf("g", "开发人员", 2000);Leaf h = new Leaf("h", "销售人员", 5000);Leaf i = new Leaf("i", "销售人员", 4000);Leaf j = new Leaf("j", "财务人员", 5000);Leaf k = new Leaf("k", "CEO秘书", 8000);Leaf zhengLaoLiu = new Leaf("郑老六", "研发部副经理", 20000);//开始组装//CEO下有三个部门经理和一个秘书root.addSubordinate(k);root.addSubordinate(developDep);root.addSubordinate(salesDep);root.addSubordinate(financeDep);//研发部经理developDep.addSubordinate(zhengLaoLiu);developDep.addSubordinate(firstDevGroup);developDep.addSubordinate(secondDevGroup);//看看两个开发小组下有什么firstDevGroup.addSubordinate(a);firstDevGroup.addSubordinate(b);firstDevGroup.addSubordinate(c);secondDevGroup.addSubordinate(d);secondDevGroup.addSubordinate(e);secondDevGroup.addSubordinate(f);//再看销售部下的人员情况salesDep.addSubordinate(h);salesDep.addSubordinate(i);//最后一个财务financeDep.addSubordinate(j);return root;}public static String getTreeInfo(Branch root) {ArrayList<Corp> subordinateList = root.getSubordinate();String info = "";for (Corp s : subordinateList) {if (s instanceof Leaf) {//是员工就直接获得信息info = info + s.getInfo() + "\n";} else {//是个小头目info = info + s.getInfo() + "\n" + getTreeInfo((Branch) s);}}return info;}
}

后话

看完了吗?看完了就忘了吧,上面说的都是文绉绉的屁话。。
下面的图就是组合模式,单表存储的树形结构。目前自己接触到的树形结构基本上都是如下类似的单表。写过树形结构的基本等同于会用组合模式。。自己看法
附赠一个三行代码组装树形结构
在这里插入图片描述

这篇关于DesignPattern_组合模式_15的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/520582

相关文章

PowerShell中15个提升运维效率关键命令实战指南

《PowerShell中15个提升运维效率关键命令实战指南》作为网络安全专业人员的必备技能,PowerShell在系统管理、日志分析、威胁检测和自动化响应方面展现出强大能力,下面我们就来看看15个提升... 目录一、PowerShell在网络安全中的战略价值二、网络安全关键场景命令实战1. 系统安全基线核查

Java设计模式---迭代器模式(Iterator)解读

《Java设计模式---迭代器模式(Iterator)解读》:本文主要介绍Java设计模式---迭代器模式(Iterator),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,... 目录1、迭代器(Iterator)1.1、结构1.2、常用方法1.3、本质1、解耦集合与遍历逻辑2、统一

Java 线程安全与 volatile与单例模式问题及解决方案

《Java线程安全与volatile与单例模式问题及解决方案》文章主要讲解线程安全问题的五个成因(调度随机、变量修改、非原子操作、内存可见性、指令重排序)及解决方案,强调使用volatile关键字... 目录什么是线程安全线程安全问题的产生与解决方案线程的调度是随机的多个线程对同一个变量进行修改线程的修改操

Redis Cluster模式配置

《RedisCluster模式配置》:本文主要介绍RedisCluster模式配置,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录分片 一、分片的本质与核心价值二、分片实现方案对比 ‌三、分片算法详解1. ‌范围分片(顺序分片)‌2. ‌哈希分片3. ‌虚

RabbitMQ工作模式中的RPC通信模式详解

《RabbitMQ工作模式中的RPC通信模式详解》在RabbitMQ中,RPC模式通过消息队列实现远程调用功能,这篇文章给大家介绍RabbitMQ工作模式之RPC通信模式,感兴趣的朋友一起看看吧... 目录RPC通信模式概述工作流程代码案例引入依赖常量类编写客户端代码编写服务端代码RPC通信模式概述在R

SQL Server身份验证模式步骤和示例代码

《SQLServer身份验证模式步骤和示例代码》SQLServer是一个广泛使用的关系数据库管理系统,通常使用两种身份验证模式:Windows身份验证和SQLServer身份验证,本文将详细介绍身份... 目录身份验证方式的概念更改身份验证方式的步骤方法一:使用SQL Server Management S

Redis高可用-主从复制、哨兵模式与集群模式详解

《Redis高可用-主从复制、哨兵模式与集群模式详解》:本文主要介绍Redis高可用-主从复制、哨兵模式与集群模式的使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝... 目录Redis高可用-主从复制、哨兵模式与集群模式概要一、主从复制(Master-Slave Repli

一文带你搞懂Redis Stream的6种消息处理模式

《一文带你搞懂RedisStream的6种消息处理模式》Redis5.0版本引入的Stream数据类型,为Redis生态带来了强大而灵活的消息队列功能,本文将为大家详细介绍RedisStream的6... 目录1. 简单消费模式(Simple Consumption)基本概念核心命令实现示例使用场景优缺点2

Nginx location匹配模式与规则详解

《Nginxlocation匹配模式与规则详解》:本文主要介绍Nginxlocation匹配模式与规则,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、环境二、匹配模式1. 精准模式2. 前缀模式(不继续匹配正则)3. 前缀模式(继续匹配正则)4. 正则模式(大

Linux系统配置NAT网络模式的详细步骤(附图文)

《Linux系统配置NAT网络模式的详细步骤(附图文)》本文详细指导如何在VMware环境下配置NAT网络模式,包括设置主机和虚拟机的IP地址、网关,以及针对Linux和Windows系统的具体步骤,... 目录一、配置NAT网络模式二、设置虚拟机交换机网关2.1 打开虚拟机2.2 管理员授权2.3 设置子