-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathConceptualExample01.cpp
More file actions
177 lines (140 loc) · 5.24 KB
/
Copy pathConceptualExample01.cpp
File metadata and controls
177 lines (140 loc) · 5.24 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
// ===========================================================================
// ConceptualExample01.cpp // Factory Method
// ===========================================================================
#include <string>
#include <memory>
#include <print>
namespace ConceptualExample01
{
/**
* 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 (!)
// now, *use* the product:
product->anyOperation();
std::string name{ product->getName() };
std::println("FactoryBase: This factory's code has just created a {}", name);
// 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;
};
/**
* Concrete FactoryBase classes override the factory method
* in order to change the resulting product's type.
*/
class ConcreteFactoryA final : public FactoryBase
{
/**
* Note that the signature of the method still uses the abstract product type,
* even though the concrete product is actually returned from the method.
* This way the FactoryBase can stay independent of concrete product classes:
* ==> Compare with "Virtual Constructor" Pattern
*/
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;
}
};
/**
* The client code works with an instance of a concrete FactoryBase,
* albeit through its base interface.
* As long as the client keeps working with the FactoryBase
* via the base interface, you can pass it any FactoryBase's subclass.
*/
static void clientCode(FactoryBase& factory) {
std::println("Client: Not aware of the concrete creator's class (FactoryBase):");
std::unique_ptr<ProductBase> product{ factory.requestProduct() };
std::println("Created {}", product->getName());
std::println("Total Products: {}", factory.getNumberOfProductsProduced());
}
}
/**
* The Application picks a factory's type
* depending on the configuration or environment.
*/
void test_conceptual_example_01()
{
using namespace ConceptualExample01;
std::println("Example: Launched with ConcreteFactory A:");
ConcreteFactoryA factoryA;
clientCode(factoryA);
std::println();
std::println("Example: Launched with ConcreteFactory B:");
ConcreteFactoryB factoryB;
clientCode(factoryB);
std::println();
}
// ===========================================================================
// End-of-File
// ===========================================================================