DesignPattern_组合模式_15

文章目录代码实现 后话
组合模式 定义
into treetopart-whole . letsandof.
将对象组合成树形结构以表示“部分-整体”的层次结构,使得用户对单个对象和组合对象的使用具有一致性 。
【DesignPattern_组合模式_15】组合模式( )也叫合成模式,有时又叫做部分-整体模式(Part-Whole),主要是用来描述部分与整体的关系 。
优点使用场景代码实现 基础代码
/*** @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 subordinateList = new ArrayList();//构造函数是必需的public Branch(String _name, String _position, int _salary) {super(_name, _position, _salary);}//增加一个下属,可能是小头目,也可能是个小兵public void addSubordinate(Corp corp) {this.subordinateList.add(corp);}//我有哪些下属public ArrayList 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 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;}}
后话
看完了吗?看完了就忘了吧 , 上面说的都是文绉绉的屁话 。。
下面的图就是组合模式,单表存储的树形结构 。目前自己接触到的树形结构基本上都是如下类似的单表 。写过树形结构的基本等同于会用组合模式 。。自己看法
附赠一个三行代码组装树形结构