See the author’s blog for a detailed explanation of composition patterns

“Design patterns” part 3 structural design patterns chapter 11 composition patterns (composition patterns) (A: C++ implementation)

This article is a Java implementation of the Composition pattern.

Transparent combination pattern implementation code

Step 1: Abstract artifacts

package com;

public interface Component {
	
	public void operation(a);
	public void add(Component c);
    public void remove(Component c);
    public Component getChild(int i);
    public void print(a);
    
}
Copy the code

Step 2: Leaf components

package com;

public class Leaf implements Component{

    private String name;
    public Leaf(String name)
    {
        this.name=name;
    }
    
	@Override
	public void operation(a) {
		
		System.out.println("I am "+name); 
	}

	@Override
	public void add(Component c) {}@Override
	public void remove(Component c) {}@Override
	public Component getChild(int i) {
		return null;
	}

	@Override
	public void print(a) {}}Copy the code

Step 3: Branch components

package com;

import java.util.ArrayList;

public class Composite implements Component{

    private String name;
    public Composite(String name)
    {
        this.name=name;
    }
    
	private ArrayList<Component> children=new ArrayList<Component>(); 
	
	@Override
	public void operation(a) {
		System.out.println("I am "+name); 
	}

	@Override
	public void add(Component c) {
		children.add(c);
	}

	@Override
	public void remove(Component c) {
		children.remove(c);
	}

	@Override
	public Component getChild(int i) {
        return children.get(i);
	}

	@Override
	public void print(a) {
		for(Object obj:children) { ((Component)obj).operation(); }}}Copy the code

Step 4: Test

package com;

public class Main {

	public static void main(String[] args) {

		Component Node = new Composite("Beijing Head Office");
	    Component NodeHr = new Leaf("Beijing Human Resources Department");

	    Component SubNodeSh = new Composite("Shanghai Branch");
	    Component SubNodeCd = new Composite("Chengdu Branch");
	    Component SubNodeBt = new Composite("Baotou Branch");
	    Node.add(NodeHr);
	    Node.add(SubNodeSh);
	    Node.add(SubNodeCd);
	    Node.add(SubNodeBt);
	    Node.print();
	    
	    System.out.println("-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -");
	    Component SubNodeShHr = new Leaf("Shanghai Human Resources Department");
	    Component SubNodeShCg = new Leaf("Shanghai Purchasing Department");
	    Component SubNodeShXs = new Leaf("Shanghai Sales department");
	    Component SubNodeShZb = new Leaf("Shanghai Quality supervision Department");

	    SubNodeSh.add(SubNodeShHr);
	    SubNodeSh.add(SubNodeShCg);
	    SubNodeSh.add(SubNodeShXs);
	    SubNodeSh.add(SubNodeShZb);

	    SubNodeSh.print();
	    System.out.println("-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -");
	    // The company was in a recession and needed to close the Quality supervision department in ShanghaiSubNodeSh.remove(SubNodeShZb); SubNodeSh.print(); }}Copy the code

The result is as follows:

Secure combination pattern implementation code

Step 1: Abstract artifacts

package com;

public interface Component {
	
	public void operation(a);
}
Copy the code

Step 2: Leaf components

package com;

public class Leaf implements Component{

    private String name;
    public Leaf(String name)
    {
        this.name=name;
    }
    
	@Override
	public void operation(a) {
		
		System.out.println("I am "+name); }}Copy the code

Step 3: Branch components

package com;

import java.util.ArrayList;

public class Composite implements Component{

    private String name;
    public Composite(String name)
    {
        this.name=name;
    }
    
	private ArrayList<Component> children=new ArrayList<Component>(); 
	
	@Override
	public void operation(a) {
		System.out.println("I am "+name); 
	}

	public void add(Component c) {
		children.add(c);
	}

	public void remove(Component c) {
		children.remove(c);
	}

	public Component getChild(int i) {
        return children.get(i);
	}

	public void print(a) {
		for(Object obj:children) { ((Component)obj).operation(); }}}Copy the code

Step 4: Test

package com;

public class Main {

	public static void main(String[] args) {

		Composite Node = new Composite("Beijing Head Office");
		Leaf NodeHr = new Leaf("Beijing Human Resources Department");

		Composite SubNodeSh = new Composite("Shanghai Branch");
		Composite SubNodeCd = new Composite("Chengdu Branch");
		Composite SubNodeBt = new Composite("Baotou Branch");
	    Node.add(NodeHr);
	    Node.add(SubNodeSh);
	    Node.add(SubNodeCd);
	    Node.add(SubNodeBt);
	    Node.print();
	    
	    System.out.println("-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -");
	    Leaf SubNodeShHr = new Leaf("Shanghai Human Resources Department");
	    Leaf SubNodeShCg = new Leaf("Shanghai Purchasing Department");
	    Leaf SubNodeShXs = new Leaf("Shanghai Sales department");
	    Leaf SubNodeShZb = new Leaf("Shanghai Quality supervision Department");

	    SubNodeSh.add(SubNodeShHr);
	    SubNodeSh.add(SubNodeShCg);
	    SubNodeSh.add(SubNodeShXs);
	    SubNodeSh.add(SubNodeShZb);

	    SubNodeSh.print();
	    System.out.println("-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -");
	    // The company was in a recession and needed to close the Quality supervision department in ShanghaiSubNodeSh.remove(SubNodeShZb); SubNodeSh.print(); }}Copy the code

Same result as above.