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=[]}]}]}
     */
相关推荐
在未来等你18 小时前
AI Agent设计模式 Day 5:Reflexion模式:自我反思与持续改进
设计模式·llm·react·ai agent·plan-and-execute
程序员三藏18 小时前
快速弄懂POM设计模式
自动化测试·软件测试·python·selenium·测试工具·设计模式·职场和发展
Lei_33596718 小时前
[設計模式]設計模式的作用
设计模式
将编程培养成爱好19 小时前
C++ 设计模式《统计辅助功能》
开发语言·c++·设计模式·访问者模式
乙己4071 天前
设计模式——桥接模式(bridge)
设计模式·桥接模式
WKP94181 天前
原型设计模式
java·设计模式
guangzan1 天前
常用设计模式:工厂方法模式
设计模式
桦说编程2 天前
Guava 迭代器增强类介绍
java·后端·设计模式
李宥小哥2 天前
创建型设计模式1
stm32·嵌入式硬件·设计模式