@ParasArora/

decorator design pattern

C++11

No description

fork
loading
Files
  • main.cpp
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include <iostream>

// abstract class Iicecream (interface)
class IiceCream{
  public:
    virtual void Make() = 0;
    virtual int getPrice() = 0;
    virtual ~IiceCream() = default;
};

// base class for simple ice cream
class SimpleIceCream : public IiceCream{
  private:
    int price = 100;
  public:
    void Make() override {
      std::cout << "milk + suger + ice cream powder";
    }

    int getPrice() override{
      return price;
    }
};

// decorators class
class IceCreamDecorator: public IiceCream{
  private:
    IiceCream &m_decorator;

  public:
    explicit IceCreamDecorator(IiceCream &decorator)
      : m_decorator(decorator) {};

    void Make() override{
      m_decorator.Make();
    }

    int getPrice() override{
      return m_decorator.getPrice();
    }

};
        
// actual decorators
class WithFruits : public IceCreamDecorator{
private:
   int price = 20;
public:
   explicit WithFruits(IiceCream &decorator):IceCreamDecorator(decorator){

   }

   void Make() override{
      IceCreamDecorator::Make();
      std::cout << " + Fruits";
   }
   int getPrice() override{
      return IceCreamDecorator::getPrice() + price;
   }
};

class WithNuts : public IceCreamDecorator{
private:
   int price = 30;
  public:
    explicit WithNuts(IiceCream &decorator)
      :IceCreamDecorator(decorator){}

    void Make() override{
      IceCreamDecorator::Make();
      std::cout << " + Nuts";
    }

    int getPrice() override{
      return IceCreamDecorator::getPrice() + price;
    }
};

class WithWafers : public IceCreamDecorator{
  private:
    int price = 40;
  public:
    explicit WithWafers(IiceCream &decorator)
      :IceCreamDecorator(decorator){}

    void Make() override {
      IceCreamDecorator::Make();
      std::cout << " + wafers" ;
    }

    int getPrice() override {
      return IceCreamDecorator::getPrice() + price;
    }
};

int main() {

  IiceCream* myIceCream = new SimpleIceCream;

  myIceCream = new WithFruits(*myIceCream);

//   std::cout << myIceCream->getPrice() << std::endl;

  myIceCream = new WithWafers(*myIceCream);

  myIceCream = new WithNuts(*myIceCream);

  std::cout << std::endl;
  myIceCream->Make();

  std::cout
    << std::endl << std::endl
    << "total price: "
    << myIceCream->getPrice() << std::endl;

  return 0;
}


/*
        Main abstract class-------------+
        ________|________               |
        |       |        |              |
      obj-1    obj-2   obj-3            |
                                  decorator class
                                ________|________         
                                |       |        |      
                              deco-1  deco-2   deco-3
                            (main* x) 


 */