Repository navigation
Expand file tree
/
Copy pathAbstract_factory.cpp
More file actions
189 lines (173 loc) · 5.93 KB
/
Copy pathAbstract_factory.cpp
File metadata and controls
189 lines (173 loc) · 5.93 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
#include <string>
#include <iostream>
#include <memory>
/**
* Each distinct product of a product family should have a base interface. All
* variants of the product must implement this interface.
*/
class AbstractProductA {
public:
virtual ~AbstractProductA(){
std::cout << "AbstractProductA destroyed" << std::endl;
};
virtual std::string UsefulFunctionA() const = 0;
};
/**
* Concrete Products are created by corresponding Concrete Factories.
*/
class ConcreteProductA1 : public AbstractProductA {
public:
std::string UsefulFunctionA() const override {
return "The result of the product A1.";
}
~ConcreteProductA1(){
std::cout << "ConcreteProductA1 destroyed" << std::endl;
};
};
class ConcreteProductA2 : public AbstractProductA {
std::string UsefulFunctionA() const override {
return "The result of the product A2.";
}
~ConcreteProductA2(){
std::cout << "ConcreteProductA2 destroyed" << std::endl;
};
};
/**
* Here's the the base interface of another product. All products can interact
* with each other, but proper interaction is possible only between products of
* the same concrete variant.
*/
class AbstractProductB {
/**
* Product B is able to do its own thing...
*/
public:
virtual ~AbstractProductB(){
std::cout << "AbstractProductB destroyed" << std::endl;
};
virtual std::string UsefulFunctionB() const = 0;
/**
* ...but it also can collaborate with the ProductA.
*
* The Abstract Factory makes sure that all products it creates are of the
* same variant and thus, compatible.
*/
virtual std::string AnotherUsefulFunctionB(const AbstractProductA &collaborator) const = 0;
};
/**
* Concrete Products are created by corresponding Concrete Factories.
*/
class ConcreteProductB1 : public AbstractProductB {
public:
std::string UsefulFunctionB() const override {
return "The result of the product B1.";
}
~ConcreteProductB1(){
std::cout << "ConcreteProductB1 destroyed" << std::endl;
};
/**
* The variant, Product B1, is only able to work correctly with the variant,
* Product A1. Nevertheless, it accepts any instance of AbstractProductA as an
* argument.
*/
std::string AnotherUsefulFunctionB(const AbstractProductA &collaborator) const override {
const std::string result = collaborator.UsefulFunctionA();
return "The result of the B1 collaborating with ( " + result + " )";
}
};
class ConcreteProductB2 : public AbstractProductB {
public:
std::string UsefulFunctionB() const override {
return "The result of the product B2.";
}
~ConcreteProductB2(){
std::cout << "ConcreteProductB2 destroyed" << std::endl;
};
/**
* The variant, Product B2, is only able to work correctly with the variant,
* Product A2. Nevertheless, it accepts any instance of AbstractProductA as an
* argument.
*/
std::string AnotherUsefulFunctionB(const AbstractProductA &collaborator) const override {
const std::string result = collaborator.UsefulFunctionA();
return "The result of the B2 collaborating with ( " + result + " )";
}
};
/**
* The Abstract Factory interface declares a set of methods that return
* different abstract products. These products are called a family and are
* related by a high-level theme or concept. Products of one family are usually
* able to collaborate among themselves. A family of products may have several
* variants, but the products of one variant are incompatible with products of
* another.
*/
class AbstractFactory {
public:
~AbstractFactory(){
std::cout << "AbstractFactory destroyed" << std::endl;
};
virtual AbstractProductA *CreateProductA() const = 0;
virtual AbstractProductB *CreateProductB() const = 0;
};
/**
* Concrete Factories produce a family of products that belong to a single
* variant. The factory guarantees that resulting products are compatible. Note
* that signatures of the Concrete Factory's methods return an abstract product,
* while inside the method a concrete product is instantiated.
*/
class ConcreteFactory1 : public AbstractFactory {
public:
~ConcreteFactory1(){
std::cout << "ConcreteFactory1 destroyed" << std::endl;
};
AbstractProductA *CreateProductA() const override {
return new ConcreteProductA1();
}
AbstractProductB *CreateProductB() const override {
return new ConcreteProductB1();
}
};
/**
* Each Concrete Factory has a corresponding product variant.
*/
class ConcreteFactory2 : public AbstractFactory {
public:
~ConcreteFactory2(){
std::cout << "ConcreteFactory2 destroyed" << std::endl;
};
AbstractProductA *CreateProductA() const override {
return new ConcreteProductA2();
}
AbstractProductB *CreateProductB() const override {
return new ConcreteProductB2();
}
};
/**
* The client code works with factories and products only through abstract
* types: AbstractFactory and AbstractProduct. This lets you pass any factory or
* product subclass to the client code without breaking it.
*/
void ClientCode(const AbstractFactory &factory) {
//const AbstractProductA *product_a = factory.CreateProductA();
std::unique_ptr<const AbstractProductA> product_a(factory.CreateProductA());
//const AbstractProductB *product_b = factory.CreateProductB();
std::unique_ptr<const AbstractProductB> product_b(factory.CreateProductB());
std::cout << product_b->UsefulFunctionB() << "\n";
std::cout << product_b->AnotherUsefulFunctionB(*product_a) << "\n";
//delete product_a;
//delete product_b;
}
int main() {
std::cout << "Client: Testing client code with the first factory type:\n";
//ConcreteFactory1 *f1 = new ConcreteFactory1();
std::unique_ptr<ConcreteFactory1> f1 = std::make_unique<ConcreteFactory1>();
ClientCode(*f1);
//delete f1;
std::cout << std::endl;
std::cout << "Client: Testing the same client code with the second factory type:\n";
//ConcreteFactory2 *f2 = new ConcreteFactory2();
std::unique_ptr<ConcreteFactory2> f2 = std::make_unique<ConcreteFactory2>();
ClientCode(*f2);
//delete f2;
return 0;
}