-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBuilder.cpp
More file actions
128 lines (106 loc) · 3.23 KB
/
Builder.cpp
File metadata and controls
128 lines (106 loc) · 3.23 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
namespace Product {
namespace Parts {
class Vehicle {
public:
std::string type;
};
class Clan {
public:
std::string name;
};
class Weapons {
public:
std::vector<std::string> list;
};
}
class Transformer {
public:
Parts::Vehicle *vehicle;
Parts::Clan *clan;
Parts::Weapons *weapons;
void getSpecs() {
std::cout << "Vehicle: " << vehicle->type << std::endl;
std::cout << "Clan: " << clan->name << std::endl;
std::cout << "Weapons: " << std::endl;
for (auto it : weapons->list) {
std::cout << it << std::endl;
}
};
};
}
namespace Builder {
class Engineer {
public:
virtual Product::Parts::Vehicle* getVehicle() = 0;
virtual Product::Parts::Clan* getClan() = 0;
virtual Product::Parts::Weapons* getWeapons() = 0;
};
class OptimusPrimeEngineer : public Engineer {
public:
Product::Parts::Vehicle *getVehicle() {
Product::Parts::Vehicle *vehicle = new Product::Parts::Vehicle();
vehicle->type = "tir";
return vehicle;
}
Product::Parts::Clan *getClan() {
Product::Parts::Clan *clan = new Product::Parts::Clan();
clan->name = "Autobot";
return clan;
}
Product::Parts::Weapons *getWeapons() {
Product::Parts::Weapons *weapons = new Product::Parts::Weapons();
weapons->list.push_back("energy ax");
weapons->list.push_back("laser ball");
return weapons;
}
};
class MegatronEngineer : public Engineer {
public:
Product::Parts::Vehicle *getVehicle() {
Product::Parts::Vehicle *vehicle = new Product::Parts::Vehicle();
vehicle->type = "airplane";
return vehicle;
}
Product::Parts::Clan *getClan() {
Product::Parts::Clan *clan = new Product::Parts::Clan();
clan->name = "Decepticon";
return clan;
}
Product::Parts::Weapons *getWeapons() {
Product::Parts::Weapons *weapons = new Product::Parts::Weapons();
weapons->list.push_back("fusion cannon");
return weapons;
}
};
}
class Director {
Builder::Engineer *engineer;
public:
void setEngineer(Builder::Engineer *newEngineer) {
engineer = newEngineer;
}
Product::Transformer *getSpecs() {
Product::Transformer *transformer = new Product::Transformer();
transformer->vehicle = engineer->getVehicle();
transformer->clan = engineer->getClan();
transformer->weapons = engineer->getWeapons();
return transformer;
}
};
int main()
{
Product::Transformer *transformer;
Director director;
Builder::OptimusPrimeEngineer optimusPrimeEngineer;
Builder::MegatronEngineer megatronEngineer;
std::cout << "Optimus Prime" << std::endl;
director.setEngineer(&optimusPrimeEngineer);
transformer = director.getSpecs();
transformer->getSpecs();
std::cout << std::endl;
std::cout << "Megatron" << std::endl;
director.setEngineer(&megatronEngineer);
transformer = director.getSpecs();
transformer->getSpecs();
return 0;
}