设计模式 合成模式

  • 设计模式 - 合成模式

    合成模式用于需要以类似方式将一组对象视为单个对象的情况。合成模式根据树结构组成对象,以表示部分以及整个层次结构。这种设计模式属于结构模式,因为该模式创建了一组对象的树形结构。
    此模式创建一个包含其自己的对象组的类。此类提供了修改其相同对象组的方法。
    我们通过以下示例演示合成模式的使用,在该示例中我们将显示组织的员工层次结构。
  • 实例

    我们有一个Employee类,它充当合成模式参与者类。CompositePatternDemo,我们的演示类将使用Employee类添加部门级别的层次结构并打印所有员工。
    dp
    第1步 - 创建员工具有列表类员工的对象。 Employee.java
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class Employee {
       private String name;
       private String dept;
       private int salary;
       private List<Employee> subordinates;
    
       // constructor
       public Employee(String name,String dept, int sal) {
          this.name = name;
          this.dept = dept;
          this.salary = sal;
          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;
       }
    
       public String toString(){
          return ("Employee :[ Name : " + name + ", dept : " + dept + ", salary :" + salary+" ]");
       }   
    }
    
    第2步 - 使用Employee类创建和打印员工层次结构。CompositePatternDemo.java
    
    public class CompositePatternDemo {
       public static void main(String[] args) {
       
          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);
    
          //print all employees of the organization
          System.out.println(CEO); 
          
          for (Employee headEmployee : CEO.getSubordinates()) {
             System.out.println(headEmployee);
             
             for (Employee employee : headEmployee.getSubordinates()) {
                System.out.println(employee);
             }
          }         
       }
    }
    
    第3步 - 验证输出。
    
    Employee :[ Name : John, dept : CEO, salary :30000 ]
    Employee :[ Name : Robert, dept : Head Sales, salary :20000 ]
    Employee :[ Name : Richard, dept : Sales, salary :10000 ]
    Employee :[ Name : Rob, dept : Sales, salary :10000 ]
    Employee :[ Name : Michel, dept : Head Marketing, salary :20000 ]
    Employee :[ Name : Laura, dept : Marketing, salary :10000 ]
    Employee :[ Name : Bob, dept : Marketing, salary :10000 ]