I. Definition
English name: decorator pattern, also known as decorator pattern.
The decoration mode dynamically extends the functions of an object without changing the original class file and using inheritance. It creates a packaging object, that is, decoration, to package a real object.
Ii. Summary
When is the decoration mode practical?
When the system needs new functions, it adds new code to the old class, and the new Code decorated the core responsibilities or main actions of the original class.
If a new field, method, and logic are added to the main class, the complexity of the main class is increased. These newly added classes only meet the special needs of specific situations.
Solution: in the decoration mode, each function to be decorated is placed in a separate class and the class is used to wrap the object to be decorated. Therefore, when performing special behaviors, the client can have decorative packaging objects in sequence and with choices at runtime.
3. Example
From the big talk design model, the dishes are dressed. How do I analyze the style of a person based on the order of clothes? Underpants are ordinary people, while underpants are Superman.
1) dress up the first version of the dish
The following program completes the basic dress types through the console.
Disadvantages:
What if I want to add a "Superman" Dress (wearing underpants?
Changing the person class violates the open-closed principle. How can we optimize it? Write clothing as a subclass to facilitate the extension of clothing subclass.
# Include <iostream> using namespace STD; class person {PRIVATE: string name; public: Person (string INAME) // it is best to distinguish it from name (do not use name again) {name = INAME; // name does not have this }~ Person () {} void weartshirts () {cout <"T-shirt" <Endl;} void wearbigtrouser () {cout <"Pants" <Endl ;} void wearsneakers () {cout <"Broken sneakers" <Endl;} void wearsuit () {cout <"Suit" <Endl;} void weartie () {cout <"Tie" <Endl;} void wearleatershoes () {cout <"Shoes" <Endl;} void show () {cout <"Dress Up" <this-> name <Endl ;}}; int main () {person Xiaocai ("coriander "); // no new object creation method cout <"first dress up" <Endl; Xiaocai. weartshirts (); Xiaocai. wearbigtrouser (); Xiaocai. wearsneakers (); Xiaocai. show (); person * daniu = new person ("Daniel"); // No newcout <"Dress Up 2" <Endl; daniu-> wearsuit (); daniu-> weartie (); daniu-> wearleatershoes (); daniu-> show (); Return 0 ;}
2) dress up for the second edition
Improvement: the finery apparel abstract class and various specific apparel subclass are added to facilitate the extension of various apparel subclass without changing the person class, which complies with the open-closed principle.
Disadvantage: to display what you wear, you have to display the T-shirt and KK. Show () // display the pants on the client dtx. Show. It is equivalent to wearing clothes in a wide audience. How can we improve it?
After the internal assembly is completed, you can see what to wear. This looks like the builder mode (but the builder mode requires the construction process to be stable. Here you can wear a suit and coat T-shirt)
# Include <iostream> using namespace STD; class person {public: Person (string name) {m_name = Name ;}; void show (void) {cout <"Dress Up" <m_name <Endl ;}; private: String m_name ;}; class finery // apparel abstract class {public: Virtual void show (void) = 0 ;}; class tshirts: Public finery {public: void show (void) {cout <"big t-shirt" <Endl ;};}; class bigtrouser: public finery {public: void show (void) {STD: cout <"Pants" <Endl ;};}; class sneakers: Public finery {public: void show (void) {cout <"Broken sneakers" <Endl ;};}; class suit: Public finery {public: void show (void) {cout <"Suit" <Endl ;};}; class tie: Public finery {public: void show (void) {cout <"Tie" <Endl ;};}; class leathershoe: Public finery {public: void show (void) {cout <"Shoes" <Endl ;};}; int main () {person * Xiaocai = new person ("coriander "); finery * dtx = new tshirts (); finery * KK = new bigtrouser (); finery * pqx = new sneakers (); dtx-> show (); KK-> show (); pqx-> show (); Xiaocai-> show (); Return 0 ;}
3) dressing up the third edition
Structure: The person class must be dressed to inherit the person class from the abstract decoration class. Then inherit the abstract clothing class and initialize the clothing subclass.
Improvement: compared with the second version, you can first wear clothes (decoration process)
Sneakers * pqx = new sneakers ();
Bigtrouser * KK = new bigtrouser ();
Tshirts * dtx = new tshirts ();
Pqx-> decorator (Xiaocai );
KK-> decorator (pqx );
Dtx-> decorator (kk );
Then dtx. Show () is displayed ()
# Include <string> # include <iostream> class person {public: Person (STD: string name) {m_name = Name ;}; virtual void show (void) {STD :: cout <"Dress Up" <m_name <STD: Endl ;}; person () {}; PRIVATE: STD: String m_name ;}; // apparel class finery: public person {public: void decorator (person * comp) {This-> m_pcomponent = comp ;}; void show (void) {If (m_pcomponent! = NULL) {m_pcomponent-> show () ;}}; protected: person * m_pcomponent ;}; // class tshirts: Public finery {public: void show (void) {STD: cout <"big t-shirt" <STD: Endl; finery: Show () ;}}; class bigtrouser: Public finery {public: void show (void) {STD: cout <"Pants" <STD: Endl; finery: Show () ;};}; class sneakers: public finery {public: void show (void) {STD: cout <"Broken sneakers" <STD: Endl; finery: Show ();};}; class suit: Public finery {public: void show (void) {STD: cout <"Suit" <STD: Endl; finery :: show () ;};}; class tie: Public finery {public: void show (void) {STD: cout <"Tie" <STD: Endl; finery:: Show () ;};}; class leathershoe: Public finery {public: void show (void) {STD: cout <"" <STD: Endl; finery: Show () ;};}; int main () {person * Xiaocai = new person ("coriander"); STD :: cout <"first dress up" <STD: Endl; sneakers * pqx = new sneakers (); bigtrouser * KK = new bigtrouser (); tshirts * dtx = new tshirts (); pqx-> decorator (Xiaocai); KK-> decorator (pqx); dtx-> decorator (kk); dtx-> show (); STD: cout <"second dress up" <STD: Endl; leathershoe * PX = new leathershoe (); tie * Tie = new tie (); suit * suit = new suit (); PX-> decorator (Xiaocai); tie-> decorator (PX); suit-> decorator (TIE); suit-> show (); return 0 ;}