[design mode] combination mode

Full analysis of 23 design patterns (implemented in JAVA).

Introduction:

Composite Pattern, also known as partial overall pattern, is used to treat a group of similar objects as a single object. The combination mode combines objects according to the tree structure, which is used to represent the partial and overall levels. This type of design pattern belongs to structural pattern, which creates a tree structure of object groups.

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

We use the following example to demonstrate the usage of composite pattern. An example demonstrates the hierarchical structure of employees in an organization.

Intent:

Combine objects into a tree structure to represent a "part whole" hierarchy. The combination mode makes the user's use of single objects and combined objects consistent.

Main solutions:

In our tree structure problem, it blurs the concepts of simple elements and complex elements. The client program can deal with complex elements as simple elements, so as to decouple the internal structure of the client program and complex elements.

When to use:

1. You want to represent the part of the object - the overall hierarchy (tree structure).
2. You want users to ignore the difference between composite objects and individual objects, and users will use all objects in the composite structure uniformly.

How to solve:

The branch and leaf realize a unified interface, and the interface is combined inside the branch.

Key codes:

The interface is combined inside the tree branch, and contains the internal attribute List, in which Component is placed.

Application example:

1. An arithmetic expression includes an operand, an operator, and another operand, where the other operator can also be an operand, an operator, and another operand.
2. In JAVA AWT and SWING, the Button and Checkbox are leaves and the Container is branches.

advantage:

1. High level module calls are simple.
2. Nodes increase freely.

Disadvantages:

When using the composite pattern, the declarations of its leaves and branches are implementation classes, not interfaces, which violates the dependency inversion principle.

Usage scenario:

Partial and overall scenes, such as tree menu, file and folder management.

matters needing attention:

It is a concrete class when defined.

realization

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

UML diagram of composite pattern

Step 1

Create a list with the class Employee.

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+" ]");
   }   
}

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);
 
      //Print all employees of this organization
      System.out.println(CEO); 
      for (Employee headEmployee : CEO.getSubordinates()) {
         System.out.println(headEmployee);
         for (Employee employee : headEmployee.getSubordinates()) {
            System.out.println(employee);
         }
      }        
   }
}

Step 3

Execute the program, and the output result is:

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 ]

Keywords: Design Pattern

Added by harrison on Sun, 20 Feb 2022 11:10:13 +0200