What is composite mode

The “composite pattern” is also known as the “partial whole pattern”, which is one of the structural patterns.

Before explaining this pattern, let’s familiarize ourselves with “tree structure.” If you are not familiar with the “tree structure”, you can think of a tree. A tree has roots, branches and leaves respectively. It happens that the tree structure also has root nodes, child nodes (non-leaf nodes) and leaf nodes.

The combination mode uses the tree structure. The core idea of this mode is to combine multiple objects into a tree structure to represent the hierarchical relationship between “whole and part”.

Composite pattern UML class diagram

UML class diagram:

Component: The “root node” in the composite pattern can be an interface, an abstract class, or a generic class that defines all the common content of its subclasses and has methods for accessing and managing its children.

2. Leaf: Leaf node in the combination, that is, the last node, and there will be no child nodes under this node. 3. Composite: non-leaf node, which is used to store sub-components and realize relevant operations on sub-components in Composite.

Learn composition patterns through case studies

Case: The boss of a company needs to develop a personnel and department management system, which can flexibly add, delete and display departments and employees under departments. At this point we can do this by combining patterns.

Organization => Corresponding Component role

public abstract class OrganizationComponent {

  / / name
  private String name;

  / / description
  private String describe;

  /** * display method * This method is required for both child and leaf nodes, so it is designed as an abstract method to be implemented by subclasses. * /
  public abstract void show(a);

  /** * New method * Since the leaf node does not need this method, write the method as an empty implementation method so that the leaf node is not forced to implement an unwanted method when inheriting the class. * /
  public void add(OrganizationComponent component) {
    throw new UnsupportedOperationException();
  }

  /** * The delete method * is designed to be an empty implementation like the add() method. * /
  public void remove(OrganizationComponent component) {
    throw newUnsupportedOperationException(); }... Get (), set(), parameter constructor}Copy the code

Company => Composite role

public class Company extends OrganizationComponent {

  // Components is used to manage the departments under the company, that is, the child nodes under the company.
  private List<OrganizationComponent> components = newArrayList<>(); . Parameterized constructor/** * override the add method */
  @Override
  public void add(OrganizationComponent component) {
    this.components.add(component);
  }

  /** * override delete method */
  @Override
  public void remove(OrganizationComponent component) {
    this.components.remove(component);
  }

  /** * implements the display method */
  @Override
  public void show(a) {
    // Print the company name
    System.out.println("= = = = = =" + "Name:" + this.getName() + "Description:" + this.getDescribe() + "= = = = = =");
    // Prints the child node under the company
    for(OrganizationComponent component : components) { component.show(); }}}Copy the code

Department => Corresponds to the Composite role

public class Department extends OrganizationComponent {

  // Components is used to manage the employees of the department, that is, the child nodes of the department.
  private List<OrganizationComponent> components = newArrayList<>(); . Parameterized constructor/** * override the add method */
  @Override
  public void add(OrganizationComponent component) {
    this.components.add(component);
  }

  /** * override delete method */
  @Override
  public void remove(OrganizationComponent component) {
    this.components.remove(component);
  }

  /** * implements the display method */
  @Override
  public void show(a) {
    // Prints the department name
    System.out.println("= = = = = =" + "Name:" + this.getName() + "Description:" + this.getDescribe() + "= = = = = =");
    // Print the child node under the department
    for(OrganizationComponent component : components) { component.show(); }}}Copy the code

Employee => Corresponds to the Leaf role

public class Workers extends OrganizationComponent {... Parameterized constructor/** * implements the display method */
  @Override
  public void show(a) {
    System.out.println("Name:" + this.getName() + "Description:" + this.getDescribe()); }}Copy the code

Client test classes

public class Client {

  public static void main(String[] args) {
    / / the company
    OrganizationComponent company = new Company("Little Rookie Company."."The company that never works overtime.");
    / / department
    OrganizationComponent department = new Department("R&d Department"."An amazing department.");
    // Add a department under the company
    company.add(department);
    / / employee
    department.add(new Workers("Daniel"."Touch the fish at work."));
    department.add(new Workers("Rookie One."."Every day the CRUD"));
    / / outputcompany.show(); }}Copy the code

The execution result

conclusion

1. The use of composite mode allows users to process individual objects and composite objects in a unified way throughout the tree structure, thus simplifying the operation of the client.

2. The composite mode has strong scalability. When we want to change the composite object, we only need to adjust the internal hierarchical relationship, and the client does not need to make any changes.

3. Clients can create complex tree structures by adding nodes and leaves without worrying about the details of the composition.

4. Consider using composite mode when you need to work with a tree structure.

5. It is not recommended to use the combined mode when there is a big difference between nodes and leaf nodes.

This is the end of today’s sharing. If you feel that the article written by “newbie” is still good, rememberLike and followYo! Your support is what keeps me going. Article where to write problems also hope that you can point out, I will be modestly taught.

<>