Skip to content

Commit 02497b1

Browse files
committed
Polishing.
1 parent bf7c4ed commit 02497b1

File tree

3 files changed

+215
-262
lines changed

3 files changed

+215
-262
lines changed

src/AbstractFactory/Conceptual/main.cc

Lines changed: 125 additions & 162 deletions
Original file line numberDiff line numberDiff line change
@@ -12,73 +12,36 @@
1212
* Назначение: Предоставляет интерфейс для создания семейств связанных или
1313
* зависимых объектов без привязки к их конкретным классам.
1414
*/
15-
/**
16-
* EN: The Abstract Factory interface declares a set of methods that return
17-
* different abstract products. These products are called a family and are
18-
* related by a high-level theme or concept. Products of one family are usually
19-
* able to collaborate among themselves. A family of products may have several
20-
* variants, but the products of one variant are incompatible with products of
21-
* another.
22-
*
23-
* RU: Интерфейс Абстрактной Фабрики объявляет набор методов, которые возвращают
24-
* различные абстрактные продукты. Эти продукты называются семейством и связаны
25-
* темой или концепцией высокого уровня. Продукты одного семейства обычно могут
26-
* взаимодействовать между собой. Семейство продуктов может иметь несколько
27-
* вариаций, но продукты одной вариации несовместимы с продуктами другой.
28-
*/
29-
30-
class AbstractProductA;
31-
class AbstractProductB;
32-
33-
class AbstractFactory
34-
{
35-
public:
36-
virtual AbstractProductA* CreateProductA() const = 0;
37-
virtual AbstractProductB* CreateProductB() const = 0;
38-
};
3915

4016
/**
4117
* EN: Each distinct product of a product family should have a base interface.
4218
* All variants of the product must implement this interface.
4319
*
4420
* RU: Каждый отдельный продукт семейства продуктов должен иметь базовый
4521
* интерфейс. Все вариации продукта должны реализовывать этот интерфейс.
46-
*/
47-
48-
class AbstractProductA
49-
{
50-
/**
51-
* EN: Define virtual destructor in case you need it.
52-
*
53-
* RU:
54-
*/
55-
public:
56-
virtual ~AbstractProductA(){};
57-
virtual std::string UsefulFunctionA() const = 0;
22+
*/
23+
class AbstractProductA {
24+
public:
25+
virtual ~AbstractProductA() {};
26+
virtual std::string UsefulFunctionA() const = 0;
5827
};
5928

60-
6129
/**
6230
* EN: Concrete Products are created by corresponding Concrete Factories.
6331
*
6432
* RU: Конкретные продукты создаются соответствующими Конкретными Фабриками.
6533
*/
66-
67-
class ConcreteProductA1 : public AbstractProductA
68-
{
69-
public:
70-
std::string UsefulFunctionA() const override
71-
{
72-
return "The result of the product A1.";
73-
}
34+
class ConcreteProductA1 : public AbstractProductA {
35+
public:
36+
std::string UsefulFunctionA() const override {
37+
return "The result of the product A1.";
38+
}
7439
};
7540

76-
class ConcreteProductA2 : public AbstractProductA
77-
{
78-
std::string UsefulFunctionA() const override
79-
{
80-
return "The result of the product A2.";
81-
}
41+
class ConcreteProductA2 : public AbstractProductA {
42+
std::string UsefulFunctionA() const override {
43+
return "The result of the product A2.";
44+
}
8245
};
8346

8447
/**
@@ -90,81 +53,93 @@ class ConcreteProductA2 : public AbstractProductA
9053
* друг с другом, но правильное взаимодействие возможно только между продуктами
9154
* одной и той же конкретной вариации.
9255
*/
93-
94-
class AbstractProductB
95-
{
96-
/**
97-
* EN: Product B is able to do its own thing...
98-
*
99-
* RU: Продукт B способен работать самостоятельно...
100-
*/
101-
public:
102-
virtual ~AbstractProductB(){};
103-
104-
virtual std::string UsefulFunctionB() const = 0;
105-
/**
106-
* EN: ...but it also can collaborate with the ProductA.
107-
*
108-
* The Abstract Factory makes sure that all products it creates are of the
109-
* same variant and thus, compatible.
110-
*
111-
* RU: ...а также взаимодействовать с Продуктами Б той же вариации.
112-
*
113-
* Абстрактная Фабрика гарантирует, что все продукты, которые она создает,
114-
* имеют одинаковую вариацию и, следовательно, совместимы.
115-
*/
116-
virtual std::string AnotherUsefulFunctionB(const AbstractProductA& collaborator) const = 0;
56+
class AbstractProductB {
57+
/**
58+
* EN: Product B is able to do its own thing...
59+
*
60+
* RU: Продукт B способен работать самостоятельно...
61+
*/
62+
public:
63+
virtual ~AbstractProductB() {};
64+
virtual std::string UsefulFunctionB() const = 0;
65+
/**
66+
* EN: ...but it also can collaborate with the ProductA.
67+
*
68+
* The Abstract Factory makes sure that all products it creates are of the
69+
* same variant and thus, compatible.
70+
*
71+
* RU: ...а также взаимодействовать с Продуктами Б той же вариации.
72+
*
73+
* Абстрактная Фабрика гарантирует, что все продукты, которые она создает,
74+
* имеют одинаковую вариацию и, следовательно, совместимы.
75+
*/
76+
virtual std::string AnotherUsefulFunctionB(const AbstractProductA &collaborator) const = 0;
11777
};
11878

11979
/**
12080
* EN: Concrete Products are created by corresponding Concrete Factories.
12181
*
12282
* RU: Конкретные Продукты создаются соответствующими Конкретными Фабриками.
12383
*/
124-
125-
class ConcreteProductB1: public AbstractProductB{
126-
public:
127-
128-
std::string UsefulFunctionB() const override{
129-
return "The result of the product B1.";
130-
}
131-
/**
132-
* EN: The variant, Product B1, is only able to work correctly with the
133-
* variant, Product A1. Nevertheless, it accepts any instance of
134-
* AbstractProductA as an argument.
135-
*
136-
* RU: Продукт B1 может корректно работать только с Продуктом A1. Тем не
137-
* менее, он принимает любой экземпляр Абстрактного Продукта А в качестве
138-
* аргумента.
139-
*/
140-
std::string AnotherUsefulFunctionB(const AbstractProductA& collaborator) const override{
141-
const std::string result= collaborator.UsefulFunctionA();
142-
return "The result of the B1 collaborating with ( "+ result+" )";
143-
}
84+
class ConcreteProductB1 : public AbstractProductB {
85+
public:
86+
std::string UsefulFunctionB() const override {
87+
return "The result of the product B1.";
88+
}
89+
/**
90+
* EN: The variant, Product B1, is only able to work correctly with the
91+
* variant, Product A1. Nevertheless, it accepts any instance of
92+
* AbstractProductA as an argument.
93+
*
94+
* RU: Продукт B1 может корректно работать только с Продуктом A1. Тем не
95+
* менее, он принимает любой экземпляр Абстрактного Продукта А в качестве
96+
* аргумента.
97+
*/
98+
std::string AnotherUsefulFunctionB(const AbstractProductA &collaborator) const override {
99+
const std::string result = collaborator.UsefulFunctionA();
100+
return "The result of the B1 collaborating with ( " + result + " )";
101+
}
144102
};
145103

146-
class ConcreteProductB2: public AbstractProductB{
147-
public:
148-
std::string UsefulFunctionB() const override{
149-
return "The result of the product B2.";
150-
}
151-
152-
/**
153-
* EN: The variant, Product B2, is only able to work correctly with the
154-
* variant, Product A2. Nevertheless, it accepts any instance of
155-
* AbstractProductA as an argument.
156-
*
157-
* RU: Продукт B2 может корректно работать только с Продуктом A2. Тем не
158-
* менее, он принимает любой экземпляр Абстрактного Продукта А в качестве
159-
* аргумента.
160-
*/
161-
std::string AnotherUsefulFunctionB(const AbstractProductA& collaborator) const override{
162-
const std::string result= collaborator.UsefulFunctionA();
163-
return "The result of the B2 collaborating with ( "+ result+" )";
164-
}
104+
class ConcreteProductB2 : public AbstractProductB {
105+
public:
106+
std::string UsefulFunctionB() const override {
107+
return "The result of the product B2.";
108+
}
109+
/**
110+
* EN: The variant, Product B2, is only able to work correctly with the
111+
* variant, Product A2. Nevertheless, it accepts any instance of
112+
* AbstractProductA as an argument.
113+
*
114+
* RU: Продукт B2 может корректно работать только с Продуктом A2. Тем не
115+
* менее, он принимает любой экземпляр Абстрактного Продукта А в качестве
116+
* аргумента.
117+
*/
118+
std::string AnotherUsefulFunctionB(const AbstractProductA &collaborator) const override {
119+
const std::string result = collaborator.UsefulFunctionA();
120+
return "The result of the B2 collaborating with ( " + result + " )";
121+
}
165122
};
166123

167-
124+
/**
125+
* EN: The Abstract Factory interface declares a set of methods that return
126+
* different abstract products. These products are called a family and are
127+
* related by a high-level theme or concept. Products of one family are usually
128+
* able to collaborate among themselves. A family of products may have several
129+
* variants, but the products of one variant are incompatible with products of
130+
* another.
131+
*
132+
* RU: Интерфейс Абстрактной Фабрики объявляет набор методов, которые возвращают
133+
* различные абстрактные продукты. Эти продукты называются семейством и связаны
134+
* темой или концепцией высокого уровня. Продукты одного семейства обычно могут
135+
* взаимодействовать между собой. Семейство продуктов может иметь несколько
136+
* вариаций, но продукты одной вариации несовместимы с продуктами другой.
137+
*/
138+
class AbstractFactory {
139+
public:
140+
virtual AbstractProductA *CreateProductA() const = 0;
141+
virtual AbstractProductB *CreateProductB() const = 0;
142+
};
168143

169144
/**
170145
* EN: Concrete Factories produce a family of products that belong to a single
@@ -177,43 +152,31 @@ class ConcreteProductB2: public AbstractProductB{
177152
* сигнатуры методов Конкретной Фабрики возвращают абстрактный продукт, в то
178153
* время как внутри метода создается экземпляр конкретного продукта.
179154
*/
180-
181-
class ConcreteFactory1 : public AbstractFactory
182-
{
183-
public:
184-
AbstractProductA* CreateProductA() const override
185-
{
186-
return new ConcreteProductA1();
187-
}
188-
189-
AbstractProductB* CreateProductB() const override
190-
{
191-
return new ConcreteProductB1();
192-
}
155+
class ConcreteFactory1 : public AbstractFactory {
156+
public:
157+
AbstractProductA *CreateProductA() const override {
158+
return new ConcreteProductA1();
159+
}
160+
AbstractProductB *CreateProductB() const override {
161+
return new ConcreteProductB1();
162+
}
193163
};
164+
194165
/**
195166
* EN: Each Concrete Factory has a corresponding product variant.
196167
*
197168
* RU: Каждая Конкретная Фабрика имеет соответствующую вариацию продукта.
198169
*/
199-
200-
class ConcreteFactory2 : public AbstractFactory
201-
{
202-
public:
203-
AbstractProductA* CreateProductA() const override
204-
{
205-
return new ConcreteProductA2();
206-
}
207-
208-
AbstractProductB* CreateProductB() const override
209-
{
210-
return new ConcreteProductB2();
211-
}
170+
class ConcreteFactory2 : public AbstractFactory {
171+
public:
172+
AbstractProductA *CreateProductA() const override {
173+
return new ConcreteProductA2();
174+
}
175+
AbstractProductB *CreateProductB() const override {
176+
return new ConcreteProductB2();
177+
}
212178
};
213179

214-
215-
216-
217180
/**
218181
* EN: The client code works with factories and products only through abstract
219182
* types: AbstractFactory and AbstractProduct. This lets you pass any factory or
@@ -224,24 +187,24 @@ class ConcreteFactory2 : public AbstractFactory
224187
* любой подкласс фабрики или продукта клиентскому коду, не нарушая его.
225188
*/
226189

227-
void ClientCode(const AbstractFactory& factory){
228-
const AbstractProductA* product_a =factory.CreateProductA();
229-
const AbstractProductB* product_b =factory.CreateProductB();
230-
std::cout << product_b->UsefulFunctionB() << "\n";
231-
std::cout << product_b->AnotherUsefulFunctionB(*product_a) << "\n";
232-
delete product_a;
233-
delete product_b;
190+
void ClientCode(const AbstractFactory &factory) {
191+
const AbstractProductA *product_a = factory.CreateProductA();
192+
const AbstractProductB *product_b = factory.CreateProductB();
193+
std::cout << product_b->UsefulFunctionB() << "\n";
194+
std::cout << product_b->AnotherUsefulFunctionB(*product_a) << "\n";
195+
delete product_a;
196+
delete product_b;
234197
}
235198

236-
int main(){
237-
std::cout << "Client: Testing client code with the first factory type:\n";
238-
ConcreteFactory1* f1= new ConcreteFactory1();
239-
ClientCode(*f1);
240-
delete f1;
241-
std::cout << std::endl;
242-
std::cout << "Client: Testing the same client code with the second factory type:\n";
243-
ConcreteFactory2* f2= new ConcreteFactory2();
244-
ClientCode(*f2);
245-
delete f2;
246-
return 0;
199+
int main() {
200+
std::cout << "Client: Testing client code with the first factory type:\n";
201+
ConcreteFactory1 *f1 = new ConcreteFactory1();
202+
ClientCode(*f1);
203+
delete f1;
204+
std::cout << std::endl;
205+
std::cout << "Client: Testing the same client code with the second factory type:\n";
206+
ConcreteFactory2 *f2 = new ConcreteFactory2();
207+
ClientCode(*f2);
208+
delete f2;
209+
return 0;
247210
}

0 commit comments

Comments
 (0)