设计模式之组合模式

简介

组合模式(Composite Pattern)也叫合成模式,有时又叫部分-整体模式,主要用来描述部分也整体的关系;主要是将对象组合成树形结构以表示“部分-整体”的层次结构,使得用户对单个和组合对象的使用具有一致性。

优点:

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

缺点:

在使用组合模式时,其叶子和树枝的声明都是实现类,而不是接口,违反了依赖倒置原则。

类图

Component:抽象构建角色,定义参加组合对象的共有方法和属性,可以定义一些默认的行为和属性,
Leaf:叶子构件,叶子对象,下面再也没有其他的分支。
Composite:树枝构件,树枝对象,它的作用是组合树枝节点和叶子节点形成一个树形结构。

例:一个公司中员工的结构就能用该模式来表示;类图:

java实现

创建抽象的公司职员类

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(){
            StringBuffer sb = new StringBuffer();
            sb.append("姓名:"+this.name);
            sb.append("\t职位:"+this.position);
            sb.append("\t薪水:"+this.salary);
            return sb.toString();
        }
    }

创建树叶节点

public class Leaf extends Corp {
        public Leaf(String name, String position, int salary) {
            super(name, position, salary);
        }
    }

创建树枝节点

public class Branch extends Corp {
        //该员工下的员工
        protected List<Corp> subordinateList = new ArrayList<>();
        public Branch(String name, String position, int salary) {
            super(name, position, salary);
        }
        //添加下属员工
        public void addSubordinate(Corp corp){
            this.subordinateList.add(corp);
        }
        //得到下属员工
        public List<Corp> getSubordinate() {
            return this.subordinateList;
        }
    }

场景类

    public class Client {
        //添加公司员工信息
        public static Branch createCorpTree(){
            Branch root = new Branch("John","CEO", 30000);

            Branch decelopeDep = new Branch("Robert","研发部经理", 20000);
            Branch salesDep = new Branch("Michel","销售部经理", 20000);

            Branch firstGroup =  new Branch("Laura","开发一组组长", 10000);
            Branch secondGroup =  new Branch("Bob","开发二组组长", 10000);

            Leaf a = new Leaf("a","开发人员",5000);
            Leaf b = new Leaf("b","开发人员",5000);
            Leaf c = new Leaf("c","开发人员",5000);
            Leaf d = new Leaf("d","开发人员",5000);
            Leaf e = new Leaf("e","销售人员",5000);
            Leaf f = new Leaf("f","销售人员",5000);
            //组装员工
            root.addSubordinate(decelopeDep);
            root.addSubordinate(salesDep);

            salesDep.addSubordinate(e);
            salesDep.addSubordinate(f);

            decelopeDep.addSubordinate(firstGroup);
            decelopeDep.addSubordinate(secondGroup);

            firstGroup.addSubordinate(a);
            firstGroup.addSubordinate(b);
            secondGroup.addSubordinate(c);
            secondGroup.addSubordinate(d);

            return root;
        }
        //遍历整棵树
        public static String getTreeInfo(Branch root){
            List<Corp> subordinate = root.getSubordinate();
            StringBuffer sb = new StringBuffer();
            for(Corp s :subordinate){
                if(s instanceof Leaf){
                    sb.append(s.getInfo()+"\n");
                }else{
                    sb.append("=================\n"+s.getInfo()+"\n"+getTreeInfo((Branch) s));
                }
            }
            return sb.toString();
        }

        public static void main(String[] args){
            //首先创建出公司员工结构
            Branch corpTree = createCorpTree();
            System.out.println(corpTree.getInfo());
            System.out.println(getTreeInfo(corpTree));
        }
    }

整个公司员工的结构如下:

遍历结果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值