Iii. Combination Mode
The combination mode, also called the combination mode and tree mode, is a combination of a group of objects. These objects can be container objects and are represented as group concepts.
The purpose of the combination mode is to ensure the consistency of a single client call object in the combination object.
Involved roles:
1. Component is the object declaration interface in the combination. When appropriate, it implements the default behavior of interfaces common to all classes. Declare an interface to access and manage Component
Child parts.
2. Leaf indicates the Leaf node object in the combination, and the Leaf node does not have any child nodes.
3. Composite defines the behavior of a branch node, which is used to store sub-parts. operations related to sub-parts are implemented in the Component interface, such as adding and removing.
Applicability
The combination mode is applicable in the following situations:
1. You want to represent the part of the object-the overall hierarchy
2. You want to ignore the differences between a composite object and a single object. You will use all objects in the composite structure in a unified manner.
The following code demonstrates the idea of the combination mode:
[Java]
Package tree;
Public abstract class Tree {
/**
* Add a tree node
* @ Param tree
*/
Public abstract void add (Tree tree );
/**
* Batch add Tree nodes
* @ Param trees
*/
Public abstract void add (Tree [] trees );
/**
* Remove a tree node
* @ Param tree
*/
Public abstract void remove (Tree tree );
}
[Java]
Package cn. sunsharp. tbapp. util. tree;
Import java. util. ArrayList;
Import java. util. List;
Public class TreeNode extends Tree {
Private Long id;
Private Long parentId;
Private List <Tree> children;
Private String text;
Private String state;
Private Boolean checked;
Public static class Builder {
Private Long id;
Private Long parentId;
Private List <Tree> children = new ArrayList <Tree> ();
Private String text;
Private String state;
Private Boolean checked;
Public Builder id (Long id ){
This. id = id;
Return this;
}
Public Builder parentId (Long parentId ){
This. parentId = parentId;
Return this;
}
Public Builder children (List <Tree> children ){
This. children = children;
Return this;
}
Public Builder text (String text ){
This. text = text;
Return this;
}
Public Builder state (String state ){
This. state = state;
Return this;
}
Public Builder checked (Boolean checked ){
This. checked = checked;
Return this;
}
Public Tree build (){
Return new TreeNode (this );
}
}
@ Override
Public void add (Tree tree ){
Children. add (tree );
}
@ Override
Public void remove (Tree tree ){
Children. remove (tree );
}
@ Override
Public String toString (){
StringBuilder sb = new StringBuilder (1000 );
Sb. append ("{id :");
Sb. append (id );
Sb. append (", text :\"");
Sb. append (text );
Sb. append ("\", iconCls :");
Sb. append ("\" icon-OK \"");
Sb. append (", children :");
Sb. append (children. toString ());
Sb. append ("}");
Return sb. toString ();
}
Private TreeNode (Builder builder ){
Id = builder. id;
ParentId = builder. parentId;
Children = builder. children;
Text = builder. text;
State = builder. state;
Checked = builder. checked;
}
Public Long getId (){
Return id;
}
Public Long getParentId (){
Return parentId;
}
Public String getText (){
Return text;
}
Public String getState (){
Return state;
}
Public Boolean getChecked (){
Return checked;
}
Public List <Tree> getChildren (){
Return children;
}
@ Override
Public void add (Tree [] trees ){
For (Tree tree: trees ){
Children. add (tree );
}
}
}
[Java]
Public static void main (String [] args ){
Tree root = new TreeNode. Builder (). text ("root Node"). build ();
Tree parent1 = new TreeNode. Builder (). text ("parent node 1"). build ();
Tree parent2 = new TreeNode. Builder (). text ("parent node 2"). build ();
Root. add (parent1 );
Root. add (parent2 );
Tree son11 = new TreeNode. Builder (). text ("subnode 11"). build ();
Tree son12 = new TreeNode. Builder (). text ("subnode 12"). build ();
Parent1.add (son11 );
Parent1.add (son11 );
System. out. println (root. toString ());
}