Explanation of the Combined Mode of the C ++ Design Mode
Combination of C ++ Design Patterns
The combination mode combines objects into a tree structure to represent a "part-whole" hierarchy. The combination mode ensures consistency between the use of a single object and a composite object.
I. Reasons
In nature, there are often many tree-like relationships, such as the company's structure, including subsidiaries and departments. Another example is the structure of the file system. There are directories or files under the directory, and the directories under the Directory have directories and files. This recursion goes on. WhileCombination ModeIt exists to process this tree-like relationship.
II. Implementation
Combination ModeBecause tree relationships are defined recursively, the definition of the combination mode is also recursive. The UML class diagram of the combination mode is as follows:
In combination mode, there are three roles:
Component abstract Component class. This class provides users with a unified perspective. Users can program leaves and containers without having to care about leaf or container classes.
Composite, container component class. On the one hand, container component classes can provide operations like leaf classes, and on the other hand, they can contain child components. sub-structures can be easy components or leaf classes. If the file system is used for analogy, easy component classes are directories.
Leaf, Leaf type. Leaf classes can provide function operations, but cannot accommodate sub-components. If the file system is used for analogy, the leaf node is a common file.
Iii. instance code
# Include
# Include
# Include
# Include using std: string; class Component {public: virtual ~ Component () {}; virtual void operation () = 0; virtual void add (Component * subComponent) {} virtual void remove (Component * subComponent) {} virtual Component * getChild (std: vector
: Size_type index) {return NULL ;}}; class Leaf: public Component {public: virtual ~ Leaf () {}; virtual void operation () override {std: cout <"Here is leaf" <std: endl ;}; class Composite: public Component {private: std: vector
Children; public: virtual ~ Composite () {for (auto & child: children) delete child ;}; virtual void operation () override {std: cout <"Here is composite. childen: "<std: endl; for (auto & child: children) {child-> operation () ;}; virtual void add (Component * subComponent) override {children. push_back (subComponent);} virtual void remove (Component * subComponent) override {auto ret = std: find (children. begin (), children. end (), subComponent); if (ret = children. end () return; children. erase (ret);} virtual Component * getChild (std: vector
: Size_type index) override {if (index> children. size () return nullptr; return children [index] ;}; int main (void) {Component * component = new Composite; component-> add (new Composite ); component-> add (new Composite); component-> add (new Leaf); component-> getChild (1) -> add (new Leaf); component-> getChild (1)-> add (new Leaf); component-> getChild (1)-> add (new Leaf ); component-> operation ();} running result: Here is composite. childen: Here is composite. childen: Here is composite. childen: Here is leafHere is leaf