Read big talk design patterns-decorator)

Source: Internet
Author: User

Decoration Mode

By dynamically adding some additional responsibilities to an object, the decoration mode is more flexible than the subclass generation.

 

General implementation code

  1. Class component
  2. {
  3. Public:
  4. // Pure virtual class
  5. Virtual void operation () = 0;
  6. };
  7. Class concretecomponent: public component
  8. {
  9. Public:
  10. Virtual void operation ()
  11. {
  12. Cout <"actions on specific objects" <Endl;
  13. }
  14. };
  15. Class decorator: Component
  16. {
  17. Protected:
  18. Component * component;
  19. Public:
  20. Void setcomponent (component * component)
  21. {
  22. This-> compontent = component;
  23. }
  24. // Rewrite operation (). The actual execution is the operation () of component ()
  25. Virtual void operation ()
  26. {
  27. Component-> operation ();
  28. }
  29. }
  30. Class concretedecoratora: decorator
  31. {
  32. PRIVATE:
  33. // Unique functions of this class, different from concretedecoratorb
  34. String addedstate;
  35. Public:
  36. Virtual void operation ()
  37. {
  38. // 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.
  39. Component: Operation ();
  40. Addedstate = "new state ";
  41. Cout <"operations on decoration object ";
  42. }
  43. }
  44. Class concretedecoratorb: decorator
  45. {
  46. Public:
  47. Virtual void operation ()
  48. {
  49. // 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.
  50. Component: Operation ();
  51. Addedbehavior ();
  52. Cout <"operations on decoration object B ";
  53. }
  54. PRIVATE:
  55. Void addedbehavior (){}
  56. }
  57. Int main ()
  58. {
  59. Concretecomponent * c = new concretecomponent;
  60. Concretedecoratora * D1 = new concretecomponent;
  61. Concretedecoratorb * D2 = new concretecomponent;
  62. // The decoration method is: first use concretecomponent to instantiate Object C, and then use concretedecoratora's instantiated object D1 to Package C,
  63. // Use the concretedecoratorb's instantiated object D2 to wrap D1, and finally execute the D2 operation ()
  64. D1-> setcomponent (C );
  65. D2-> setcomponent (D1 );
  66. D2-> operation ();
  67. Return 0;
  68. }

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:

  1. # Include <iostream>
  2. # Include <string>
  3. Using namespace STD;
  4. Class person
  5. {
  6. Public:
  7. Person (){};
  8. Person (string name)
  9. {
  10. This-> name = Name;
  11. }
  12. Virtual void show ()
  13. {
  14. Cout <"Dress Up" <name <Endl;
  15. }
  16. PRIVATE:
  17. String name;
  18. };
  19. Class finery: Public Person
  20. {
  21. Protected:
  22. Person * component;
  23. // Dress up
  24. Public:
  25. Void decorate (person * component)
  26. {
  27. This-> component = component;
  28. }
  29. Virtual void show ()
  30. {
  31. Component-> show ();
  32. }
  33. };
  34. // Concretedecorator)
  35. Class tshirts: Public finery
  36. {
  37. Public:
  38. Virtual void show ()
  39. {
  40. Cout <"big t-shirt ";
  41. Finery: Show ();
  42. }
  43. };
  44. Class bigtrouser: Public finery
  45. {
  46. Public:
  47. Virtual void show ()
  48. {
  49. Cout <"Pants ";
  50. Finery: Show ();
  51. }
  52. };
  53. Class sneakers: Public finery
  54. {
  55. Public:
  56. Virtual void show ()
  57. {
  58. Cout <"Broken sneakers ";
  59. Finery: Show ();
  60. }
  61. };
  62. Class businesssuit: Public finery
  63. {
  64. Public:
  65. Virtual void show ()
  66. {
  67. Cout <"Suit ";
  68. Finery: Show ();
  69. }
  70. };
  71. Class necktie: Public finery
  72. {
  73. Public:
  74. Virtual void show ()
  75. {
  76. Cout <"Tie ";
  77. Finery: Show ();
  78. }
  79. };
  80. Class shoeleather: Public finery
  81. {
  82. Public:
  83. Virtual void show ()
  84. {
  85. Cout <"Shoes ";
  86. Finery: Show ();
  87. }
  88. };
  89. Int main (INT argc, char * argv [])
  90. {
  91. Person * XC = new person ("coriander ");
  92. Cout <"first dress up:" <Endl;
  93. Tshirts * Ts = new tshirts ();
  94. Bigtrouser * bt = new bigtrouser ();
  95. Sneakers * Sk = new sneakers ();
  96. Ts-> decorate (XC );
  97. BT-> decorate (TS );
  98. SK-> decorate (BT );
  99. SK-> show ();
  100. Return 0;
  101. }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.