Combined design pattern

Hey guys, today we’re going to talk about composite design patterns. That’s right, so this is the kind of combination that we often use instead of inheritance.

The composite design pattern emphasizes that “combining objects into a tree structure to represent a hierarchy of ‘parts and whole’ allows clients to work with a single object or a combination of objects in a way.”

Let’s take a look at its character composition:

  • Built-up parts (Component) : Abstract roles to provide a unified interface for objects to be composed,The function to be invoked by the client.
  • Leaves (Leaf) : indicates pair objects in a combination. Leaf nodes cannot have child nodes
  • Composite component (Composite) : Defines behavior with child nodes, used to store parts, implemented inComponentFunction in.

Let’s look at its class diagram implementation:


Let’s look at its code implementation again:

Component Component

abstract class Node {
    abstract void print(a);
}
Copy the code

Leaf node

class ConentNode extends Node {
    private String content ;
    public ConentNode(String content){ this.content = content ; }
    void print(a) {
        System.out.println(this.content);
 } } Copy the code

Composite components

class MiddleNode extends Node {
    private List<Node> listNodes = new ArrayList<>();
    private String content ;
    public List<Node> getListNodes(a){returnlistNodes; }    public MiddleNode(String content){ this.content = content ; }
 void print(a) {  System.out.println(this.content);  }  public MiddleNode addNode(Node node){  this.listNodes.add(node);  return this ;  } } Copy the code

test

public static void main(String[] args) {
        MiddleNode node = new MiddleNode("root");
        MiddleNode node1 = new MiddleNode("chapter1");
        MiddleNode node2 = new MiddleNode("chapter2");
        Node node1_1 = new ConentNode("content1_1");
 Node node1_2 = new ConentNode("content1_2");  Node node1_3 = new ConentNode("content1_3");  Node node2_1 = new ConentNode("content2_1");  Node node2_2 = new ConentNode("content2_2");  node.addNode(node1).addNode(node2);  node1.addNode(node1_1).addNode(node1_2).addNode(node1_3);  node2.addNode(node2_1).addNode(node2_2);  printNodeContent(node,0);  }   // Use this method to print the value of each node, recursively (a tree traverse)  static void printNodeContent(Node node,int depth){  // To add layers, print a -- for each layer  for (int i = 0; i < depth; i++) {  System.out.print("--");  }  node.print();  if(node instanceof MiddleNode){  MiddleNode node1 = (MiddleNode)node ;  for (Node node2 : node1.getListNodes()){  printNodeContent(node2,depth+1);  }  }  } Copy the code

The results

root
--chapter1
----content1_1
----content1_2
----content1_3
--chapter2 ----content2_1 ----content2_2 Copy the code

Ok, so that’s the end of the composite design pattern, which is essentially “by combining different objects into a tree structure, the client can be unified through the same method to call execution.”

This article is formatted using MDNICE