Combination Mode: Combines objects into a tree structure to represent a "partial-whole" hierarchy. The combined mode makes the user consistent with the use of individual objects and composite objects. is a structural pattern
Usage Scenario: 1, for the part of the object-the overall hierarchy, such as the Tree menu, folder menu, departmental organizational chart, etc. 2, the user to hide the combination of objects and a single object, so that users uniformly use all the objects in the composite structure.
1#include <iostream>2#include <string>3#include <vector>4 5 using namespacestd;6 7 classstcomponent8 {9 Public:Ten stcomponent () One { A - } -Stcomponent (stringstrName): M_strname (strName) the { - - } - Virtual~stcomponent () + { - + } A at /* - virtual void Add (stcomponent* c); - virtual void Remove (stcomponent* c); - virtual void Display (int idepth); - */ - in - Virtual voidADD (stcomponent* c) =0; to Virtual voidRemove (stcomponent* c) =0; + Virtual voidDisplay (intidepth) =0; - the stringM_strname; * $ };Panax Notoginseng - classStleaf: Publicstcomponent the { + Public: AStleaf (stringstrName): Stcomponent (strName) the { + - } $ $ Virtual voidADD (stcomponent*c) - { -cout<<"Cann ' t Add to a leaf"<<Endl; the } - Virtual voidRemove (stcomponent*c)Wuyi { thecout<<"Cann ' t Remove from a leaf"<<Endl; - } Wu Virtual voidDisplay (intidepth) - { Aboutcout<<string(Idepth,'-') << m_strname<<Endl; $ } - }; - - classStcomposite: Publicstcomponent A { + Public: theStcomposite (stringstrName): Stcomponent (strName) - { $ the } the~Stcomposite () the { the m_vecstcomposite.clear (); - } in the Virtual voidADD (stcomponent*c) the { About M_vecstcomposite.push_back (c); the } the the Virtual voidRemove (stcomponent*c) + { - for(typeof(M_vecstcomposite.begin ()) it = M_vecstcomposite.begin (); It! =m_vecstcomposite.end ();) the {Bayi if(*it = =c) the { theit =M_vecstcomposite.erase (it); -cout<<"Erase SUCC:"<< (*it)->m_strname<<Endl; - } the Else the { the++it; the } - } the } the the Virtual voidDisplay (intidepth)94 { thecout<<string(Idepth,'-') << m_strname<<Endl; the for(size_t i =0; I < m_vecstcomposite.size (); ++i) the {98M_vecstcomposite[i]->display (idepth+1); About } - }101 102Vector<stcomponent*>M_vecstcomposite;103 };104 the intMainintargcChar*argv[])106 {107 //stleaf* pstleaf = new Stleaf ("Leafa");108 //Pstleaf->add (NULL);109 //Pstleaf->remove (NULL); the //Pstleaf->display (ten);111 //Delete pstleaf; the 113stcomposite* root =NewStcomposite ("Root"); theRoot->add (NewStleaf ("Leaf A")); theRoot->add (NewStleaf ("Leaf B")); the 117stcomposite* Comp =NewStcomposite ("Composite X");118Comp->add (NewStleaf ("Leaf XA"));119Comp->add (NewStleaf ("Leaf XB")); -Root->ADD (comp);121 122stcomposite* COMP2 =NewStcomposite ("Composite XY");123Comp2->add (NewStleaf ("Leaf XYA"));124Comp2->add (NewStleaf ("Leaf XYB")); theComp->Add (COMP2);126 127stleaf* pstleaf =NewStleaf ("Leaf D"); -Root->Add (pstleaf);129Root->display (1); the 131 //Root->remove (pstleaf); theRoot->Remove (comp);133Root->display (1);134 135 return 0;136 }137 /////////////////////////////138[Email protected] ~/learn_code/design_pattern/16_composite]$.Composite139-Root $--Leaf A141--Leaf B142--Composite X143---Leaf XA144---Leaf XB145---Composite XY146----Leaf XYA147----Leaf XYB148--Leaf D149 Erase Succ:leaf D Max-Root151--Leaf A the--Leaf B153--leaf D
Design pattern--Combined mode (c + + implementation)