23种设计模式-组合模式

  • 主要解决:它在我们树型结构的问题中,模糊了简单元素和复杂元素的概念,客户程序可以像处理简单元素一样来处理复杂元素,从而使得客户程序与复杂元素的内部结构解耦。

以创建和打印员工的层次结构为例,最小单元示例:

  • 1、创建 Employee 类,该类带有 Employee 对象的列表。

    java 复制代码
    public class Employee {
    
        private String name;
        // 部门
        private String dept;
        // 工资
        private int salary;
        // 员工 list
        private List<Employee> subordinates;
    
        public Employee(String name, String dept, int salary) {
            this.name = name;
            this.dept = dept;
            this.salary = salary;
            this.subordinates = new ArrayList<Employee>();
        }
    
        public void add(Employee e) {
            subordinates.add(e);
        }
    
        public void remove(Employee e) {
            subordinates.remove(e);
        }
    
        public List<Employee> getSubordinates() {
            return subordinates;
        }
    
        @Override
        public String toString() {
            return "Employee{" +
                    "name='" + name + '\'' +
                    ", dept='" + dept + '\'' +
                    ", salary=" + salary +
                    ", subordinates=" + subordinates +
                    '}';
        }
    }

    2.使用 Employee 类来创建和打印员工的层次结构。

    java 复制代码
    final Employee ceo = new Employee("John", "CEO", 30000);
    
    Employee headSales = new Employee("Robert", "Head sales", 20000);
    
    Employee headMarketing = new Employee("Michel", "Head Marketing", 20000);
    
    Employee clerk1 = new Employee("Laura", "Marketing", 10000);
    Employee clerk2 = new Employee("Bob", "Marketing", 10000);
    
    Employee salesExecutive1 = new Employee("Richard", "Sales", 10000);
    Employee salesExecutive2 = new Employee("Rob", "Sales", 10000);
    
    ceo.add(headSales);
    ceo.add(headMarketing);
    
    headSales.add(salesExecutive1);
    headSales.add(salesExecutive2);
    
    headMarketing.add(clerk1);
    headMarketing.add(clerk2);
    
    Log.e("---", ceo.toString());
    
    // 打印
    /*
     * Employee{name='John', dept='CEO', salary=30000,
     * subordinates=[Employee{name='Robert', dept='Head sales', salary=20000,
     * subordinates=[
     * Employee{name='Richard', dept='Sales', salary=10000, subordinates=[]},
     * Employee{name='Rob', dept='Sales', salary=10000, subordinates=[]}]},
     * Employee{name='Michel', dept='Head Marketing', salary=20000,
     * subordinates=[Employee{name='Laura', dept='Marketing', salary=10000, subordinates=[]},
     * Employee{name='Bob', dept='Marketing', salary=10000, subordinates=[]}]}]}
     */
相关推荐
phdsky2 小时前
【设计模式】策略模式
设计模式·策略模式
烛阴2 小时前
【TS 设计模式完全指南】构建你的专属“通知中心”:深入观察者模式
javascript·设计模式·typescript
Mr_WangAndy4 小时前
C++设计模式_创建型模式_原型模式Prototype
c++·设计模式·原型模式
哆啦code梦5 小时前
设计模式之代理模式-骆驼与巴巴羊的故事
设计模式·代理模式
贝塔实验室5 小时前
ADMM 算法的基本概念
算法·数学建模·设计模式·矩阵·动态规划·软件构建·傅立叶分析
Chan167 小时前
【 设计模式 | 结构型模式 代理模式 】
java·spring boot·后端·设计模式·intellij-idea
大飞pkz10 小时前
【设计模式】适配器模式
开发语言·设计模式·c#·适配器模式
Meteors.10 小时前
23种设计模式——组合模式(Composite Pattern)
设计模式·组合模式
大飞pkz10 小时前
【设计模式】外观模式
开发语言·设计模式·c#·外观模式
青草地溪水旁10 小时前
设计模式(C++)详解——解释器模式(2)
c++·设计模式·解释器模式