-
Notifications
You must be signed in to change notification settings - Fork 0
/
abstract_factory.cpp
95 lines (86 loc) · 1.85 KB
/
abstract_factory.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
#include <iostream>
#include <string>
struct Castle {
virtual std::string description() = 0;
};
struct King {
virtual std::string description() = 0;
};
struct Army {
virtual std::string description() = 0;
};
struct ElfCastle : Castle {
std::string description() override {
return "This is elf castle";;
}
};
struct ElfKing : King {
std::string description() override {
return "This is elf king";;
}
};
struct ElfArmy : Army {
std::string description() override {
return "This is elf army";;
}
};
struct OrcCastle : Castle {
std::string description() override {
return "This is orc castle";
}
};
struct OrcKing : King {
std::string description() override {
return "This is orc king";
}
};
struct OrcArmy : Army {
std::string description() override {
return "This is orc army";
}
};
class KingdomFactory {
public:
virtual Castle *createCastle() = 0;
virtual King *createKing() = 0;
virtual Army *createArmy() = 0;
};
class OrcKingdomFactory : public KingdomFactory {
public:
Castle *createCastle() override {
return new OrcCastle();
}
King *createKing() override {
return new OrcKing();
}
Army *createArmy() override {
return new OrcArmy();
}
};
class ElfKingdomFactory : public KingdomFactory {
public:
Castle *createCastle() override {
return new ElfCastle();
}
King *createKing() override {
return new ElfKing();
}
Army *createArmy() override {
return new ElfArmy();
}
};
int main() {
KingdomFactory *factory = new ElfKingdomFactory();
// KingdomFactory *factory = new OrcKingdomFactory();
auto *castle = factory->createCastle();
auto *king = factory->createKing();
auto *army = factory->createArmy();
std::cout << castle->description() << std::endl;
std::cout << king->description() << std::endl;
std::cout << army->description() << std::endl;
delete castle;
delete king;
delete army;
delete factory;
return 0;
}