java combination design pattern

In Java, the composite design pattern (Composite Design Pattern) is used to combine objects into a tree structure, so that individual objects and composite objects are consistent for the client. The composite pattern is a structural design pattern that simplifies client code by making it unnecessary for the client to distinguish between individual objects and composite objects.

Composite mode contains the following main roles:

  1. Component: An object in a composition that declares an interface and, where appropriate, implements the default behavior of the interface common to all classes. Declare an interface for accessing and managing child components.

  2. Leaf: In the combination, it represents the leaf node object, and the leaf node has no child nodes.

  3. Composite: Define the behavior of those components with subcomponents, store subcomponents, and implement operations related to subcomponents in the Component interface.

Here is a simple example:

First, we create a component interface (Component):

public interface Component {     void operation(); } Then, we create a leaf class (Leaf) to implement the component interface:


public class Leaf implements Component {
    private String name;

    public Leaf(String name) {
        this.name = name;
    }

    @Override
    public void operation() {         System.out.println("Leaf " + name + " is doing something.");     } } Next, we create a composite class (Composite), implement the component interface, and store in it Subcomponents, and implement operations related to subcomponents:



import java.util.ArrayList;
import java.util.List;

public class Composite implements Component {
    private String name;
    private List<Component> children;

    public Composite(String name) {
        this.name = name;
        children = new ArrayList<>();
    }

    @Override
    public void operation() {
        System.out.println("Composite " + name + " is doing something.");
        for (Component child : children) {
            child.operation();
        }
    }

    public void addComponent(Component component) {
        children.add(component);
    }

    public void removeComponent(Component component) {         children.remove(component);     } } Now, we can use the Composite pattern to compose objects. First, create a leaf object and a composite object, then add the leaf object to the composite object, and finally call the operation of the composite object by calling the method of the composite object:



operation()

public class Main {
    public static void main(String[] args) {
        Leaf leaf1 = new Leaf("Leaf 1");
        Leaf leaf2 = new Leaf("Leaf 2");

        Composite composite1 = new Composite("Composite 1");
        composite1.addComponent(leaf1);
        composite1.addComponent(leaf2);

        Leaf leaf3 = new Leaf("Leaf 3");
        Composite composite2 = new Composite("Composite 2");
        composite2.addComponent(leaf3);
        composite2.addComponent(composite1);

        composite2.operation();
    }
}
output result:

Composite Composite 2 is doing something.
Leaf Leaf 3 is doing something.
Composite Composite 1 is doing something.
Leaf Leaf 1 is doing something.
Leaf Leaf 2 is doing something.
This example shows how the composite pattern is implemented. By creating component interfaces, leaf classes and composite classes, and storing subcomponents in the composite class, a tree-like composite structure can be created, and the client can recursively call the operation method of the composite object without knowing its specific structure, thus simplifying the client end code.

Guess you like

Origin blog.csdn.net/u010479989/article/details/131890115