This is the 17th day of my participation in the August Text Challenge.More challenges in August

Portfolio model

Group objects into a tree structure to represent a partial-whole hierarchy. The composite pattern makes the use of single objects and composite objects consistent.Copy the code

structure

  1. Abstract Component role: Its main role is to declare common interfaces for leaf and branch components and implement their default behavior. Abstract artifacts also declare interfaces to access and manage subclasses in a transparent composition pattern; Interfaces that access and manage subclasses are not declared in the secure composite mode; management is done by branch components. (General abstract class or interface, define some common methods, such as add, delete)
  2. Leaf role: A Leaf node object in a composite that has no child nodes and is used to inherit or implement abstract artifacts.
  3. Composite role/intermediary: is a branch node object in a Composite that has child nodes for inheriting and implementing abstract artifacts. Its main function is to store and manage child widgets, usually including methods like add(), remove(), getChild(), and so on.

The combined mode can be divided into transparent mode and security mode

Transparent mode

All methods of all subclasses are declared in the abstract component, so there is no need for the client to distinguish between leaf and branch objects, so it is transparent to the client.

The obvious downside of schema change is that it is not secure enough to implement methods that are not needed in a leaf object, which creates security issues.

demo

1. Abstract components

public abstract class Combination {
    public abstract void operation(a);
    public abstract void add(Combination combination);
    public abstract void remove(Combination combination);
    public abstract List<Combination> getChild(a);
}
Copy the code

2. Branch members

public class Composite extends Combination{
    private String name;
    private ArrayList<Combination> list = new ArrayList<Combination>();
    public Composite(String name) {
        this.name = name;
    }
    @Override
    public void operation(a) {
        System.out.println(this.name);
        this.list.forEach(item -> {
            item.operation();
        });
    }
    @Override
    public void add(Combination combination) {
        this.list.add(combination);
    }
    @Override
    public void remove(Combination combination) {
        this.list.remove(combination);
    }
    @Override
    public List<Combination> getChild(a) {
        return this.list; }}Copy the code

3. Leaf components

public class Leaf extends Combination{
    private String name;
    public Leaf(String name) {
        this.name = name;
    }
    @Override
    public void operation(a) {
        System.out.println(this.name);
    }
    @Override
    public void add(Combination combination) {
        / / not processing
    }
    @Override
    public void remove(Combination combination) {
        / / not processing
    }
    @Override
    public List<Combination> getChild(a) {
        / / not processing
        return null; }}Copy the code

4. Client

public class Client {
    public static void main(String[] args) {
        Composite composite = new Composite("Branch node");
        Leaf leaf = new Leaf("Leaf node");
        Leaf leaf2 = new Leaf("Leaf node 2");
        composite.add(leaf);
        composite.add(leaf2);
        composite.operation();
        // Branch node
        // Leaf node
        // Leaf node 2}}Copy the code

Safe mode

Management method of child components will be moved to the branch component, component abstract component and leaves no child object management method, a way on this avoids the security issues, but as a result of leaf and branch has a different interface, the client should know when calling the existence of the leaves and branches object, so lost its transparency

demo

1. Abstract components

public abstract class Combination {
    public abstract void operation(a);
}
Copy the code

2. Branch members

public class Composite extends Combination{
    private String name;
    private ArrayList<Combination> list = new ArrayList<Combination>();
    public Composite(String name) {
        this.name = name;
    }
    @Override
    public void operation(a) {
        System.out.println(this.name);
        this.list.forEach(item -> {
                item.operation();
        });
    }
    public void add(Combination combination) {
        this.list.add(combination);
    }
    public void remove(Combination combination) {
        this.list.remove(combination);
    }
    public List<Combination> getChild(a) {
        return this.list; }}Copy the code

3. Leaf components

public class Leaf extends Combination{
    private String name;
    public Leaf(String name) {
        this.name = name;
    }
    @Override
    public void operation(a) {
        System.out.println(this.name); }}Copy the code

4. Client

public class Client {
    public static void main(String[] args) {
        Composite composite = new Composite("Branch node");
        Leaf leaf = new Leaf("Leaf node");
        Leaf leaf2 = new Leaf("Leaf node 2");
        composite.add(leaf);
        composite.add(leaf2);
        composite.operation();
        // Branch node
        // Leaf node
        // Leaf node 2}}Copy the code

conclusion

advantages

  1. The composite pattern simplifies client code by allowing it to work consistently with both single and composite objects without caring whether it is working with a single object or a composite object.
  2. It is easier to add new objects into the composition body, and the client will not change the source code because of adding new objects, which meets the “open and close principle”.

disadvantages

  1. The design is more complex, and the client needs to spend more time sorting out the hierarchical relationships between classes.
  2. It is not easy to restrict components in containers;
  3. It is not easy to add new functions to components by inheritance.