Decoration Mode
By dynamically adding some additional responsibilities to an object, the decoration mode is more flexible than the subclass generation.
General implementation code
- Class component
- {
- Public:
- // Pure virtual class
- Virtual void operation () = 0;
- };
- Class concretecomponent: public component
- {
- Public:
- Virtual void operation ()
- {
- Cout <"actions on specific objects" <Endl;
- }
- };
- Class decorator: Component
- {
- Protected:
- Component * component;
- Public:
- Void setcomponent (component * component)
- {
- This-> compontent = component;
- }
- // Rewrite operation (). The actual execution is the operation () of component ()
- Virtual void operation ()
- {
- Component-> operation ();
- }
- }
- Class concretedecoratora: decorator
- {
- PRIVATE:
- // Unique functions of this class, different from concretedecoratorb
- String addedstate;
- Public:
- Virtual void operation ()
- {
- // First run the opretion () of the original component, and then execute the functions of this class, such as addedstate, which is equivalent to decorating the original component.
- Component: Operation ();
- Addedstate = "new state ";
- Cout <"operations on decoration object ";
- }
- }
- Class concretedecoratorb: decorator
- {
- Public:
- Virtual void operation ()
- {
- // First run the opretion () of the original component, and then execute the functions of this class, such as addedbehavior (), which is equivalent to decorating the original component.
- Component: Operation ();
- Addedbehavior ();
- Cout <"operations on decoration object B ";
- }
- PRIVATE:
- Void addedbehavior (){}
- }
- Int main ()
- {
- Concretecomponent * c = new concretecomponent;
- Concretedecoratora * D1 = new concretecomponent;
- Concretedecoratorb * D2 = new concretecomponent;
- // The decoration method is: first use concretecomponent to instantiate Object C, and then use concretedecoratora's instantiated object D1 to Package C,
- // Use the concretedecoratorb's instantiated object D2 to wrap D1, and finally execute the D2 operation ()
- D1-> setcomponent (C );
- D2-> setcomponent (D1 );
- D2-> operation ();
- Return 0;
- }
The decoration mode uses setcomponent to encapsulate objects, so that the implementation of each decoration object is separated from how to use this object,
Each decoration object only cares about its own functions and does not need to be added to the object.
A specific implementation code:
- # Include <iostream>
- # Include <string>
- Using namespace STD;
- Class person
- {
- Public:
- Person (){};
- Person (string name)
- {
- This-> name = Name;
- }
- Virtual void show ()
- {
- Cout <"Dress Up" <name <Endl;
- }
- PRIVATE:
- String name;
- };
- Class finery: Public Person
- {
- Protected:
- Person * component;
- // Dress up
- Public:
- Void decorate (person * component)
- {
- This-> component = component;
- }
- Virtual void show ()
- {
- Component-> show ();
- }
- };
- // Concretedecorator)
- Class tshirts: Public finery
- {
- Public:
- Virtual void show ()
- {
- Cout <"big t-shirt ";
- Finery: Show ();
- }
- };
- Class bigtrouser: Public finery
- {
- Public:
- Virtual void show ()
- {
- Cout <"Pants ";
- Finery: Show ();
- }
- };
- Class sneakers: Public finery
- {
- Public:
- Virtual void show ()
- {
- Cout <"Broken sneakers ";
- Finery: Show ();
- }
- };
- Class businesssuit: Public finery
- {
- Public:
- Virtual void show ()
- {
- Cout <"Suit ";
- Finery: Show ();
- }
- };
- Class necktie: Public finery
- {
- Public:
- Virtual void show ()
- {
- Cout <"Tie ";
- Finery: Show ();
- }
- };
- Class shoeleather: Public finery
- {
- Public:
- Virtual void show ()
- {
- Cout <"Shoes ";
- Finery: Show ();
- }
- };
- Int main (INT argc, char * argv [])
- {
- Person * XC = new person ("coriander ");
- Cout <"first dress up:" <Endl;
- Tshirts * Ts = new tshirts ();
- Bigtrouser * bt = new bigtrouser ();
- Sneakers * Sk = new sneakers ();
- Ts-> decorate (XC );
- BT-> decorate (TS );
- SK-> decorate (BT );
- SK-> show ();
- Return 0;
- }