Combination mode
1. Definition
Groups objects into tree structures to represent part-whole hierarchies. The composite mode allows users to use individual objects and composite objects consistently.
2. Sample code
Commodity classification tree structure, so that categories and leaf nodes are accessed through a unified component object.
/*Abstract component object, declare an interface for the object in the composition, and implement the default behavior of the interface*/ public abstract class Component{ // output the name of the component itself public abstract void printStruct(String preStr); /* Add the component object to the composite object */ public void addChild(Component child){ throw new UnsupportedOperationException("Object does not support this method"); } /* remove component object */ public void removeChild(Component child){ throw new UnsupportedOperationException("Object does not support this method"); } /* Get the component object according to the index */ public Component getChildren(int index){ throw new UnsupportedOperationException("Object does not support this method"); } }
/* Leaf object implementation */ public class Leaf extends Component{ // leaf object name private String name = ""; public Leaf(String name){ this.name = name; } //output leaf object structure public void printStruct(String preStr){ System.out.println(preStr + "-" + name); } } /* Combined object implementation, can contain other combined objects and leaf nodes */ public class Composite extends Component{ //Used to store the subcomponent objects contained in the composite object private List<Component> childComponents = null; //combine object name private String name = ""; public Composite(String name){ this.name = name; } public void addChild(Component child){ if(childComponents == null){ childComponents = new ArrayList<Component>(); } childComponents.add(child); } public void printStruct(String preStr){ // first output yourself System.out.println(preStr+ "-" + name); //If it also contains a self-component object, then output the child component object if(this.childComponents != null){ //add space for indentation preStr += " "; // output child component object for(Component c : childComponents){ c.printStruct (preStr); } } } }
/* Client operations do not need to distinguish between leaf nodes and composite objects */ public class Client{ public static void main(String args[]){ //define all composite objects Component root = new Component("服装"); Component c1 = new Component("男装"); Component c2 = new Component("女装"); //define all leaf objects Component leaf1 = new Leaf("Shirt"); Component leaf2 = new Leaf("jacket"); Component leaf3 = new Leaf("skirt"); Component leaf4 = new Leaf("Set"); // Combine objects and leaf objects according to the structure root.addChild(c1); root.addChild(c2); c1.addChild(leaf1); c1.addChild(leaf2); c2.addChild(leaf3); c2.addChild(leaf4); //Call the root object to output the entire tree root.printStruct(""); } }
3. Practical application
The purpose of the composition mode is to allow the client to operate in a unified way instead of distinguishing between the composition object and the leaf object. The key to achieving this goal is to design an abstract component class that can represent the composition object. And leaf objects, which also means that all functions that can be described or manipulated using the object tree can be considered using composition mode, such as reading xml files, or parsing statements.
The essence of the composition pattern: unifying leaf objects and composition objects