-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathConceptualExample02.cpp
More file actions
206 lines (176 loc) · 6.01 KB
/
ConceptualExample02.cpp
File metadata and controls
206 lines (176 loc) · 6.01 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
// ===========================================================================
// ConceptualExample02.cpp // Builder Pattern
// ===========================================================================
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <memory>
/**
* It makes sense to use the Builder pattern only when your products
* are quite complex and require extensive configuration.
*
* Unlike in other creational patterns, different concrete builders can produce
* unrelated products. In other words, results of various builders may not
* always follow the same interface.
*/
class Product
{
private:
std::vector<std::string> m_parts;
public:
void addPart(const std::string& part) {
m_parts.push_back(part);
}
std::string operator()() {
std::ostringstream oss;
oss << "Product parts: ";
for (size_t i = 0; i < m_parts.size(); ++i) {
if (m_parts[i] == m_parts.back()) {
oss << m_parts[i];
}
else {
oss << m_parts[i] << ", ";
}
}
oss << std::endl;
return oss.str();
}
};
/**
* The Builder interface specifies methods for creating
* the different parts of Product objects.
*/
class Builder {
public:
virtual ~Builder() {}
virtual void createProducePartA() const = 0;
virtual void createProducePartB() const = 0;
virtual void createProducePartC() const = 0;
virtual std::unique_ptr<Product> getProduct() = 0;
};
/**
* The Concrete Builder classes follow the Builder interface and provide
* specific implementations of the building steps. Your program may have several
* variations of Builders, implemented differently.
*/
class ConcreteBuilder : public Builder {
private:
std::unique_ptr<Product> m_product;
public:
/**
* A fresh builder instance should contain a blank product object,
* which is used in further assembly.
*/
ConcreteBuilder() {
reset();
}
void reset() {
m_product = std::make_unique<Product>();
}
/**
* All production steps work with the same product instance.
*/
void createProducePartA() const override {
m_product->addPart(std::string("Part A1"));
}
void createProducePartB() const override {
m_product->addPart(std::string("Part B1"));
}
void createProducePartC() const override {
m_product->addPart(std::string("Part C1"));
}
/**
* Concrete Builders are supposed to provide their own methods for
* retrieving results. That's because various types of builders may create
* entirely different products that don't follow the same interface.
* Therefore, such methods cannot be declared in the base Builder interface
* (at least in a statically typed programming language).
*
* Usually, after returning the end result to the client, a builder instance
* is expected to be ready to start producing another product. That's why
* it's a usual practice to call the reset method at the end of the
* 'getProduct' method body. However, this behavior is not mandatory, and
* you can make your builders wait for an explicit reset call from the
* client code before disposing of the previous result.
*/
/**
* Please be careful here with the memory ownership. Once you call
* 'getProduct' the user of this function is responsable to release this
* memory. Here could be a better option to use smart pointers to avoid
* memory leaks, see below
*/
std::unique_ptr<Product> getProduct() {
std::unique_ptr<Product> result = std::move(m_product);
reset();
return result;
}
};
/**
* The Director is only responsible for executing the building steps in a
* particular sequence. It is helpful when producing products according to a
* specific order or configuration. Strictly speaking, the Director class is
* optional, since the client can control builders directly.
*/
class Director {
private:
std::shared_ptr<Builder> m_builder;
public:
/**
* The Director works with any builder instance that the client code passes
* to it. This way, the client code may alter the final type of the newly
* assembled product.
*/
void setBuilder(std::shared_ptr<Builder> builder) {
m_builder = builder;
}
/**
* The Director can construct several product variations using the same
* building steps.
*/
void buildMinimalViableProduct() {
m_builder->createProducePartA();
}
void buildFullFeaturedProduct() {
m_builder->createProducePartA();
m_builder->createProducePartB();
m_builder->createProducePartC();
}
};
/**
* The client code creates a builder object, passes it to the director and then
* initiates the construction process. The end result is retrieved from the
* builder object.
*/
static void clientCode(Director& director)
{
std::shared_ptr<ConcreteBuilder> builder{
std::make_shared<ConcreteBuilder>()
};
director.setBuilder(builder);
std::cout << "Standard basic product:" << std::endl;
director.buildMinimalViableProduct();
std::unique_ptr<Product> product{
builder->getProduct()
};
std::cout << (*product)() << std::endl;
std::cout << "Standard full featured product:" << std::endl;
director.buildFullFeaturedProduct();
product = builder->getProduct();
std::cout << (*product)() << std::endl;
// remember, the Builder pattern can be used without a Director class.
std::cout << "Custom product:" << std::endl;
builder->createProducePartA();
builder->createProducePartC();
product = builder->getProduct();
std::cout << (*product)() << std::endl;
}
// function prototypes
void test_conceptual_example_02()
{
Director director;
clientCode(director);
}
// ===========================================================================
// End-of-File
// ===========================================================================