Design Patterns: Composite)
Combination Mode:
Combine objects into a tree structure to represent the relationship between parts and the whole. The combination mode allows the customer to treat a single object and a combination object in a unified manner.
Compose objects into tree structures to represent part-whole hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly.
It seems that this is a very interesting model, and the recursive thinking is also reflected here. If the business model to be considered has a tree structure, you can consider this mode.
UML diagram:
It mainly includes: <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> Examples/Eo8q91tC1xLbUz/PJ + cP3wcvSu7j2vdO/examples/samples/ztcS907/examples/samples + m6z7bUz/samples + m6z7bUz/samples/placement = "brush: java;"> # Include # Include # Include # Include using namespace std; class Component {public: Component (string n = string (""): name (n) {} virtual void add (Component * c) {} virtual void removeComponent (Component * c) {} virtual Component * getChild (int index) {} virtual void display (int depth) = 0; protected: string name ;}; class Leaf: public Component {public: Leaf (string str = ""): Component (str) {} void display (int depth) {string str (depth + 2, '-'); std: cout < : Iterator iter = children. begin (); for (; iter! = Children. end (); iter ++) (* iter)-> display (depth + 1);} Component * getChild (int index) {return children [index];} private: vector Children ;}; int main () {std: cout <"combined mode example" < Add (etc); root-> add (mnt); Component * txt1 = new Leaf ("txt1"); Component * txt2 = new Leaf ("txt2 "); etc-> add (txt1); etc-> add (txt2); root-> display (0); std: cout < RemoveComponent (txt1); root-> display (0); return 0 ;}
Here, I also reviewed the stl container deletion operation. If a pointer is provided, you can directly perform erase, but if you want to delete an object with a specific value, you can follow the following rules:
For sequential containers in the memory, such as vector, deque, and string, the erase-remove method is recommended (this method is also available for list, but list has a more efficient method)
Vector
C; // note that the remove Function c. erase (remove (c. begin (), c. end (), 25), c. end ());
For list, you can use the list member function remove, which is more efficient:
list
list_int; .... list_int.remove(25);
For associated containers, you can directly call the erase function.
map
mapContainer; ... mapContainer.erase(25);
Execution output:
The following is an example of a specific drawing program:
The Component is DrawingElement, the Leaf is the PrimitiveElement, and the Basic Drawing Element cannot contain other drawing elements. The Composite is a CompositeElement.
In fact, the above structure is a simplified version of various elements in the UI. There are various basic elements in the UI, such as buttons, text boxes, and combination elements, such as various layout controls and dialog boxes. They should be a good application of the combination mode.
UML class diagram:
C ++ implementation code:
# Include
# Include
# Include
# Include using namespace std; class DarawingElement {public: DarawingElement (string n = string (""): name (n) {} virtual void add (DarawingElement * c) {} virtual void removeDarawingElement (DarawingElement * c) {} virtual DarawingElement * getChild (int index) {} virtual void display (int depth) = 0; protected: string name ;}; class PrimitiveElement: public DarawingElement {public: PrimitiveElement (string str = ""): DarawingElement (str) {} void display (int depth) {string str (depth + 2, '-'); std: cout <
: Iterator iter = children. begin (); for (; iter! = Children. end (); iter ++) (* iter)-> display (depth + 1);} private: vector
Children ;}; int main () {std: cout <"specific combination mode, drawing program example" <
Add (redLine); pic-> add (blueCircle); pic-> add (greenBox); DarawingElement * twoCircles = new CompositeElement ("two circles "); darawingElement * blackCircle = new PrimitiveElement ("black circle"); DarawingElement * whiteCircle = new PrimitiveElement ("white Circle"); twoCircles-> add (blackCircle ); twoCircles-> add (whiteCircle); pic-> add (twoCircles); pic-> display (1); return 0 ;}
Execution output: