JAVA设计模式之 组合模式【Composite Pattern】
来源:互联网 发布:spring aop切面编程 编辑:程序博客网 时间:2024/05/29 11:39
一、组合模式介绍
组合模式(Composite Pattern)有时候又叫做部分-整体模式,它使我们树型结构的问题中,模糊了简单元素和复杂元素的概念。
组合模式有两种模式,透明模式 和 安全模式、下面先来两个类图吧、来看看透明模式和安全模式区别;
从类图上应该能看清楚了,这两种模式各有优缺点,透明模式是把用来组合使用的方法放到抽象类中,比如add(),remove()以及getChildren 等方法,不管叶子对象还是树枝对象都有相同的结构,通过判断是getChildren 的返回值确认是叶子节点还是树枝节点,如果处理不当,这个会在运行期出现问题的,不是很建议的方式;安全模式就不同了,它是把树枝节点和树叶节点彻底分开,树枝节点单独拥有用来组合的方法,这种方法比较安全;
二、DEMO程序、接下来、我们就来一个安全模式的DEMO(以公司的管理结构为例):
1、定义一个公司的人员的抽象类、
package com.ice.composite;/** * * @author lvzb.software@qq.com * 定义一个公司的人员的抽象类 */public 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;} /** * 获得员工信息 * @return */public String getInfo(){String info = "";info = "姓名:" + this.name;info = info + "\t职位:"+ this.position;info = info + " \t薪水:" + this.salary;return info;}}
2、定义普通员工的实现类、
package com.ice.composite;/** * * @author lvzb.software@qq.com * 普通员工就像树形结构中叶子节点、他的下面不可再分啦、就像我们这种最最苦逼不过的程序员一样 */public class Leaf extends Corp {public Leaf(String name, String position, int salary) {super(name, position, salary);}}
3、定义那些小头目的实现类、
package com.ice.composite;import java.util.ArrayList;/** * * @author lvzb.software@qq.com * 这里是小头目们的实现类、什么总经理啊、测试经理啊、项目经理啊、开发组长啊 之类的家伙 */public class Branch extends Corp {public Branch(String name, String position, int salary) {super(name, position, salary);}// 领导下边有那些下级领导和小兵ArrayList<Corp> subordinateList = new ArrayList<Corp>();// 增加一个下属,可能是小头目,也可能是个小兵public void addSubordinate(Corp corp){subordinateList.add(corp);}// 获得该领导有哪些下属,他是领导、他肯定得知道他下属一级的情况啊、是不?public ArrayList<Corp> getSubordinate(){return subordinateList;}}
4、组装和展示树形结构:
package com.ice.composite;import java.util.ArrayList;/** * * @author lvzb.software@qq.com * 组装这个树形结构,并展示出来 */public class Client {/** * @param args */public static void main(String[] args) {// 首先是组装一个组织结构出来Branch ceo = compositeCorpTree();// 首先把CEO的信息打印出来:System.out.println(ceo.getInfo());// 把树的完整结构打印出来System.out.println(getTreeInfo(ceo));}/** * 把整个数组装起来 * @return */private static Branch compositeCorpTree() {// 首先产生总经理ceoBranch root = new Branch("王大麻子", "总经理", 100000);// 接着产生三个部门经理Branch developDep = new Branch("刘大瘸子", "研发部门经理", 15000);Branch salesDep = new Branch("马二拐子","销售部门经理",10000);Branch financeDep = new Branch("赵三驼子","财务部经理",80000);// 再把两个开发小组长产生出来Branch firstDevGroup = new Branch("杨三乜斜","开发一组组长",5000);Branch secondDevGroup = new Branch("吴大棒槌","开发二组组长",6000);// 最后就是我们这些小罗罗啦、当然啦、秘书的话是隶属总经理的、呵呵 你懂的...Leaf a = new Leaf("小王","开发人员",3000);Leaf b = new Leaf("小龙","开发人员",3000);Leaf c = new Leaf("小周","开发人员",3000);Leaf d = new Leaf("小和","开发人员",3000);Leaf e = new Leaf("小康","开发人员",3000);Leaf f = new Leaf("小兰","开发人员",3000);Leaf k = new Leaf("小蜜","秘书",5000); // ps: 你看、秘书工资都比咱们高高啦、不想吐槽、、// 开始组装树root.addSubordinate(k);root.addSubordinate(developDep);root.addSubordinate(salesDep);root.addSubordinate(financeDep);developDep.addSubordinate(firstDevGroup);developDep.addSubordinate(secondDevGroup);firstDevGroup.addSubordinate(a);firstDevGroup.addSubordinate(b);firstDevGroup.addSubordinate(c);secondDevGroup.addSubordinate(d);secondDevGroup.addSubordinate(e);secondDevGroup.addSubordinate(f);return root;}/** * 遍历整棵树,只要给我根节点,我就能遍历出所有的节点 */private static String getTreeInfo(Branch root) {ArrayList<Corp> subordinate = root.getSubordinate();String info = "";for(Corp s:subordinate){if(s instanceof Leaf){info = info+s.getInfo()+"\n";}else{info = info+s.getInfo()+"\n"+getTreeInfo((Branch)s);}} return info;}}
5、下面我们来看一下运行结果:
三、组合模式的几个角色:
抽象构件角色(Component):定义参加组合的对象的共有方法和属性,可以定义一些默认的行为或属性;比如我们例子中的getInfo 就封装到了抽象类中。
叶子构件(Leaf):叶子对象,其下再也没有其他的分支。
树枝构件(Composite):树枝对象,它的作用是组合树枝节点和叶子节点;
- 设计模式之组合模式(Composite Pattern)
- 设计模式之组合模式(Composite Pattern)
- 设计模式之组合模式(Composite Pattern)
- 设计模式之组合模式---Composite Pattern
- 设计模式之组合模式(Composite Pattern)
- Java设计模式--组合模式【Composite Pattern】
- JAVA设计模式之 组合模式【Composite Pattern】
- Java设计模式之--组合模式(Composite Pattern)
- Java Composite Pattern(组合模式)
- java设计模式之组合模式Composite
- net设计模式实例之组合模式(Composite Pattern)
- Php设计模式之【组合模式Composite Pattern】
- 设计模式之组合模式(Composite Pattern)
- C#设计模式之组合模式(Composite Pattern)
- 设计模式(结构型)之组合模式(Composite Pattern)
- 设计模式之----组合模式(Composite Design Pattern)
- 设计模式总结之Composite Pattern(组合模式)
- Net设计模式实例之组合模式(Composite Pattern)
- 7 Important Data Science Papers
- 泛型
- 有了MAC地址,为什么还要有IP地址?
- 黑马程序员:模板
- 在jsp页面获得url参数的方法
- JAVA设计模式之 组合模式【Composite Pattern】
- RedrawWindow(NULL, NULL, RDW_INVALIDATE|RDW_ERASE|RDW_ERASENOW|RDW_ALLCHILDREN);
- 5 Cross-Platform Mobile Development Tools You Should Try
- android(客户端)和PC(服务器端)通信RSA 加密解密
- Android关机流程源码分析
- Invoke 用法
- 【分享】娱乐圈大V微博信息数据
- 几个输入输出函数的比较
- linux下抓包命令--tcpdump的使用