Composite: combines objects into a tree structure to represent the "Whole-part" hierarchy. Composite makes the use of a single object and a composite object consistent.
UML class diagram:
Mode description:
Component is the object declaration interface in the combination. When appropriate, it implements the default behavior of all class common interfaces. Declare an interface to access and manage component sub-parts. Leaf indicates the leaf node object in the combination, and the leaf node does not have any child nodes. Composite defines the behavior of a branch node to store sub-parts. It implements operations related to sub-parts in the component interface, such as adding and removing. The composite mode is applicable in the following cases: 1. you want to represent the part of the object-the overall hierarchy 2. you want to ignore the differences between a composite object and a single object. You will use all objects in the composite structure in a unified manner. Basic code:
/*************************************** * **************************** Filename: composite. hcreated: 2013-01-24author: firehoodpurpose: the design mode of firehood-combination mode ******************************** * **********************************/# pragma once # include <iostream> # include <string> # include <list> using namespace std; // composite object (tree structure) class Component {public: Component (string name): m_strName (name) {} virtual ~ Component (void) {} public: virtual void add (Component * pChild) {} virtual void remove (Component * pChild) {} virtual Component * getChild (int index) {return NULL;} virtual void operation (int depth) = 0; protected: string m_strName;}; // Leaf node class Leaf: public Component {public: Leaf (string name ): component (name) {} virtual ~ Leaf (void) {} public: virtual void operation (int depth) {for (int I = 0; I <depth; I ++) {cout <"--";} cout <m_strName <endl ;}; // class Composite: public Component {public: Composite (string name): Component (name) {m_ComponentList.clear ();} virtual ~ Composite (void) {for (list <Component *>: iterator iter = m_ComponentList.begin (); iter! = M_ComponentList.end (); iter ++) {delete (* iter) ;}} public: virtual void add (Component * pChild) {if (pChild! = NULL) {m_ComponentList.push_back (pChild) ;}} virtual void remove (Component * pChild) {if (pChild = NULL) {m_ComponentList.remove (pChild );}} virtual Component * getChild (int index) {list <Component *>: iterator iter; int I = 0; for (iter = m_ComponentList.begin (); iter! = M_ComponentList.end (); iter ++) {if (index = I ++) {return (* iter) ;}} return NULL;} virtual void operation (int depth) {for (int I = 0; I <depth; I ++) {cout <"--" ;}cout <m_strName <endl; list <Component *> :: iterator iter; for (iter = m_ComponentList.begin (); iter! = M_ComponentList.end (); iter ++) {(* iter)-> operation (depth + 1) ;}} private: list <Component *> m_ComponentList ;};
Client call code:
# Include "Composite. h "# include <iostream> using namespace std; int main (int argc, char * argv []) {cout <"*********************************** ** "<endl; cout <"firehood learning design mode-combination mode" <endl; cout <"************************************ * "<endl; composite root ("root"); root. add (new Leaf ("Leaf A"); root. add (new Leaf ("Leaf B"); Composite branch ("branch A"); branch. add (new Leaf ("Leaf X"); branch. add (new Leaf ("Leaf Y"); root. add (& branch); Composite branch2 ("branch B"); branch2.add (new Leaf ("Leaf X"); branch2.add (new Leaf ("Leaf Y ")); branch. add (& branch2); root. add (new Leaf ("Leaf C"); root. operation (1); system ("pause"); return 0 ;}
Execution result:
*************************************
Firehood learning design mode-Combination Mode
*************************************
-- Root
---- Leaf
---- Leaf B
---- Branch
------ Leaf X
------ Leaf y
------ Branch B
-------- Leaf x
-------- Leaf y
---- Leaf C
Press any key to continue...