Portfolio model

The objectCombine into a tree structureTo indicate thatParts and wholeThe hierarchy of. The composite pattern enables users to use single objects and composite objects consistently.

A, role,

1.Com Ponent is an object declaration interface in a composite that, where appropriate, implements the default behavior of interfaces common to all classes. Declare an interface for accessing and managing Component children. 2.Leaf represents the Leaf node object in the combination, and Leaf node has no children. Composite defines the dot behavior that stores subcomponents and implements subcomponent-related operations, such as add and remove, in the Component interface.

The class diagram

Two, code implementation

2.1 Abstraction of managed objects in a portfolio

/** Component : An interface declared by an object in a composite that implements the default behavior of interfaces common to all classes, where appropriate, declaring an interface for accessing and managing Component child */ public Abstract Class Component {private String name; public Component(String name) { this.name = name; } public String getName() { return name; } /** * add child * @param component */ public void add(component component); Public void fetch(); public void fetch(); @param component */ public void remove(component component); }Copy the code

2.2. Composite classes, which define the behavior with branches, are used to store child parts

import java.util.ArrayList; import java.util.List; /** combination class: Public class extends Component{private List<Component> childs = new ArrayList<Component>(); public Composite(String name) { super(name); } @Override public void add(Component component) { childs.add(component); } @override public void fetch() {system.out.println (" node :\t"+this.getName());} @override public void fetch() {system.out.println (" node :\t"+this.getName()); For (Component Component: childs) {component.fetch(); } } @Override public void remove(Component component) { childs.remove(component); }}Copy the code

2.3. Leaf nodes, no children

Public class Leaf extends Component{public Leaf(String name) {super(name); } @Override public void add(Component component) { // TODO Auto-generated method stub } @Override public void fetch() { // TODO auto-generated method stub system.out.println (" leaf node :"+this.getName())); } @Override public void remove(Component component) { // TODO Auto-generated method stub } }Copy the code

2.4, tests,

Public class Test {public static void main(String[] args) {Component root = new Composite(" root "); Component child = new Composite(" Component child"); Component child_1 = new Leaf(" a"); Component child_2 = new Leaf(" b"); Component child_2 = new Leaf(" B "); child.add(child_1); child.add(child_2); Component child2 = new Composite(" child2"); root.add(child); root.add(child2); root.fetch(); }} Result: Node name: root node Node name: level-1 child child node leaf node: level-1 child node A Leaf node: Level-1 child node B child node name: level-1 child node child2Copy the code

Three, applicable scenarios

1, when the demand is found to reflect the part and the wholehierarchyWhen you want users to use all objects in a composite structure uniformly, regardless of the differences between a composite object and a single object (consistency), you should consider the composite pattern.

Four,

The composite pattern decouples the client from the internal structure of complex elements, allowing the client to process complex elements as if they were simple elements. If you want to create a hierarchy where all elements are treated the same way, the composite pattern is ideal.