Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Combined mode


May 27, 2021 Design mode


Table of contents


Combined mode

Composite Pattern, also known as partial overall pattern, is used to treat a similar set of objects as a single object. T he composition pattern combines objects based on a tree structure to represent parts and the overall hierarchy. This type of design pattern belongs to the structural pattern, which creates the tree structure of the object group.

This pattern creates a class that contains its own group of objects. This class provides a way to modify the same group of objects.

Let's demonstrate the use of composite patterns with the following examples. Examples demonstrate the hierarchy of employees in an organization.

Introduced

Intent: Combine objects into a tree structure to represent a "partial-whole" hierarchy. The composition pattern makes the user's use of individual and composite objects consistent.

The main solution: It blurs the concept of simple and complex elements in our tree structure, and the client program can handle complex elements as if it were simple elements, thus decoupling the client program from the internal structure of complex elements.

When to use: 1, the part-whole hierarchy (tree structure) that you want to represent the object. 2 . You want the user to ignore the difference between a composite object and a single object, and the user will use all objects in the composite structure uniformly.

How to solve: Branches and leaves implement a unified interface, which is combined inside the branch.

Key code: The interface is combined inside the branch and contains the internal property List, which contains the Component.

Application instance: 1, arithmetic expression includes operanoms, operators, and another operanom, where another operator can also be an operation tree, an operator, and another operatic number. 2 . In JAVA AWT and SWING, for Button and Checkbox are leaves and Container is a branch.

Pros: 1, high-level module call is simple. 2 , node freedom increase.

Cons: When using combined mode, its leaves and branches are declarations of implementation classes, not interfaces, in violation of the dependency inverting principle.

Use scenes: Partial, overall scenes, such as tree menus, file, folder management.

Note: Defined as a specific class.

Realize

We have a class, Employee, which is treated as a composite model class. CompositePatternDemo, our demo class uses the Employee class to add a department hierarchy and print all employees.

Combined mode

Step 1

Create an Employee class with a list of Employee objects.

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;

   //构造函数
   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+" ]");
   }   
}

Step 2

Use the Employee class to create and print a hierarchy of employees.

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

      //打印该组织的所有员工
      System.out.println(CEO); 
      for (Employee headEmployee : CEO.getSubordinates()) {
         System.out.println(headEmployee);
         for (Employee employee : headEmployee.getSubordinates()) {
            System.out.println(employee);
         }
      }        
   }
}

Step 3

Verify the output.

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 ]