-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathspecification-pattern.cpp
More file actions
217 lines (174 loc) · 6.2 KB
/
specification-pattern.cpp
File metadata and controls
217 lines (174 loc) · 6.2 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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#include <stdint.h>
#include <string>
#include <vector>
#include <memory>
#include <iostream>
// --------- Typedefs --------- //
typedef uint32_t Price;
typedef enum { Small, Medium, Large, Huge } Size; /*!< Possible sizes of the Products */
typedef enum { Blue, Red, Green, Yellow } Color; /*!< Possible colors of the Products */
/*!
* \brief The Product class is the class
* on which you want to apply and combine
* different business logics.
*/
class Product {
public:
Product( std::string&& p_name = "",
Color&& p_color = Blue,
Size && p_size = Small,
Price&& p_price = 0) :
m_name(p_name), m_color(p_color), m_size(p_size), m_price(p_price) {}
virtual ~Product() = default;
std::string getDescription(void) const { return "\t- " + m_name; }
// Getters
Color getColor (void) const { return m_color; }
Size getSize (void) const { return m_size; }
Price getPrice (void) const { return m_price; }
private:
std::string m_name;
Color m_color;
Size m_size;
Price m_price;
};
// --------- SPECIFICATIONS INTERFACE --------- //
/*!
* \brief ISpecification - interface of specification
* to be implemented by ConcreteSpecifications.
*/
// Forward declarations
template <typename T> class ANDSpecification;
template <typename T> class ORSpecification;
template <typename T> class NOTSpecification;
template <typename T>
class ISpecification
{
public:
virtual bool isSatisfiedBy(const T&) const = 0;
std::unique_ptr<ISpecification<T> > operator&& (ISpecification<T>&& other) {
return std::make_unique<ANDSpecification<T> >(*this, other);
}
std::unique_ptr<ISpecification<T> > operator|| (ISpecification<T>&& other) {
return std::make_unique<ORSpecification <T> >(*this, other);
}
std::unique_ptr<ISpecification<T> > operator! (void) {
return std::make_unique<NOTSpecification<T> >(*this);
}
};
// --------- FILTER INTERFACE --------- //
template <typename T>
class IFilter
{
public:
virtual std::vector<T> filter(std::vector<T>& items,
std::unique_ptr<ISpecification<T>>& spec ) = 0;
};
class ProductFilter : public IFilter<Product>
{
public:
std::vector<Product> filter(std::vector<Product>& items,
std::unique_ptr<ISpecification<Product>>& spec ) override
{
std::vector<Product> ret;
for ( auto& it : items )
if ( spec->isSatisfiedBy(it) ) { ret.push_back(it); }
return ret;
}
};
// --------- SPECIFICATIONS BUSINESS LOGICS --------- //
template <typename T>
class ANDSpecification : public ISpecification<T>
{
public:
ANDSpecification(ISpecification<T>& p_left,
ISpecification<T>& p_right) : m_leftSpec(p_left), m_rightSpec(p_right) {}
virtual ~ANDSpecification() = default;
virtual bool isSatisfiedBy(const T& item) const override {
return m_leftSpec.isSatisfiedBy(item) && m_rightSpec.isSatisfiedBy(item);
}
private:
ISpecification<T>& m_leftSpec;
ISpecification<T>& m_rightSpec;
};
template <typename T>
class ORSpecification : public ISpecification<T>
{
public:
ORSpecification(ISpecification<T>& p_left,
ISpecification<T>& p_right) : m_leftSpec(p_left), m_rightSpec(p_right) {}
virtual ~ORSpecification() = default;
virtual bool isSatisfiedBy(const T& item) const override {
return m_leftSpec.isSatisfiedBy(item) || m_rightSpec.isSatisfiedBy(item);
}
private:
ISpecification<T>& m_leftSpec;
ISpecification<T>& m_rightSpec;
};
template <typename T>
class NOTSpecification : public ISpecification<T>
{
public:
NOTSpecification(ISpecification<T>& p_cnd) : m_Spec(p_cnd) {}
virtual ~NOTSpecification() = default;
virtual bool isSatisfiedBy(const T& item) const override {
return !m_Spec.isSatisfiedBy(item);
}
private:
ISpecification<T>& m_Spec;
};
// --------- CONCRETE SPECIFICATIONS --------- //
/*!
* \brief ConcreteSpecification - implements the Specification
* interface for a particular business logic.
*/
class ColorSpecification : public ISpecification<Product>
{
public:
ColorSpecification(Color&& p_color) : m_color(p_color) {}
virtual ~ColorSpecification() = default;
virtual bool isSatisfiedBy(const Product& item) const override { return item.getColor() == m_color; }
private:
Color m_color;
};
class SizeSpecification : public ISpecification<Product>
{
public:
SizeSpecification(Size&& p_size) : m_size(p_size) {}
virtual ~SizeSpecification() = default;
virtual bool isSatisfiedBy(const Product& item) const override { return item.getSize() == m_size; }
private:
Size m_size;
};
class PriceSpecification : public ISpecification<Product>
{
public:
PriceSpecification(Size&& p_price) : m_price(p_price) {}
virtual ~PriceSpecification() = default;
virtual bool isSatisfiedBy(const Product& item) const override { return item.getPrice() == m_price; }
private:
Price m_price;
};
// --------- MAIN --------- //
int main()
{
std::vector<Product> prods = { Product("House", Blue , Huge , 500000),
Product("Cake" , Yellow, Small , 3),
Product("Tree" , Green , Large , 100),
Product("Plant", Green , Medium, 12) };
auto mySpecs1 = ColorSpecification(Green) && SizeSpecification (Large );
auto mySpecs2 = ColorSpecification(Blue ) || ColorSpecification(Yellow);
auto mySpecs3 = !PriceSpecification(static_cast<Size>(500000));
ProductFilter myFilter;
std::cout << "Items corresponding to specs 1 :\n";
for ( auto& it : myFilter.filter(prods, mySpecs1) )
std::cout << it.getDescription() << std::endl;
std::cout << std::endl;
std::cout << "Items corresponding to specs 2 :\n";
for ( auto& it : myFilter.filter(prods, mySpecs2) )
std::cout << it.getDescription() << std::endl;
std::cout << std::endl;
std::cout << "Items corresponding to specs 3 :\n";
for ( auto& it : myFilter.filter(prods, mySpecs3) )
std::cout << it.getDescription() << std::endl;
std::cout << std::endl;
}