-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.cpp
99 lines (78 loc) · 2.16 KB
/
main.cpp
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
/*
* @FileName : abstract_factory/main.cpp
* @CreateAt : 2022/4/4
* @Author : Inno Fang
* @Email : innofang@yeah.net
* @Description: Simple implementation of abstract factory
*/
#include <iostream>
class ProductAInterface {
public:
virtual void doA() = 0;
};
class ProductBInterface {
public:
virtual void doB() = 0;
};
class ConcreteProductA1 : public ProductAInterface {
public:
void doA() override {
std::cout << "Create Product A1" << std::endl;
}
};
class ConcreteProductA2 : public ProductAInterface {
public:
void doA() override {
std::cout << "Create Product A2" << std::endl;
}
};
class ConcreteProductB1 : public ProductBInterface {
public:
void doB() override {
std::cout << "Create Product B1" << std::endl;
}
};
class ConcreteProductB2 : public ProductBInterface {
public:
void doB() override {
std::cout << "Create Product B2" << std::endl;
}
};
class FactoryInterface {
public:
virtual ProductAInterface* createProductA() = 0;
virtual ProductBInterface* createProductB() = 0;
};
class ConcreteFactory1 : public FactoryInterface {
public:
ProductAInterface* createProductA() override {
return new ConcreteProductA1();
}
ProductBInterface* createProductB() override {
return new ConcreteProductB1();
}
};
class ConcreteFactory2 : public FactoryInterface {
public:
ProductAInterface* createProductA() override {
return new ConcreteProductA2();
}
ProductBInterface* createProductB() override {
return new ConcreteProductB2();
}
};
int main() {
std::cout << "==> Case 1:" << std::endl;
FactoryInterface *factory1 = new ConcreteFactory1();
ProductAInterface *product_a1 = factory1->createProductA();
ProductBInterface *product_b1 = factory1->createProductB();
product_a1->doA();
product_b1->doB();
std::cout << "\n==> Case 2:" << std::endl;
FactoryInterface *factory2 = new ConcreteFactory2();
ProductAInterface *product_a2 = factory2->createProductA();
ProductBInterface *product_b2 = factory2->createProductB();
product_a2->doA();
product_b2->doB();
return 0;
}