-
Notifications
You must be signed in to change notification settings - Fork 1
/
OpenClosedPrinciple.cpp
180 lines (137 loc) · 4.48 KB
/
OpenClosedPrinciple.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
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
//
// main.cpp
// OpenClosedPrinciple
//
// Created by Amit Kumar on 19/09/23.
//
#include "vector"
#include "iostream"
using namespace std;
enum class Colour {
White, Space_Grey, Black
};
enum BatterySize {
Large, Medium, Small
};
class Product {
private:
string name;
Colour colour;
BatterySize batterySize;
public:
Product(const string &name, const Colour &colour, const BatterySize &batterySize) : name(name), colour(colour),
batterySize(batterySize) {}
const string &getName() const {
return name;
}
const Colour &getColour() const {
return colour;
}
const BatterySize &getBatterySize() const {
return batterySize;
}
};
namespace BadCode {
class Filter {
public:
static vector<Product *> by_color(const vector<Product *> &bag, const Colour &colour) {
vector < Product * > result;
for (auto &item: bag) {
if (item->getColour() == colour) {
result.push_back(item);
}
}
return result;
}
static vector<Product *> by_size(const vector<Product *> &bag, const BatterySize &size) {
vector < Product * > result;
for (auto &item: bag) {
if (item->getBatterySize() == size) {
result.push_back(item);
}
}
return result;
}
static vector<Product *> by_size_and_colour(const vector<Product *> &bag, const BatterySize &size,
const Colour &colour) {
vector < Product * > result;
for (auto &item: bag) {
if (item->getBatterySize() == size and item->getColour() == colour) {
result.push_back(item);
}
}
return result;
}
};
};
template<typename T>
class Specification {
public:
virtual bool is_satisfied(T *item) const = 0;
};
class ColourSpecification : public Specification<Product> {
Colour color;
public:
ColourSpecification(const Colour &colour) : color(colour) {}
bool is_satisfied(Product *item) const override {
return item->getColour() == color;
}
};
class SizeSpecification : public Specification<Product> {
BatterySize bsize;
public:
SizeSpecification(const BatterySize &bsize) : bsize(bsize) {}
bool is_satisfied(Product *item) const override {
return item->getBatterySize() == bsize;
}
};
class AndSpecification : public Specification<Product> {
ColourSpecification color;
SizeSpecification bsize;
public:
AndSpecification(const ColourSpecification &color, const SizeSpecification &bsize) :
color(color), bsize(bsize) {}
bool is_satisfied(Product *item) const override {
return color.is_satisfied(item) and bsize.is_satisfied(item);
}
};
template<typename T>
class Filter {
public:
virtual vector<T *> filter(const vector<T *> &bags, const Specification<T> &spec) const = 0;
};
class ProductFilter : public Filter<Product> {
public:
vector<Product *> filter(const vector<Product *> &bag, const Specification<Product> &spec) const override {
vector < Product * > result;
for (auto &item: bag) {
if (spec.is_satisfied(item)) {
result.push_back(item);
}
}
return result;
}
};
int main() {
Product phone{"Phone", Colour::White, BatterySize::Medium};
Product watch{"Watch", Colour::Black, BatterySize::Small};
Product headphone{"HeadPhone", Colour::Black, BatterySize::Medium};
Product laptop{"Laptop", Colour::Space_Grey, BatterySize::Large};
vector < Product * > bag{&phone, &watch, &headphone, &laptop};
ProductFilter pf;
ColourSpecification white_color(Colour::White);
for (auto &item: pf.filter(bag, white_color)) {
cout << item->getName() << "is white in colour" << endl;
}
cout << endl << endl;
SizeSpecification medium_battery(BatterySize::Medium);
for (auto &item: pf.filter(bag, medium_battery)) {
cout << item->getName() << " has medium battery size" << endl;
}
cout << endl << endl;
AndSpecification size_and_colour(white_color, medium_battery);
for (auto &item: pf.filter(bag, size_and_colour)) {
cout << item->getName() << " has Medium battery size and white in colour" << endl;
}
return 0;
}