设计模式-组合模式

组合模式(Composite Pattern)也叫合成模式,有时又叫做部分-整体模式(Part-Whole),
主要是用来描述部分与整体的关系:

定义:

Compose objects into tree structures to represent part-whole hierarchies.Composite lets clients treat individual objects and compositions of objects uniformly.(将对象组合成树形结构以表示“部分-整体”的层次结构,使得用户对单个对象和组合对象的使用具有一致性。)
举个最常见的例子,公司组织架构就是一个典型的树状结构(网上截取图):


 
image.png

我们一般会这样设计组织架构,看代码实现

首先根节点IROOT

/**
 * 根节点
 * @author shuliangzhao
 * @Title: IRoot
 * @ProjectName design-parent
 * @Description: TODO
 * @date 2019/6/18 22:37
 */
public interface IRoot {
    //得到总经理的信息
    public String getInfo();
    //总经理下边要有小兵,那要能增加小兵,比如研发部总经理,这是个树枝节点
    public void add(IBranch branch);
    //那要能增加树叶节点
    public void add(ILeaf leaf);
    //既然能增加,那还要能够遍历,不可能总经理不知道他手下有哪些人
    public ArrayList getSubordinateInfo();
}

ROOT实现类

/**
 * @author shuliangzhao
 * @Title: Root
 * @ProjectName design-parent
 * @Description: TODO
 * @date 2019/6/18 22:40
 */
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;
    }

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

    @Override
    public void add(IBranch branch) {
        this.subordinateList.add(branch);
    }

    @Override
    public void add(ILeaf leaf) {
        this.subordinateList.add(leaf);
    }

    @Override
    public ArrayList getSubordinateInfo() {
        return this.subordinateList;
    }
}

分之节点IBranch

/**
 * 分之节点
 * @author shuliangzhao
 * @Title: IBranch
 * @ProjectName design-parent
 * @Description: TODO
 * @date 2019/6/18 22:38
 */
public interface IBranch {
    //获得信息
    public String getInfo();
    //增加数据节点,例如研发部下设的研发一组
    public void add(IBranch branch);
    //增加叶子节点
    public void add(ILeaf leaf);
    //获得下级信息
    public ArrayList getSubordinateInfo();
}

分之节点实现Branch

/**
 * @author shuliangzhao
 * @Title: Branch
 * @ProjectName design-parent
 * @Description: TODO
 * @date 2019/6/18 22:43
 */
public class Branch implements IBranch {

    //保存根节点下的树枝节点和树叶节点,Subordinate的意思是下级
    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;
    }

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

    @Override
    public void add(IBranch branch) {
        this.subordinateList.add(branch);
    }

    @Override
    public void add(ILeaf leaf) {
        this.subordinateList.add(leaf);
    }

    @Override
    public ArrayList getSubordinateInfo() {
        return this.subordinateList;
    }
}

叶子节点ILeaf

/**
 * 叶子节点
 * @author shuliangzhao
 * @Title: ILeaf
 * @ProjectName design-parent
 * @Description: TODO
 * @date 2019/6/18 22:39
 */
public interface ILeaf {
    //获得自己的信息
    public String getInfo();
}

Leaf实现类

/**
* @author shuliangzhao
* @Title: Leaf
* @ProjectName design-parent
* @Description: TODO
* @date 2019/6/18 22:45
*/
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;
   }
}

客户端Client

/**
 * @author shuliangzhao
 * @Title: Client
 * @ProjectName design-parent
 * @Description: TODO
 * @date 2019/6/18 22:49
 */
public class Client {

    public static void main(String[] args) {
        //首先产生了一个根节点
        IRoot ceo = new Root("Jon","总经理",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下属员工
        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());
        getAllsubordinateList(ceo.getSubordinateInfo());
    }

    private static void getAllsubordinateList(ArrayList subordinateInfo) {
        for (int i = 0;i<subordinateInfo.size();i++) {
            Object o = subordinateInfo.get(i);
            if (o instanceof Leaf) {
                Leaf leaf = (Leaf) o;
                System.out.println(leaf.getInfo());
            }else {
                IBranch iBranch = (IBranch) o;
                System.out.println(iBranch.getInfo());
                getAllsubordinateList(iBranch.getSubordinateInfo());
            }
        }
    }
}

运行结果

名称:Jon  职位:总经理  薪水: 100000
名称:张三   职位:研发部门经理   薪水: 10000
名称:赵六   职位:开发一组组长   薪水: 5000
名称:a    职位:开发人员 薪水: 2000
名称:b    职位:开发人员 薪水: 2000
名称:c    职位:开发人员 薪水: 2000
名称:张飞   职位:开发二组组长   薪水: 6000
名称:d    职位:开发人员 薪水: 2000
名称:e    职位:开发人员 薪水: 2000
名称:f    职位:开发人员 薪水: 2000
名称:唐三   职位:研发部副总    薪水: 20000
名称:李四   职位:销售部门经理   薪水: 20000
名称:h    职位:销售人员 薪水: 5000
名称:i    职位:销售人员 薪水: 4000
名称:王五   职位:财务部经理    薪水: 30000
名称:j    职位:财务人员 薪水: 5000
名称:k    职位:CEO秘书    薪水: 8000

我们这样设计看起来代码比较臃肿,而且有很多重复代码。
现在看我们更改过的设计如下:
Corp 抽象类

/**
 * @author shuliangzhao
 * @Title: Corp
 * @ProjectName design-parent
 * @Description: TODO
 * @date 2019/6/18 23:23
 */
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;
    }

}

Branch

/**
 * @author shuliangzhao
 * @Title: Branch
 * @ProjectName design-parent
 * @Description: TODO
 * @date 2019/6/18 23:26
 */
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;
    }
}

Leaf

/**
 * @author shuliangzhao
 * @Title: Leaf
 * @ProjectName design-parent
 * @Description: TODO
 * @date 2019/6/18 23:27
 */
public class Leaf extends Corp {
    public Leaf(String _name, String _position, int _salary) {
        super(_name, _position, _salary);
    }
}

客户端

/**
 * @author shuliangzhao
 * @Title: Client
 * @ProjectName design-parent
 * @Description: TODO
 * @date 2019/6/18 23:27
 */
public class Client {

    public static void main(String[] args) {
        //首先产生了一个根节点
        Branch ceo = new Branch("Jon","总经理",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下属员工
        ceo.addSubordinate(developDep);
        ceo.addSubordinate(salesDep);
        ceo.addSubordinate(financeDep);
        ceo.addSubordinate(k);
        //定义研发部门下的结构
        developDep.addSubordinate(firstDevGroup);
        developDep.addSubordinate(secondDevGroup);
        //研发部经理下还有一个副总
        developDep.addSubordinate(zhengLaoLiu);
        //看看开发两个开发小组下有什么
        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);
        System.out.println(ceo.getInfo());
        System.out.println(getTreeInfo(ceo));
    }

    private static String  getTreeInfo(Branch ceo) {
        ArrayList<Corp> subordinate = ceo.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;
    }


}

运行结果

姓名:Jon  职位:总经理  薪水:100000
姓名:张三   职位:研发部门经理   薪水:10000
姓名:赵六   职位:开发一组组长   薪水:5000
姓名:a    职位:开发人员 薪水:2000
姓名:b    职位:开发人员 薪水:2000
姓名:c    职位:开发人员 薪水:2000
姓名:张飞   职位:开发二组组长   薪水:6000
姓名:d    职位:开发人员 薪水:2000
姓名:e    职位:开发人员 薪水:2000
姓名:f    职位:开发人员 薪水:2000
姓名:唐三   职位:研发部副总    薪水:20000
姓名:李四   职位:销售部门经理   薪水:20000
姓名:h    职位:销售人员 薪水:5000
姓名:i    职位:销售人员 薪水:4000
姓名:王五   职位:财务部经理    薪水:30000
姓名:j    职位:财务人员 薪水:5000
姓名:k    职位:CEO秘书    薪水:8000
组合模式的优点

1.高层模块调用简单
2.节点自由增加

题外话,如果我们想找到每个节点的父节点我们的Corp类可以从新设计
增加一个熟悉parent
升级版Crop

/**
 * @author shuliangzhao
 * @Title: Corp
 * @ProjectName design-parent
 * @Description: TODO
 * @date 2019/6/18 23:23
 */
public abstract class Corp {

    //公司每个人都有名称
    private String name = "";
    //公司每个人都职位
    private String position = "";
    //公司每个人都有薪水
    private int salary =0;

    private Corp parent;

    public Corp getParent() {
        return parent;
    }

    public void setParent(Corp parent) {
        this.parent = parent;
    }

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

}

子节点实现


/**
 * @author shuliangzhao
 * @Title: Branch
 * @ProjectName design-parent
 * @Description: TODO
 * @date 2019/6/18 23:26
 */
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) {
        super.setParent(this);
        this.subordinateList.add(corp);
    }
    //我有哪些下属
    public ArrayList<Corp> getSubordinate() {
        return this.subordinateList;
    }
}

小礼物走一走,来简书关注我

posted @ 2019-06-19 21:41  爱在惜缘前  阅读(133)  评论(0编辑  收藏  举报