-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathmain.cpp
More file actions
73 lines (63 loc) · 1.68 KB
/
Copy pathmain.cpp
File metadata and controls
73 lines (63 loc) · 1.68 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
#include <iostream>
class IDoor {
public:
virtual void GetDescription() = 0;
};
class WoodenDoor : public IDoor {
public:
void GetDescription() override {
std::cout << "I am a wooden door" << std::endl;
}
};
class IronDoor : public IDoor {
public:
void GetDescription() override {
std::cout << "I am a iron door" << std::endl;
}
};
class IDoorFittingExpert {
public:
virtual void GetDescription() = 0;
};
class Carpenter : public IDoorFittingExpert {
void GetDescription() override {
std::cout << "I can only fit wooden doors" << std::endl;
}
};
class Welder : public IDoorFittingExpert {
void GetDescription() override {
std::cout << "I can only fit iron doors" << std::endl;
}
};
class IDoorFactory {
public:
virtual IDoor* MakeDoor() = 0;
virtual IDoorFittingExpert* MakeFittingExpert() = 0;
};
template <typename Door, typename DoorFittingExpert>
class DoorFactory : public IDoorFactory {
public:
IDoor* MakeDoor() override {
return new Door();
}
IDoorFittingExpert* MakeFittingExpert() override {
return new DoorFittingExpert();
}
};
int main()
{
IDoorFactory* woodenFactory = new DoorFactory<WoodenDoor, Carpenter>();
{
IDoor* door = woodenFactory->MakeDoor();
IDoorFittingExpert* expert = woodenFactory->MakeFittingExpert();
door->GetDescription();
expert->GetDescription();
}
IDoorFactory* ironFactory = new DoorFactory<IronDoor, Welder>();
{
IDoor* door = ironFactory->MakeDoor();
IDoorFittingExpert* expert = ironFactory->MakeFittingExpert();
door->GetDescription();
expert->GetDescription();
}
}