The definition given in DP Book: grouping objects into a tree structure to represent a "partial-whole" hierarchy. The combination makes the user consistent with the use of a single object and a composite object. Note the two characters "tree". This tree-like structure is ubiquitous in real life, such as a group company, which has a parent firm with many subsidiaries. Both the parent company and the subsidiary have their own financial department, Human Resources department, sales department and so on. For the parent company, whether it is a subsidiary, or directly under the Finance Department, the Human Resources department, is its department. The entire company's departmental topology is a tree structure.
The following is a UML diagram of the combined pattern. As you can see, Financedepartment, hrdepartment two classes as leaf nodes, so there is no definition to add a function. The Concretecompany class can be used as an intermediate node, so you can add functions. So how do you add it? This class defines a list of elements to be added.
1 //Composite.cpp: Defines the entry point of the console application. 2 //3 4#include"stdafx.h"5#include <iostream>6#include <string>7#include <vector>8#include <list>9 using namespacestd;Ten One class Company A { - Public: -Company (stringname) the { -M_name =name; - } - Virtual~Company () {} + Virtual voidADD (Company *PCom) {} - Virtual voidShow (intdepth) {} + protected: A stringM_name; at }; - - classConcretecompany: Public Company - { - Public: -Concretecompany (stringname): Company (name) {} in Virtual~Concretecompany () {} - voidADD (Company *PCom) {M_listcompany.push_back (pCom);} to + voidShow (intdepth) - { the for(inti =0; i < depth; i++) *cout<<"-"; $cout<<m_name<<Endl;Panax NotoginsengList<company *>::iterator iter=M_listcompany.begin (); - for(; ITER! = M_listcompany.end (); iter++)//Show Lower Nodes the(*iter)->show (depth +2); + } A Private: theList<company*>M_listcompany; + - }; $ $ //Specific Department, finance Department - - classFinancedepartment: Public Company the { - Public:WuyiFinancedepartment (stringname): Company (name) {} the Virtual~financedepartment () {} - Virtual voidShow (intDepth//just show, add the function infinitely, because it's a leaf node. Wu { - for(inti =0; I < depth; i++) Aboutcout<<"-"; $cout<<m_name<<Endl; - } - }; - A classHrdepartment: Public Company + { the Public: -Hrdepartment (stringname): Company (name) {} $ Virtual~hrdepartment () {} the Virtual voidShow (intdepth) the { the for(inti =0; I < depth; i++) thecout<<"-"; -cout<<m_name<<Endl; in } the }; the About int_tmain (intARGC, _tchar*argv[]) the { theCompany *root =NewConcretecompany ("Head Office"); theCompany *leaf1=NewFinancedepartment ("Finance Department"); +Company *leaf2=NewHrdepartment ("HR Department"); -Root->Add (LEAF1); theRoot->Add (leaf2);Bayi //Branch a theCompany *MID1 =NewConcretecompany ("Branch a"); theCompany *leaf3=NewFinancedepartment ("Finance Department"); -Company *leaf4=NewHrdepartment ("HR Department"); -Mid1->Add (LEAF3); theMid1->Add (LEAF4); theRoot->Add (MID1); the the //Branch B -Company *mid2=NewConcretecompany ("Branch B"); theFinancedepartment *leaf5=NewFinancedepartment ("Finance Department"); theHrdepartment *leaf6=NewHrdepartment ("HR Department"); theMid2->Add (LEAF5);94Mid2->Add (LEAF6); theRoot->Add (MID2); theRoot->show (0); the 98 DeleteLEAF1;Deleteleaf2; About DeleteLEAF3;DeleteLeaf4; - DeleteLEAF5;DeleteLeaf6; 101 DeleteMID1;DeleteMid2;102 DeleteRoot;103System"Pause");104 return 0; the}
The combination mode of C + + design pattern