Design Pattern _ combination pattern

Source: Internet
Author: User

Design Pattern _ combination pattern
Composite PatternCompose objects into tree structures to represent part-whole hierarchies. composite lets clients treat individual objects and compositions of objects uniformly. (The object is combined into a tree structure to represent the part-the overall hierarchy, so that the user's use of a single object and a combination object is consistent)
Unfamiliar WordsHierarchies level, Level
The uniformly is consistent and the same

Example

Link Representation

 

AnalysisThe tree, leaf node, branch node, and root node must all have a getInfo () method. All three have names, positions, and salaries. This information is temporarily passed in through the constructor. Differences: leaf nodes cannot be added to subnodes, while tree branches and root nodes can. In addition, these two classes must have a set to store the subnode objects.

 

 

Public abstract class Corp {private String name =; // each person has a private String position =; // each person has a position private int salary = 0; // everyone has a salary of public Corp (String _ name, String _ position, int _ salary) {this. name = _ name; this. position = _ position; this. salary = _ salary;} public String getInfo () {String info =; info = Name: + this. name + salary: + this. salary + position: + this. position; return info ;}}

 

Use the root node as a branch node

Public class Branch extends Corp {ArrayList
 
  
SubordinateList = new ArrayList
  
   
(); Public Branch (String _ name, String _ position, int _ salary) {super (_ name, _ position, _ salary);}/*** Add a subordinate, it may be a leader or a soldier * @ param corp */public void addSubordinate (Corp corp) {this. subordinateList. add (corp);}/*** which subordinates do I have? * @ return */public ArrayList
   
    
GetSubordinate () {return this. subordinateList ;}}
   
  
 

 

public class Leaf extends Corp { public Leaf(String _name, String _position, int _salary) {  super(_name, _position, _salary); } }

 

Test
Public class Client {public static void main (String [] args) {Branch ceo = compositeCorpTree (); System. out. println (CEO information: + ceo. getInfo (); getTreeInfo (ceo);}/*** prepare data * @ return */public static Branch compositeCorpTree () {Branch root = new Branch (Liu damachang, General Manager, 10000); Branch developDep = new Branch (Liu dazun, R & D department manager, 10000); Branch salesDep = new Branch (Ma Er, sales department manager, 20000 ); branch financeDep = new Branch, 5000); Branch secondDevGroup = new Branch (Wu dabang, Development Group Leader, 6000); // generate all the soldiers Leaf a = new Leaf (a, developer, 2000); Leaf B = new Leaf (B, developer, 2000); Leaf c = new Leaf (c, developer, 2000); Leaf d = new Leaf (d, developer, 2000); Leaf e = new Leaf (e, developer, 2000); Leaf f = new Leaf (f, developer, 2000 ); leaf g = new Leaf (g, developer, 2000); Leaf h = new Leaf (h, sales staff, 5000); Leaf I = new Leaf (I, sales staff, 4000); Leaf j = new Leaf (j, financial personnel, 5000); Leaf k = new Leaf (k, CEO Secretary, 8000); Leaf zhengLaoLiu = new Leaf (Zheng Lavi, deputy Manager of R & D department, 20000); // start assembly // The CEO has three department managers and one Secretary root. addSubordinate (k); root. addSubordinate (developDep); root. addSubordinate (salesDep); root. addSubordinate (financeDep); // R & D department manager developDep. addSubordinate (zhengLaoLiu); developDep. addSubordinate (firstDevGroup); developDep. addSubordinate (secondDevGroup); // check the firstDevGroup in the two development groups. addSubordinate (a); firstDevGroup. addSubordinate (B); firstDevGroup. addSubordinate (c); secondDevGroup. addSubordinate (d); secondDevGroup. addSubordinate (e); secondDevGroup. addSubordinate (f); // check the salesDep in the sales department. addSubordinate (h); salesDep. addSubordinate (I); // last financial financeDep. addSubordinate (j); return root;}/*** traverse all nodes * @ param root * @ return */public static void getTreeInfo (Branch root) {ArrayList
 
  
List = root. getSubordinate (); if (list! = Null) {for (Corp c: list) {if (c instanceof Leaf) {System. out. println (c. getInfo ();} else {System. out. println (root. getInfo (); getTreeInfo (Branch) c );}}}}}
 

Running result
CEO information: name: Liu Dama Zi salary: 10000 position: General Manager name: k salary: 8000 position: CEO Secretary name: Liu Dama Zi salary: 10000 position: General Manager name: Zheng la salary: 20000 position: deputy manager of the R & D department name: Liu Da min Zi salary: 10000 position: R & D department manager name: a Salary: 2000 position: developer name: B Salary: 2000 position: developer Name: c Salary: 2000 job title: developer name: Liu Da min Zi salary: 10000 job title: R & d department manager name: d salary: 2000 job title: developer name: e salary: 2000 job title: developer name: f Salary: 2000 job title: developer name: Liu damazi salary: 10000 job title: General Manager name: h Salary: 5000 job title: salesperson name: I Salary: 4000 job title: sales personnel name: Liu Dama Zi salary: 10000 position: General Manager name: j salary: 5000 position: Finance Personnel


 

WuAfter reading it, I felt that this item was too special and could not be used in other places. This pattern will occur when you see similar problems. Simply put, it is to extract the same content into an abstract class, which is based on the gourd painting.
AdvantagesThe freedom to call simple nodes is increased, which is very easy to expand. You can also write, delete, modify, and so on. It seems like the data structure is gone.
 

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.