directory
- define
- Usage scenarios
- Code implementation
define
The composite pattern, also known as the partial-whole pattern, combines objects into a tree structure to represent a partial-whole hierarchy, providing consistency between the use of single sum objects and composite objects. The composite mode allows users to combine groups of objects that have similar characteristics and treat them as one object.
Usage scenarios
- When you need to represent the partial-global structure of an object.
- Simple and complex elements are treated the same way.
- The ability to separate modules or functions from a whole.
implementation
- Ensure that the core model of the application can be represented as a tree structure. Try splitting it into simple elements and containers, and the container must be able to contain both simple elements and other containers.
- Declares a component interface and its set of methods that make sense for both simple and complex elements.
- Create a leaf node class to represent a simple element. There can be multiple different leaf node classes in a program.
- Create a container class to represent complex elements. In this class, an array member variable is created to store references to its child elements. The array must be able to hold both leaf nodes and containers, so be sure to declare it as a composite interface type. When implementing component interface methods, the host container should delegate most of the work to its children.
- Finally, define methods to add and remove child elements in the container. These operations can be declared in the component interface. This would violate interface isolation because these methods in the leaf node class are empty. However, this gives clients undifferentiated access to all elements, even those that make up a tree structure.
Code implementation
public abstract class Component {
/ / the node name
protected String name;
public Component(String name) {
this.name = name;
}
/** * concrete logic, implemented by submethods */
public abstract void doSomething(a);
/** * Add child node **@paramChild Child node */
public abstract void addChild(Component child);
/** * removes the child node **@paramChild Child node */
public abstract void removeChild(Component child);
/** * gets the child node **@paramThe index subscript *@returnChild node * /
public abstract Component getChildren(int index);
}
public class Composite extends Component {
/** * the container that stores the child nodes */
private List<Component> childs = new ArrayList<Component>();
public Composite(String name) {
super(name);
}
@Override
public void doSomething(a) {
System.out.println(name);
for(Component child : childs) { child.doSomething(); }}@Override
public void addChild(Component child) {
childs.add(child);
}
@Override
public void removeChild(Component child) {
child.removeChild(child);
}
@Override
public Component getChildren(int index) {
returnchilds.get(index); }}public class ChildComposite extends Component {
/** * the container that stores the child nodes */
private List<Component> childs = new ArrayList<Component>();
public ChildComposite(String name) {
super(name);
}
@Override
public void doSomething(a) {
System.out.println(name);
for(Component child : childs) { child.doSomething(); }}@Override
public void addChild(Component child) {
childs.add(child);
}
@Override
public void removeChild(Component child) {
child.removeChild(child);
}
@Override
public Component getChildren(int index) {
returnchilds.get(index); }}public class Leaf extends Component {
public Leaf(String name) {
super(name);
}
@Override
public void doSomething(a) {
System.out.println(name);
}
@Override
public void addChild(Component child) {
throw new UnsupportedOperationException("Also children have no children.");
}
@Override
public void removeChild(Component child) {
throw new UnsupportedOperationException("Also children have no children.");
}
@Override
public Component getChildren(int index) {
throw new UnsupportedOperationException("Also children have no children."); }}public class CompositeTest {
@Test
public void test(a) {
Composite composite = new Composite("Root");
ChildComposite childComposite = new ChildComposite("Child Root");
Leaf branch1 = new Leaf("Branch1");
Leaf branch2 = new Leaf("Branch2");
Leaf branch3 = new Leaf("Branch3");
childComposite.addChild(branch3);
composite.addChild(branch1);
composite.addChild(branch2);
composite.addChild(childComposite);
composite.doSomething();
}
}
Root
Branch1
Branch2
Child Root
Branch3
Copy the code