First, UML diagram
Second, the concept
combined Mode (Composite): 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.
Third, the description
Role:
(1) Component: declares an interface for an object in a composition, and, where appropriate, implements the default behavior for all classes of common interfaces. Declares an interface for accessing and managing child parts of the component.
(2) Leaf: in the composition of white Oh is a leaf node object, the leaf node has no child nodes.
(3) Composite: defines a minor point behavior that is used to store sub-parts and to implement operations related to subassemblies in the component interface, such as adding add and removing remove.
When do I use the combo mode?
When you find that a requirement is a structure that reflects part and whole hierarchy, and that you want users to be able to ignore the difference between a combined object and a single object, you should consider combining patterns when you use all the objects in a composite structure uniformly.
What are the benefits of using the combo mode?
(1) The combination pattern defines the class hierarchy that contains the base object (Leaf) and the combined object (Composite). Basic objects can be combined into more complex objects, and this combination of objects can be combined, so that the Cabinet continues to continue, in the customer code, any use of basic objects can be used in the composition of the object.
(2) The user does not care whether to deal with a leaf node or a combination of components, it is not necessary for the definition of the combination of writing some judgment statement.
(3) in short, combined mode allows customers to consistently use composite structures and individual objects.
Iv. implementation of C + +
(1) Composite.h
#ifndef composite_h#define composite_h#include <iostream> #include <list> #include <string>// Component: Here is the abstract company class Company{protected:std::string Name;public:company (std::string name) {this->name=name;} Add node virtual void Add (company* c) =0;//Delete node virtual void remove (company* c) =0;//show virtual void display (int depth) =0;// Responsibility virtual void Lineofduty () =0;//operator overload inline BOOL operator== (const company &company) Const{return this->name== company.name;}};/ /composite: Specific company classes class Concretecompany:public company{private:std::list<company*> *children;public: Concretecompany (std::string name): Company (name) {children=new std::list<company*>;} ~concretecompany () {for (Std::list<company*>::iterator it=children->begin (); It!=children->end (); it++) Delete *it;delete children;} Add leaf node void Add (company* c) {children->push_back (c);} Delete leaf node void remove (company* c) {for (Std::list<company*>::iterator it=children->begin ();it!=children-> End (); it++) {if (**it==*c) {ChilDren->erase (it); break;}}} print void Display (int depth) {for (int i=0;i<depth;i++) std::cout<< "-"; STD::COUT<<NAME<<STD:: Endl;for (Std::list<company*>::iterator it=children->begin (); It!=children->end (); it++) (*it) Display (depth+4);} Duty void Lineofduty () {for (Std::list<company*>::iterator it=children->begin (); It!=children->end (); it+ +) (*it)->lineofduty ();}};/ /leaf: Human Resources Division class Hrdepartment:public Company{public:hrdepartment (std::string name): Company (name) {}void ADD * c) {}void Remove (company* c) {}void Display (int depth) {for (int i=0;i<depth;i++) std::cout<< "-";std::cout< <name<<std::endl;} void Lineofduty () {std::cout<<name<< "Employee recruitment training Management" <<std::endl;}};/ /leaf: Finance Department class Financedepartment:public company{public:financedepartment (std::string name): Company (name) {}void ADD (company* c) {}void Remove (company* c) {}void Display (int depth) {for (int i=0;i<depth;i++) std::cout<< "-";std::cout< <NAME<<STD:: Endl;} void Lineofduty () {std::cout<<name<< "Corporate financial Revenue Management" <<std::endl;}}; #endif
(2) Client.cpp
#include "Composite.h" #include <iostream> #include <string> #include <cstdlib>//client, client void main () {company* root=new concretecompany ("Beijing head Office") Root->add (New Hrdepartment ("Head Office Human Resources department"); Root->add (new Financedepartment ("Head Office Finance Office")); company* comp=new concretecompany ("Shanghai Huadong Branch") Comp->add (New Hrdepartment ("East China Branch Human Resources department"); Comp->add (new Financedepartment ("East China Branch office Finance Office"); Root->add (comp); company* comp1=new concretecompany ("Nanjing Office") Comp1->add (New Hrdepartment ("Human resources Department of Nanjing Office"); Comp1->add (new Financedepartment ("Nanjing Office Finance Office"); Comp->add (COMP1); company* comp2=new concretecompany ("Hangzhou Office") Comp2->add (New Hrdepartment ("Hangzhou Office Human Resources Department"); Comp2->add (new Financedepartment ("Hangzhou Office Finance Office"); Comp->add (COMP2);std::cout<< "Structure:" <<std::endl<<std::endl; Root->display (1);std::cout<<std::endl<< "Responsibilities:" <<std::endl<<std::endl;root-> Lineofduty ();d elete root;system ("pause");}
(3) operation
Big talk design pattern C + + implementation-19th Chapter-Combination mode