-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDecorator2.cpp
More file actions
109 lines (99 loc) · 2.26 KB
/
Decorator2.cpp
File metadata and controls
109 lines (99 loc) · 2.26 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
//
// Created by osher on 13/02/2022.
//
#include <iostream>
using namespace std;
class Shake {
public:
virtual int getPrice()=0;
virtual string getDescription()=0;
virtual ~Shake() {}
};
class BigShake : public Shake {
int getPrice() {
return 13;
}
string getDescription() {
return "Big Shake";
}
virtual ~BigShake() {
cout << "delete BigShake" << endl;
}
};
class SmallShake : public Shake {
int getPrice() {
return 6;
}
string getDescription() {
return "Small Shake";
}
virtual ~SmallShake() {
cout << "delete SmallShake" << endl;
}
};
class ShakeDecorator : public Shake {
protected:
Shake* _shake;
public:
virtual int getPrice()=0;
virtual string getDescription()=0;
virtual ~ShakeDecorator() {
cout << "delete ShakeDecorator" << endl;
delete _shake;
}
};
class Oreo : public ShakeDecorator {
public:
Oreo(Shake* shake) {
_shake = shake;
}
int getPrice() {
return _shake->getPrice() + 3;
}
string getDescription() {
return _shake->getDescription() + ", Oreo";
}
virtual ~Oreo() {
cout << "delete Oreo" << endl;
}
};
class Caramel : public ShakeDecorator {
public:
Caramel(Shake* shake) {
_shake = shake;
}
int getPrice() {
return _shake->getPrice() + 5;
}
string getDescription() {
return _shake->getDescription() + ", Caramel";
}
virtual ~Caramel() {
cout << "delete Caramel" << endl;
}
};
class Strawberry : public ShakeDecorator {
public:
Strawberry(Shake* shake) {
_shake = shake;
}
int getPrice() {
return _shake->getPrice() + 1;
}
string getDescription() {
return _shake->getDescription() + ", Strawberry";
}
virtual ~Strawberry() {
cout << "delete Strawberry" << endl;
}
};
int main() {
Shake* shake1 = new Strawberry(new Oreo(new BigShake()));
cout << "shake1=" << shake1->getDescription() << endl;
cout << "cost: " << shake1->getPrice() << endl;
delete shake1;
Shake* shake2 = new Caramel(new SmallShake());
cout << "shake2=" << shake2->getDescription() << endl;
cout << "cost: " << shake2->getPrice() << endl;
delete shake2;
}