DesignPattern

阅读: 评论:0

DesignPattern

DesignPattern

文章目录

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

组合模式

定义

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.Info());//打印出来整个树SubordinateInfo());}private static void getAllSubordinateInfo(ArrayList subordinateList) {int length = subordinateList.size();//定义一个ArrayList长度,不要在for循环中每次计算for (int m = 0; m < length; m++) {Object s = (m);if (s instanceof Leaf) {//是个叶子节点,也就是员工ILeaf employee = (ILeaf) s;System.out.println(((Leaf) s).getInfo());} else {IBranch branch = (IBranch) s;System.out.Info());//再递归调用SubordinateInfo());                  }             }}}
组合模式
/*** @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.Info());//然后是所有员工信息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 = Subordinate();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;}
}

后话

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

本文发布于:2024-02-01 21:46:20,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170679517939616.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:DesignPattern
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23