Repository navigation
Expand file tree
/
Copy pathStructural-Decorator Design Pattern
More file actions
139 lines (112 loc) · 3.37 KB
/
Copy pathStructural-Decorator Design Pattern
File metadata and controls
139 lines (112 loc) · 3.37 KB
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
133
134
135
136
137
138
139
#include <iostream>
#include <string>
using namespace std;
/*Base component class*/
class pizza
{
public:
/* it is virtual because when we delete the base( Pizza) class pointer , then it will call destructor
of pizza not derived class */
virtual ~pizza()
{ cout<<"In Pizza - Destructor"; }
virtual double getCost()=0;
virtual std::string getRecipe()=0;
};
class plainPizza : public pizza
{
private:
std::string recipe;
public:
plainPizza()
{
cout<<"plain Pizza Constructor"<<endl;
}
double getCost()
{
return 10.0;
}
string getRecipe()
{
recipe = "Add Bread , Add Some Olive Oil , Add some salt , Onion , campsicum ";
return recipe;
}
};
/* This is decorator Class, because it contains object of pizza classs,also here we will add some
extra things in plainpizza that is paneer , notice here we are using same function which is in
plainPizza */
class PaneerPizzaDecorator : public pizza
{
private:
pizza *pizzaObjInDec;
std::string paneer;
public:
/* here we are gettting object of pizza class sothat we can use function of plianPizza class */
PaneerPizzaDecorator( pizza *pizzaObj )
{
cout<<"Inside PaneerPizzaDecorator constructor "<<endl;
pizzaObjInDec = pizzaObj;
}
double getCost()
{
/* here we are calling plainPizza getCost function plus 13.0*/
return pizzaObjInDec->getCost() + 13.0;
}
string getRecipe()
{
paneer = ", Add Paneer";
std::string recipe;
/* here we are calling plainPizza getRecipe function plus Add Paneer*/
recipe.append(paneer);
recipe.append(pizzaObjInDec->getRecipe());
return recipe;
}
~PaneerPizzaDecorator()
{
delete pizzaObjInDec;
}
};
/* Now here it is decorator of decorator , object of plainPizza + object of paneerPizza + on top of this
we are adding cheez*/
class PaneerCheezPizzaDecorator : public pizza
{
private:
pizza *pizzaCheezObjInDec;
std::string cheez;
double cheezChrge;
public:
/* here we are gettting object of pizza class sothat we can use function of plianPizza class */
PaneerCheezPizzaDecorator( pizza *pizzaObj )
{
cheezChrge = 5.0;
cout<<"Inside PaneerCheezPizzaDecorator constructor "<<endl;
pizzaCheezObjInDec = pizzaObj;
}
double getCost()
{
/* here we are calling plainPizza getCost function plus 13.0*/
return pizzaCheezObjInDec->getCost() + cheezChrge;
}
string getRecipe()
{
cheez = ", Add Cheez";
std::string recipe;
/* here we are calling plainPizza getRecipe function plus Add Paneer*/
recipe.append(cheez);
recipe.append(pizzaCheezObjInDec->getRecipe());
return recipe;
}
~PaneerCheezPizzaDecorator()
{
delete pizzaCheezObjInDec;
}
};
int main() {
pizza *plnObj = new plainPizza();
pizza *paneerObj = new PaneerPizzaDecorator(plnObj);
pizza *CheezObj = new PaneerCheezPizzaDecorator(paneerObj);
//delete pizzaObj;
//pizzaObj->getCost();
cout<<"Cost of paneer cheez Pizza = "<<plnObj->getCost()<<endl;
cout<<"Cost of paneer cheez Pizza = "<<paneerObj->getCost()<<endl;
cout<<"Cost of paneer cheez Pizza = "<<CheezObj->getCost()<<endl;
}