-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDecorator.cpp
More file actions
196 lines (158 loc) · 3.71 KB
/
Decorator.cpp
File metadata and controls
196 lines (158 loc) · 3.71 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
//
// Decorator.hpp
// COns
//
// Created by zhTian on 16/4/16.
// Copyright © 2016年 zhTian. All rights reserved.
//
#ifndef Decorator_hpp
#define Decorator_hpp
#include <iostream>
class Car
{
public:
Car()
{
m_name = "Unknown car";
}
Car(const Car&){}
Car& operator=(const Car&) = delete;
virtual ~Car()
{
std::cout << "~Car()" << std::endl;
}
public:
virtual std::string getDescription()
{
return m_name;
}
virtual double getCost() = 0;
protected:
std::string m_name;
};
//Concrete Car
class CarModell : public Car
{
public:
CarModell()
{
m_name = "CarModell";
}
CarModell(const CarModell&){}
CarModell& operator=(const CarModell&) = delete;
~CarModell()
{
std::cout << "~CarModell()" << std::endl;
}
public:
double getCost()
{
return 310000.23;
}
};
//Decorator
class Decorator : public Car
{
public:
Decorator(){}
Decorator(const Decorator&){}
Decorator& operator=(const Decorator&) = delete;
virtual ~Decorator()
{
std::cout << "~Decorator()" << std::endl;
}
public:
virtual std::string getDescription() = 0;
virtual double getCost() = 0;
};
class Navigation : public Decorator
{
public:
Navigation(){}
Navigation(const Navigation&){}
Navigation& operator=(const Navigation&) = delete;
virtual ~Navigation()
{
std::cout << "~Navigation()" << std::endl;
}
public:
Navigation(Car *b) : m_b(b){}
public:
std::string getDescription()
{
return m_b->getDescription() + ", Navigaton.";
}
double getCost()
{
return 200.3 + m_b->getCost();
}
private:
Car *m_b;
};
class PremiumSoundSystem : public Decorator
{
public:
PremiumSoundSystem(){}
PremiumSoundSystem(const PremiumSoundSystem&){}
PremiumSoundSystem& operator=(const PremiumSoundSystem&) = delete;
virtual ~PremiumSoundSystem()
{
std::cout << "~PremiumSoundSystem()" << std::endl;
}
public:
PremiumSoundSystem(Car *b) : m_b(b){}
public:
std::string getDescription()
{
return m_b->getDescription() + ", PremiumSoundSystem.";
}
double getCost()
{
return 11.34 + m_b->getCost();
}
private:
Car *m_b;
};
class ManualTransmission : public Decorator
{
public:
ManualTransmission(){}
ManualTransmission(const ManualTransmission&){}
ManualTransmission& operator=(const ManualTransmission&) = delete;
virtual ~ManualTransmission()
{
std::cout << "~ManualTransmission()" << std::endl;
}
public:
ManualTransmission(Car *b) : m_b(b){}
public:
std::string getDescription()
{
return m_b->getDescription() + ", ManualTransmission";
}
double getCost()
{
return 14.45 + m_b->getCost();
}
private:
Car *m_b;
};
class CarDecoratorExample
{
public:
void excute()
{
Car *b = new CarModell();
std::cout << "base model of " << b->getDescription() << " , and cost is " << b->getCost() << std::endl;
//let's add some features.
b = new Navigation(b);
//set cout precision to output double type value.
std::cout.precision(10);
std::cout << b->getDescription() << " will cost you " << b->getCost() << std::endl;
b = new PremiumSoundSystem(b);
std::cout << b->getDescription() << " will cost you " << b->getCost() << std::endl;
b = new ManualTransmission(b);
std::cout << b->getDescription() << " will cost you " << b->getCost() << std::endl;
}
};
#endif /* Decorator_hpp */