The English version of the combination mode is: Compose objects into the tree structures to represent part-whole hiearachies. Composite lets clients treat individual objects and compositions of objects uniformly. This means that the object is grouped into a tree structure to represent a "partial-whole" hierarchy, making the user consistent in the use of individual objects and grouped objects.
The combo mode has three roles:
Abstract widget (component) Role: Defines the common methods and properties that participate in a grouped object.
Leaf widget (leaf) role: The role is a leaf object with no branches under it, that is, no child nodes.
Digital widget (Composite) Role: The role represents a grouped, branched, branch object that combines branches and leaves into a tree structure and defines methods for managing child objects.
Combination Mode class Diagram:
Implementation code corresponding to each class:
Code for Abstract Artifacts:
Package com.zz.composite;
/**
* Abstract Component Interface
* Copyright April 25, 2015
* created by TXXS * All right reserved/public
interface Co mponent {public
void operation ();
}
Branch Structure Code:
Package com.zz.composite;
Import java.util.ArrayList;
/**
* Defines the branch widget
* Copyright April 25, 2015
* created by TXXS * All right reserved/public
class Compos ITE implements Component {
//widget container
private arraylist<component> componentlist = new arraylist< Component> ();
Add widget
public void Add (Component Component) {
this.componentList.add (Component);
}
Delete widget public
void Remove (Component Component) {
this.componentList.remove (Component);
}
Gets the child widget public
Arraylist<component>getchild () {return
this.getchild ();
}
@Override public
void operation () {
//Business logic code
}
}
Leaf Structure Code:
Package com.zz.composite;
/**
* Definition leaf widget
* Copyright April 25, 2015
* created by TXXS
* All right reserved/public
class Leaf implements Component {
@Override public
void operation () {
//Salesman logical Code
}
}
Advantages of Combinatorial mode:
1, the call is simple, the user does not have to care whether they are dealing with a single object or a combination of objects.
2, flexible add nodes, want to increase the branch or leaf node as long as you need to find the parent node, you do not have to add the object parts and modify the code
Combination mode for the scenario:
1. Describe the part of the object and the hierarchical structure of the whole.
2. When you need to ignore the difference between individual parts and composite components
SOURCE download