Composite,组合模式:将对象组合成树形结构以表示部分整体的关系,Composite使得用户对单个对象和组合对象的使用具有一致性。

 

package com.cbf4life.common;
import java.util.ArrayList;
/**
* 定义一个根节点,就为总经理服务
*/
public interface IRoot {
//得到总经理的信息
public String getInfo();
//总经理下边要有小兵,那要能增加小兵,比如研发部总经理,这是个树枝节点
public void add(IBranch branch);
//增加树叶节点,比如秘书
public void add(ILeaf leaf);
//既然能增加,那要还要能够遍历,不可能总经理不知道他手下有哪些人
public ArrayList getSubordinateInfo();
}

//这个根节点就是我们的总经理CEO,然后看实现类:
package com.cbf4life.common;
import java.util.ArrayList;
@SuppressWarnings("all")
public 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;
}
//增加树枝节点
public void add(IBranch branch) {
this.subordinateList.add(branch);
}
//增加叶子节点,比如秘书,直接隶属于总经理
public void add(ILeaf leaf) {
this.subordinateList.add(leaf);
}
//得到自己的信息
public String getInfo() {
String info = "";
info = "名称:"+ this.name;;
info = info + "\t职位:" + this.position;
info = info + "\t薪水: " + this.salary;
return info;
}
//得到下级的信息
public ArrayList getSubordinateInfo() {
return this.subordinateList;
}
}
//通过构造函数传入参数,然后获得信息,还可以增加子树枝节点(部门经理)和叶子节点(秘书)
package com.cbf4life.common;
import java.util.ArrayList;
/**
* 树枝节点,也就是各个部门经理和组长的角色
*/
public interface IBranch {
public String getInfo();
public void add(IBranch branch);
public void add(ILeaf leaf);
public ArrayList getSubordinateInfo();
}

package com.cbf4life.common;
import java.util.ArrayList;
@SuppressWarnings("all")
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(IBranch branch) {
this.subordinateList.add(branch);
}

public void add(ILeaf leaf) {
this.subordinateList.add(leaf);
}
public String getInfo() {
String info = "";
info = "名称:" + this.name;
info = info + "\t职位:"+ this.position;
info = info + "\t薪水:"+this.salary;
return info;
}
public ArrayList getSubordinateInfo() {
return this.subordinateList;
}
}

package com.cbf4life.common;
public interface ILeaf {
public String getInfo();
}

package com.cbf4life.common;
@SuppressWarnings("all")
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;
}
//最小的小兵只能获得自己的信息了
public String getInfo() {
String info = "";
info = "名称:" + this.name;
info = info + "\t职位:"+ this.position;
info = info + "\t薪水:"+this.salary;
return info;
}
}
//然后的工作就是组装成一个树状结构和遍历这个树状结构,看Client.java 程序:
package com.cbf4life.common;
import java.util.ArrayList;
@SuppressWarnings("all")
public 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();
for(int m=0;m<length;m++){ //定义一个ArrayList长度,不要在for循环中每次计算
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());
}
}
}
}
--------------------------------------------------------------------------------------------------------------------------------------------------------------------
//ICorp 是公司所有人员的信息的接口类
package com.cbf4life.advance;
/**
* 公司类,定义每个员工都有信息
*/
public interface ICorp {
public String getInfo();
}

package com.cbf4life.advance;
@SuppressWarnings("all")
public class Leaf implements ICorp {

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;
}

public String getInfo() {
String info = "";
info = "姓名:" + this.name;
info = info + "\t职位:"+ this.position;
info = info + "\t薪水:" + this.salary;
return info;
}
}

package com.cbf4life.advance;
import java.util.ArrayList;
public interface IBranch {


public void addSubordinate(ICorp corp);

public ArrayList<ICorp> getSubordinate();
/*本来还应该有一个方法delSubordinate(ICorp corp),删除下属
* 这个方法我们没有用到就不写进来了
*/
} package com.cbf4life.advance;
import java.util.ArrayList;
@SuppressWarnings("all")
public class Branch implements IBranch, ICorp {

private String name = "";

private String position = "";

private int salary = 0;

ArrayList<ICorp> subordinateList = new ArrayList<ICorp>();

public Branch(String name,String position,int salary){
this.name = name;
this.position = position;
this.salary = salary;
}

public void addSubordinate(ICorp corp) {
this.subordinateList.add(corp);
}

public ArrayList<ICorp> getSubordinate() {
return this.subordinateList;
}

public String getInfo() {
String info = "";
info = "姓名:" + this.name;
info = info + "\t职位:"+ this.position;
info = info + "\t薪水:" + this.salary;
return info;
}
}

package com.cbf4life.advance;
import java.util.ArrayList;
@SuppressWarnings("all")
public class Client {
public static void main(String[] args) {

Branch ceo = compositeCorpTree();

System.out.println(ceo.getInfo());

System.out.println(getTreeInfo(ceo));

}

public static Branch compositeCorpTree(){

Branch 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);


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<ICorp> subordinateList = root.getSubordinate();
String info = "";
for(ICorp s :subordinateList){
if(s instanceof Leaf){
info = info + s.getInfo()+"\n";
}else{
info = info + s.getInfo() +"\n"+ getTreeInfo((Branch)s);
}
}
return info;
}
}-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
package com.cbf4life.perfect;
@SuppressWarnings("all")
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;
}
}

package com.cbf4life.perfect;
public class Leaf extends Corp {
public Leaf(String _name,String _position,int _salary){
super(_name,_position,_salary);
}
}

package com.cbf4life.perfect;
import java.util.ArrayList;

public 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;
}
}

package com.cbf4life.perfect;
import java.util.ArrayList;

@SuppressWarnings("all")
public class Client {
public static void main(String[] args) {

Branch ceo = compositeCorpTree();

System.out.println(ceo.getInfo());

System.out.println(getTreeInfo(ceo));
}

public static Branch compositeCorpTree(){

Branch 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);

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 = 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;
}
}
//就是把用到ICorp 接口的地方修改为Corp 抽象类就成了