A concept,

  • Composite Pattern: Also known as the partial-whole Pattern, it creates a tree structure of object groups and groups objects into a tree structure to represent the “whole-part” hierarchical relationship
  • The composite pattern combines objects based on a tree structure, which is used to represent both part and whole hierarchies
  • The composite pattern enables users to have consistent access to both individual objects and composite objects, that is, composite allows customers to work with both individual objects and composite objects in a consistent manner
  • Simplify client operations so that clients only need to deal with consistent objects without having to worry about whole parts or leaf nodes
  • With strong scalability, when we want to change the composite object, we only need to adjust the internal hierarchy, the client does not have to make any changes
  • Easy to create complex hierarchies, the client does not care about the composition of the composition of the details, easy to add nodes or leaves to create a complex tree structure
  • Composite patterns are useful when you need to traverse an organization or work with objects that have a tree structure
  • It requires a high degree of abstraction and is not suitable to use composite patterns if leaves and nodes are very different
  • Roles and responsibilities of the Combined mode:
    • Component: This is the composite object declaration interface that implements the default interface behavior common to all classes, where appropriate, for accessing and managing Component child components, which can be abstract classes or interfaces
    • Leaf: Represents a Leaf node in a combination, which has no child nodes
    • Composite: non-leaf node, used to store child parts and implement child operations in Component, such as add and delete

Two, the application example of combination mode to solve the display of schools and departments

public abstract class OrganizationComponent {
  private String name;
  private String desc;

  public OrganizationComponent(a) {}public OrganizationComponent(String name, String desc) {
    this.name = name;
    this.desc = desc;
  }

  public abstract void add(OrganizationComponent organizationComponent);

  public abstract void remove(OrganizationComponent organizationComponent);

  public abstract void print(a);

  public String getName(a) {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public String getDesc(a) {
    return desc;
  }

  public void setDesc(String desc) {
    this.desc = desc; }}Copy the code
public class School extends OrganizationComponent {
  List<OrganizationComponent> colleges = new ArrayList<>();

  public School(a) {}public School(String name, String desc) {
    super(name, desc);
  }

  @Override
  public void add(OrganizationComponent organizationComponent) {
    colleges.add(organizationComponent);
  }

  @Override
  public void remove(OrganizationComponent organizationComponent) {
    colleges.remove(organizationComponent);
  }

  @Override
  public void print(a) {
    System.out.println(super.getName() + "" + super.getDesc());
    for(OrganizationComponent college : colleges) { college.print(); }}}Copy the code
public class College extends OrganizationComponent {
  List<OrganizationComponent> departments = new ArrayList<>();

  public College(a) {}public College(String name, String desc) {
    super(name, desc);
  }

  @Override
  public void add(OrganizationComponent department) {
    departments.add(department);
  }

  @Override
  public void remove(OrganizationComponent department) {
    departments.remove(department);
  }

  @Override
  public void print(a) {
    System.out.println(super.getName() + "" + super.getDesc());
    for(OrganizationComponent department : departments) { department.print(); }}}Copy the code
public class Department extends OrganizationComponent {
  public Department(a) {}public Department(String name, String desc) {
    super(name, desc);
  }

  @Override
  public void add(OrganizationComponent organizationComponent) {
    throw new UnsupportedOperationException();
  }

  @Override
  public void remove(OrganizationComponent organizationComponent) {
    throw new UnsupportedOperationException();
  }

  @Override
  public void print(a) {
    System.out.println(super.getName() + "" + super.getDesc()); }}Copy the code