Design pattern of Zen design pattern-Combined mode

Source: Internet
Author: User

One: the definition of the combinatorial pattern
---> Combination mode (Composite pattern) is also called the synthesis mode, sometimes called the partial-whole mode (part-whole), which is mainly used to describe the relationship between the part and the whole.
---> Combines objects into a tree structure to represent a "partial-whole" hierarchy, which makes the user consistent with the use of individual objects and composite objects.


Two: The role of the combined pattern
Component Abstract Component roles
Define common methods and properties for participating in a composite object, and you can define some default behaviors or properties
Leaf component of leaf
The leaf object has no other branches under it, that is, the smallest unit of traversal.
Composite Branch Components
The branch object, which functions as a tree-shaped structure composed of a branch node and a leaf node.

Three: The application of combination mode

Advantages of the combined mode:
High-level module invocation simple
All the nodes in a tree-shaped structure are component, and the local and the overall are no different to the caller, that is, the high-level module does not have to care about whether it is dealing with a single object or the entire composite structure, simplifying the code of the High-level module.
Node free increase
Using the combination mode, we can see, if you want to add a branch node, leaf node is not easy, as long as the parent node to find it, very easy to expand, in line with the principle of open and close, for future maintenance is very advantageous.

Disadvantages of the combined mode:
The combination mode has a very obvious disadvantage, see our definition in the scene class, refer to the definition of the use of leaves and branches? Direct use of the implementation class! This is not appropriate for interface programming, and conflicts with dependency inversion principles, and the reader should consider it when using it, which limits the scope of your interface.

Four: The combination mode of the application scenario
Maintain and showcase parts-overall relationship scenarios, such as tree menus, file and folder management.
A scenario in which a part of a module or function can be detached from a whole.
As long as the tree structure, it is necessary to consider the use of combination mode, this must remember, as long as it is to reflect the relationship between the local and the whole time, and this relationship may be relatively deep, consider the combination mode bar.

V: Best Practices for combining patterns
There are two different implementations of the composition pattern: transparent mode and Safe mode
Transparent mode: Transparent mode is used to combine the methods put into the abstract class, such as Add (), remove () and GetChildren methods (by the way, GetChildren generally return the result is Iterable implementation class, a lot, You can see the JDK's help), regardless of whether the leaf object or the branch object has the same structure
Safe Mode: It is a completely separate branch node and leaf node, the branch node alone has a method for the combination, this method is more secure, our example uses Safe mode.

Examples of combined patterns:
-The common XML structure is also a tree-shaped structure
--Our own kinship is also a tree-shaped structure
--Department position level, page Permissions menu display search is tree structure


Six: Examples of combinatorial patterns
"1" Public abstract class

1  PackageCom.yeepay.sxf.template16;2 /**3 * Abstract class of combination mode4  * 5 * The public part of the role in the abstract model6 * For example: Common attributes, common behavior methods7  * @authorSXF8  *9  */Ten  Public Abstract classComponentcorp { One     //everyone in the company has a name. A     PrivateString name= ""; -     //everyone in the company has a position. -     PrivateString position= ""; the     //everyone in the company has a paycheck. -     Private intSalary=0; -      -     //constructors for abstract classes +      PublicComponentcorp (String name,string postion,integer salary) { -          This. name=name; +          This. position=postion; A          This. salary=salary; at     } -      -     //Get personal Information -      PublicString GetInfo () { -StringBuffer buffer=NewStringBuffer (); -Buffer.append ("Name:" + This. name+ "\ T"); inBuffer.append ("Position:" + This. position+ "\ T"); -Buffer.append ("Salary:" + This. salary+ "\ T"); to         returnbuffer.tostring (); +     } -      the}
View Code

"2" leaf node

1  PackageCom.yeepay.sxf.template16;2 /**3 * Components in combo mode4 * leaf node. Properties and methods that contain only public parts5  * @authorSXF6  *7  */8  Public classLeafextendscomponentcorp{9     Ten     //constructor Function One      PublicLeaf (string name, String postion, Integer salary) { A         Super(name, postion, salary); -     } -      the}
View Code

"3" Branch node

1  PackageCom.yeepay.sxf.template16;2 3 Importjava.util.ArrayList;4 Importjava.util.List;5 6 /**7 * Integral part of the combined mode8  * 9 * Branch NodeTen * Apart from the public part, has its own characteristic part One  * @authorSXF A  * -  */ -  Public classBranchextendsComponentcorp { the     //the node is a privilege that can have subordinate nodes (except for common attribute behavior methods, unique behavior) -     PrivateList<componentcorp> subordinatelist=NewArraylist<componentcorp>(); -     //constructor Function -      PublicBranch (string name, String postion, Integer salary) { +         Super(name, postion, salary); -     } +     //Add subordinate nodes, (other than the common attribute behavior method, unique behavior) A      Public voidaddbordinate (Componentcorp Componentcorp) { at          This. Subordinatelist.add (Componentcorp); -     } -     //Get your subordinates -      PublicList<componentcorp>getbordinate () { -         return  This. subordinatelist; -     } in      -}
View Code

"4" Test class

1  PackageCom.yeepay.sxf.template16;2 /***3 * Client Testing4  */5 Importjava.util.List;6 7 /**8 * Client Testing9  * @authorSXFTen  * One  */ A  Public classClienttest { -      Public Static voidMain (string[] args) { -         //Spawn Creep theLeaf bin1=NewLeaf ("Bing 1", "technician", 1000); -Leaf bin2=NewLeaf ("Bing 2", "Technician", 1000); -Leaf bin3=NewLeaf ("Bing 3", "salesman", 500); -Leaf bin4=NewLeaf ("Bing 4", "salesman", 500); +         //generate part of the leader -Branch order1=NewBranch ("Leader 1", "Technical manager", 10000); +Branch order2=NewBranch ("Leader 2", "Sales Manager", 8000); A         //Build Boss atBranch leard=NewBranch ("Boss", "General manager", 200000); -          -         //to combine - order1.addbordinate (bin1); - order1.addbordinate (bin2); - order2.addbordinate (bin3); in order2.addbordinate (BIN4); - leard.addbordinate (order1); to leard.addbordinate (order2); +          -         //to traverse theList<componentcorp> list=leard.getbordinate (); *          for(Componentcorp c:list) { $             Panax Notoginseng             if(cinstanceofLeaf) { -                 //It's soldier . theSystem.out.println ("Clienttest.main (soldier)" +c.getinfo ()); +}Else if(cinstanceofBranch) { A                 //is the leader theSystem.out.println ("Clienttest.main (leader)" +c.getinfo ()); +             } -         } $     } $}
View Code

Design pattern of Zen design pattern-Combined mode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.