23种设计模式----组合模式

组合模式

无需关系处理的单个对象,还是组合的对象容器,实现容器之间的解耦合。当有新部件时容易添加进来。

1. 可以想象成树结构

根节点(最上层)

public interface IRoot {  //根节点接口
    //得到总经理的信息
    public String getInfo();
    //总经理下边要有小兵, 那要能增加小兵, 比如研发部总经理, 这是个树枝节点
    public void add(IBranch branch);
    //那要能增加树叶节点
    public void add(ILeaf leaf);
    //既然能增加, 那还要能够遍历, 不可能总经理不知道他手下有哪些人
    public ArrayList getSubordinateInfo();
}

2.分支节点(中间)

public interface IBranch {  //分支节点

    //获得信息
    public String getInfo();
    //增加数据节点, 例如研发部下设的研发一组
    public void add(IBranch branch);
    //增加叶子节点
    public void add(ILeaf leaf);
    //获得下级信息
    public ArrayList getSubordinateInfo();
}

3.叶子节点(最下层) 

public interface ILeaf {  //叶子节点
    //获得自己的信息
    public String getInfo();
}

4.实现 

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

 

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

 

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;
    }
}
public class Test {
    public void test() {
        //首先产生了一个根节点
        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();
        //定义一个ArrayList长度, 不要在for循环中每次计算
        for (int m = 0; m < length; m++) {
            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());
            }
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值