Advertisement
Guest User

Untitled

a guest
Feb 19th, 2020
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.73 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. class Component {
  7. public:
  8. ~Component() {}
  9. virtual string Add() const = 0;
  10. virtual string Remove() const = 0;
  11. };
  12.  
  13. class ConcreteComponent : public Component {
  14. public:
  15. string Add() const override {
  16. return "Ice-cream with: ";
  17. }
  18. string Remove() const override {
  19. return "Ice-cream without: ";
  20. }
  21. };
  22.  
  23. class Decorator : public Component {
  24.  
  25. protected:
  26. Component* component_;
  27.  
  28. public:
  29. Decorator(Component* component) : component_(component) {
  30. }
  31.  
  32. string Add() const override {
  33. return this->component_->Add();
  34. }
  35.  
  36. string Remove() const override {
  37. return this->component_->Remove();
  38. }
  39. };
  40.  
  41. class Syrup : public Decorator {
  42.  
  43. public:
  44. Syrup(Component* component) : Decorator(component) {
  45. }
  46. string Add() const override {
  47. return Decorator::Add() + "Syrup ";
  48. }
  49. string Remove() const override {
  50. return Decorator::Remove() + "Syrup ";
  51. }
  52. };
  53.  
  54. class Raspberry : public Decorator {
  55. public:
  56. Raspberry(Component* component) : Decorator(component) {
  57. }
  58.  
  59. string Add() const override {
  60. return Decorator::Add() + "Raspberry ";
  61. }
  62.  
  63. string Remove() const override {
  64. return Decorator::Remove() + "Raspberry ";
  65. }
  66. };
  67.  
  68. void ClientCode(Component* component1, Component* component2) {
  69. cout << "1) I want : " << component1->Add();
  70. cout << "And " << component2->Remove();
  71. }
  72.  
  73. int main() {
  74. Component* simple = new ConcreteComponent;
  75. Component* decorator1 = new Raspberry(simple);
  76. Component* decorator2 = new Syrup(decorator1);
  77. ClientCode(decorator2, decorator1);
  78. cout << "\n";
  79.  
  80. delete simple;
  81. delete decorator1;
  82. delete decorator2;
  83.  
  84. return 0;
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement