Design Patterns - Composition Patterns

Composite Pattern - Composite Pattern

Composite Pattern: Combine multiple objects to form a tree structure to represent a hierarchy with a "whole-part" relationship. The composition pattern is consistent in the use of single objects (ie, leaf objects) and composite objects (ie, container objects). The composition pattern can also be called the "Part-Whole" pattern, which is an object-structured pattern. .

My own understanding: For a tree structure, the container and child nodes have the same methods (both inherited from abstract components). In the following example, because there are no methods such as add remove in leaves, abstract leaves and abstract containers are used, which is safe Combination mode.

 Abstract component: It can be an interface or an abstract class, declares the interface for the leaf component and container component object, and can include the declaration and implementation of the common behavior of all subclasses in this role. The methods of accessing and managing its sub-components are defined in the abstract component, such as adding sub-components, deleting sub-components, obtaining sub-components and so on.

● Leaf component: It represents the leaf node object in the composite structure, the leaf node has no child nodes, and it implements the behavior defined in the abstract component. For those methods of accessing and managing subcomponents, exceptions can be handled.

Container  component: It represents the container node object in the composite structure. The container node contains child nodes, and its child nodes can be leaf nodes or container nodes. It provides a collection for storing child nodes, which is implemented in the abstract component. The defined behavior, including those methods for accessing and managing subcomponents, in its business methods can recursively call the business methods of its child nodes.

      The key to the composition mode is to define an abstract component class, which can represent both leaves and containers. The client can program the abstract component class without knowing whether it represents a leaf or a container. It can be unified. deal with


Abstract component:

public interface IComponent {
    void show();
}

Abstract leaf component:

public abstract class Single implements IComponent {
    @Override
    public abstract void show();
}

Specific leaf components:

public class Button extends Single {
    @Override
    public void show() {
        System.out.println("Draw button");
    }
}
public class CheckBox extends Single {
    @Override
    public void show() {
        System.out.println("Draw selection bar");
    }
}

Abstract container component:

public class SingleContent implements IComponent {

    private List<IComponent> singlelist = new ArrayList<>();

    protected String ContentName = null;

    public void setContentName(String CN){
        this.ContentName = CN;
    }


    public void show(){
        System.out.println(ContentName + "---->");
        singlelist.forEach((v)->v.show());
    }

    public void add(IComponent ic){
        singlelist.add(ic);
    }

    public void remove(IComponent ic){
        singlelist.remove(ic);
    }
}
Specific container components:

public class CenterPanel extends SingleContent {

    public CenterPanel(){
        super.setContentName("CenterPanel");
    }
}
public class Window extends SingleContent {

    public Window(){
        super.setContentName("Windows");
    }

}

Client:

public class Client {

    public static void main(String[] args) {
        Single s1,s2;
        SingleContent c1,c2;

        s1 = new Button();
        s2 = new CheckBox();
        c1 = new Window();
        c2 = new CenterPanel();


        c1.add(s1);
        c1.add(s2);

        c2.add(s1);
        c2.add(s1);

        c1.show();
        c2.show();
    }
}





Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326852304&siteId=291194637