Composite Pattern
Compose objects into tree structures to represent part-whole hierarchies.Composite lets clients treat individual objects and compositions of objects uniformly.(将对象组合成树形结构以表示"部分-整体"的层次结构,使得用户对单个对象和组合对象的使用具有一致性)
不认识的单词
hierarchies 等级,层次
uniformly 一致的,相同的
例子
将下图关系表示出来
分析
树,叶子节点,树枝节点,根节点,三者都要有一个getInfo()方法,三者都有姓名,职位,薪水,这些信息暂且通过构造函数传进来。
不同点:叶子节点不能添加子节点,而树枝节点和根节点可以,而且这俩个类还必须有一个集合来存下面的子节点对象。
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;
}
public String getInfo(){
String info="";
info="姓名:"+this.name+"\t 薪水:"+this.salary+"\t 职位:"+this.position;
return info;
}
}
将根节点当做树枝节点
public class Branch extends Corp {
ArrayList<Corp> subordinateList=new ArrayList<Corp>();
public Branch(String _name, String _position, int _salary) {
super(_name, _position, _salary);
}
/**
* 添加一个下属,可能是个小头目,也有可能是个小兵
* @param corp
*/
public void addSubordinate(Corp corp){
this.subordinateList.add(corp);
}
/**
*我有哪些下属
* @return
*/
public ArrayList<Corp> getSubordinate(){
return this.subordinateList;
}
}
public class Leaf extends Corp {
public Leaf(String _name, String _position, int _salary) {
super(_name, _position, _salary);
}
}
public class Client {
public static void main(String[] args) {
Branch ceo=compositeCorpTree();
System.out.println("CEO信息:"+ceo.getInfo());
getTreeInfo(ceo);
}
/**
* 准备数据
* @return
*/
public static Branch compositeCorpTree(){
Branch root=new Branch("刘大麻子","总经理",10000);
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;
}
/**
*遍历所有节点
* @param root
* @return
*/
public static void getTreeInfo(Branch root){
ArrayList<Corp> list= root.getSubordinate();
if(list!=null){
for(Corp c:list){
if(c instanceof Leaf){
System.out.println(c.getInfo());
}else{
System.out.println(root.getInfo());
getTreeInfo((Branch) c);
}
}
}
}
}
运行结果
CEO信息:姓名:刘大麻子 薪水:10000 职位:总经理
姓名:k 薪水:8000 职位:CEO秘书
姓名:刘大麻子 薪水:10000 职位:总经理
姓名:郑老六 薪水:20000 职位:研发部副经理
姓名:刘大瘸子 薪水:10000 职位:研发部门经理
姓名:a 薪水:2000 职位:开发人员
姓名:b 薪水:2000 职位:开发人员
姓名:c 薪水:2000 职位:开发人员
姓名:刘大瘸子 薪水:10000 职位:研发部门经理
姓名:d 薪水:2000 职位:开发人员
姓名:e 薪水:2000 职位:开发人员
姓名:f 薪水:2000 职位:开发人员
姓名:刘大麻子 薪水:10000 职位:总经理
姓名:h 薪水:5000 职位:销售人员
姓名:i 薪水:4000 职位:销售人员
姓名:刘大麻子 薪水:10000 职位:总经理
姓名:j 薪水:5000 职位:财务人员
悟
看完之后,觉得,这个东西特点性太强,用在其他地方还真不行。使人一看到类似的问题就会想到这个模式。 简单来说就是提炼相同的地方,抽取成一个抽象类,至于后面都是依葫芦画瓢了。
优点
调用简单
节点增加自由,非常容易扩展,还可以写删除节点,修改等等操作,好像数据结构了
我是菜鸟,我在路上。