组合模式比较难理解,所以先看相关例子,本章节代码较多,需在git下载
这是一个常见的数据库结构
比较典型的树状结构(常见的还有菜单)
一般的做法
CEO我们认为是根节点
developDep,developGroup,salesDep,fianceDep是树枝节点
k,a,b则是树叶节点(树叶节点是最小的节点.不能延伸或者有分支)
一般的做法是定义三个接口用来定义三个角色,然后通过add方法进行组合在一起,最终结果
详细代码: 组合模式1
demo: https://gitee.com/minstrel01/Design-Pattern-demo.git composite_pattern.example_1
虽然能完成需求,但是问题在于 IRoot和IBranch 其实是一样的,含有的方法也一样,而且每个类中都有相同的方法getInfo(),需要编写大量重复代码而且不易扩展
优化:
我们首先把IRoot和IBranch合并为同一个接口。IRoot也认为是IBranch
然后树枝节点和树叶节点也有相同的方法 getInfo(),将getInfo()放到ICorp接口中
优化详细代码:组合模式2
demo: https://gitee.com/minstrel01/Design-Pattern-demo.git composite_pattern.example_2
运行结果:
在查看优化后的代码时会发现Banch类和Leaf 中都有getInfo的信息
public 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;
}
//增加一个员工(树枝节点或者树叶节点)
public void add(ICorp corp) {
this.subordinateList.add(corp);
}
//获得自己树枝节点的信息
@Override
public String getInfo() {
String info = "";
info = "名称:" + this.name;
info = info + "\t职位:" + this.position;
info = info + "\t薪水:" + this.salary;
return info;
}
//获得下级信息
@Override
public ArrayList getSubordinateInfo() {
return this.subordinateList;
}
}
public 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;
}
//获得自己的信息
@Override
public String getInfo() {
String info = "";
info = "名称:" + this.name;
info = info + "\t职位:" + this.position;
info = info + "\t薪水:" + this.salary;
return info;
}
}
将getInfo相关信息抽象一下
Ibranch接口没有了,Crop也变成了抽象类,抽象类其实就是抽象出共性,将这段代码放到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 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;
}
}
//叶子节点
public class Leaf extends Corp {
public Leaf(String _name, String _position, int _salary){
super(_name,_position,_salary);
}
}
//树枝节点
public class Branch extends Corp {
//保存树枝节点下的子树枝节点和树叶节点,也就是高级下属和基础下属
private ArrayList subordinateList = new ArrayList();
public Branch(String _name, String _position, int _salary){
super(_name,_position,_salary);
}
//增加一个员工(树枝节点或者树叶节点)
public void add(Corp corp) {
this.subordinateList.add(corp);
}
public ArrayList getSubordinateInfo() {
return this.subordinateList;
}
}
public class Main {
public static void main(String[] args) {
//组装一个组织结构
Branch ceo = compositeCorpTree();
//打印ceo信息
System.out.println(ceo.getInfo());
//打印所有员工信息
System.out.println(getTree(ceo));
}
private static Branch compositeCorpTree(){
//先产生一个CEO
Branch ceo = new Branch("刘备","总经理",10000);
//产生树枝节点 高级下属三个部门经理
Branch developDep = new Branch("诸葛亮","研发部经理",5000);
Branch salesDep = new Branch("关羽","销售部经理",5000);
Branch financeDep = new Branch("张飞","财务部经理",5000);
//生成一个子树枝节点 组长
Branch developGroup = new Branch("马谡","研发部组长",3000);
//生成员工
Leaf k = new Leaf("k","CEO秘书",1000);
Leaf a = new Leaf("a","员工A",1000);
Leaf b = new Leaf("b","员工B",1000);
//总经理定义
ceo.add(developDep);
ceo.add(salesDep);
ceo.add(financeDep);
ceo.add(k);
//部门经理定义
developDep.add(developGroup);
salesDep.add(b);
//组长定义
developGroup.add(a);
return ceo;
}
//打印
private static String getTree(Branch branch){
ArrayList<Corp> subordinateList = branch.getSubordinateInfo();
String info = "";
for (Corp s : subordinateList){
if (s instanceof Leaf){
info = info +s.getInfo()+"\n";
}else {
info = info+ s.getInfo()+" \n" + getTree((Branch)s);
}
}
return info;
}
}
详细代码:组合模式3
demo: https://gitee.com/minstrel01/Design-Pattern-demo.git composite_pattern.example_3
这就是组织模式,详细解析在组合模式2