-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathConceptualExample02.cpp
More file actions
195 lines (156 loc) · 5.77 KB
/
Copy pathConceptualExample02.cpp
File metadata and controls
195 lines (156 loc) · 5.77 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
// ===========================================================================
// ConceptualExample02.cpp // Factory Method
// ===========================================================================
#include <algorithm>
#include <memory>
#include <print>
#include <stdexcept>
#include <string>
#include <vector>
namespace ConceptualExample02
{
/**
* Product Interface:
* The Product Interface declares the operations
* that all concrete products must implement.
*/
class ProductBase
{
public:
virtual ~ProductBase() {}
virtual std::string getName() const = 0;
virtual void anyOperation() = 0;
};
/**
* Concrete Products provide various implementations of the Product interface.
*/
class ConcreteProductA : public ProductBase
{
public:
std::string getName() const override {
return "ConcreteProductA";
}
void anyOperation() override {
std::println("Working with ConcreteProduct A");
}
};
class ConcreteProductB : public ProductBase
{
public:
std::string getName() const override {
return "ConcreteProductB";
}
void anyOperation() override {
std::println("Working with ConcreteProduct B");
}
};
// =======================================================================
/**
* The FactoryBase class declares the factory method
* that is supposed to return an object of a Product class.
* The FactoryBase's subclasses usually provide the implementation of this method.
*/
class FactoryBase
{
public:
FactoryBase() : m_numberOfProductsProduced{} {}
virtual ~FactoryBase() {} // always a virtual destructor
private:
virtual std::unique_ptr<ProductBase> createProduct() const = 0;
/**
* Note:
* The FactoryBase's class primary responsibility is *not* creating products.
* Usually, it contains some core business logic that relies on Product objects,
* returned by the underlying factory method.
* Subclasses can indirectly change that business logic by overriding
* the factory method and returning a different type of product from it.
*/
public:
std::unique_ptr<ProductBase> requestProduct() {
// call the factory method to create a Product object.
std::unique_ptr<ProductBase> product{ createProduct() }; // <= abstract method (!)
// increment the number of products produced and return the new product
++m_numberOfProductsProduced;
return product;
}
size_t getNumberOfProductsProduced() const {
return m_numberOfProductsProduced;
}
private:
size_t m_numberOfProductsProduced;
};
// =======================================================================
// Constructs an instance, taking ownership of the given factories.
class LeastBusyFactory final : public FactoryBase
{
private:
std::vector<std::unique_ptr<FactoryBase>> m_factories;
public:
explicit LeastBusyFactory(std::vector<std::unique_ptr<FactoryBase>> factories)
: m_factories{ std::move(factories) }
{
if (m_factories.empty()) {
throw std::runtime_error{ "No factories provided." };
}
}
private:
std::unique_ptr<ProductBase> createProduct() const override {
auto compareProducedProducts = [] (const auto& factory1, const auto& factory2) {
return factory1->getNumberOfProductsProduced() < factory2->getNumberOfProductsProduced();
};
auto leastBusyFactory {
std::min_element(
m_factories.begin(),
m_factories.end(),
compareProducedProducts
)
};
return (*leastBusyFactory)->requestProduct();
}
};
/**
* Concrete FactoryBase classes override the factory method
* in order to change the resulting product's type.
*/
class ConcreteFactoryA final : public FactoryBase
{
private:
std::unique_ptr<ProductBase> createProduct() const override {
std::unique_ptr<ProductBase> product{ std::make_unique<ConcreteProductA>()};
return product;
}
};
class ConcreteFactoryB final : public FactoryBase
{
private:
std::unique_ptr<ProductBase> createProduct() const override {
std::unique_ptr<ProductBase> product{ std::make_unique<ConcreteProductB>() };
return product;
}
};
}
void test_conceptual_example_02()
{
using namespace ConceptualExample02;
std::vector<std::unique_ptr<FactoryBase>> factories;
// create 2 factories: one of type 'A' and another 'B' factory:
factories.push_back(std::make_unique<ConcreteFactoryA>());
factories.push_back(std::make_unique<ConcreteFactoryB>());
// to get more interesting results,
// preorder some products from specific factories
for (size_t i : { 0, 0, 0, 0, 0, 1 }) {
factories[i]->requestProduct();
}
// create a factory that automatically selects the least busy
// factory from a list of given factories.
LeastBusyFactory leastBusyFactory{ std::move(factories) };
std::println("Starting a new production sequence ...");
// build 10 cars from the least busy factory.
for (size_t i{}; i != 10; ++i) {
auto product{ leastBusyFactory.requestProduct() };
std::println("{}", product->getName());
}
}
// ===========================================================================
// End-of-File
// ===========================================================================