-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuilder.cpp
More file actions
141 lines (115 loc) · 3.76 KB
/
builder.cpp
File metadata and controls
141 lines (115 loc) · 3.76 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include <iostream>
#include <string>
// ------ Elements of the "complex" object to be constructed ------ //
class Wheel {
public:
Wheel(int p_size = 0): m_size(p_size) {}
int getSize(void) const { return m_size; }
private:
int m_size;
};
class Engine {
public:
Engine(int p_power = 0): m_power(p_power) {}
int getPower(void) const { return m_power; }
private:
int m_power;
};
class Body {
public:
Body(std::string&& p_shape = ""): m_shape(p_shape) {}
std::string getShape(void) const { return m_shape; }
private:
std::string m_shape;
};
// ------ PRODUCT ------ //
/* Defines a common interface to every Products
In that case, a Car interface is only :
- 4 Wheels
- 1 Engine
- 1 Body
- A number of seats
*/
class Car {
public:
void specifications(void) const {
std::cout<< "Body : " << m_body->getShape () << std::endl;
std::cout<< "Power : " << m_engine->getPower () << std::endl;
std::cout<< "Seats : " << m_seats << std::endl;
std::cout<< "Tire Size : " << m_wheels[0]->getSize() << std::endl;
}
void setBody (Body * p_body ) { m_body = p_body; }
void setEngine(Engine* p_engine) { m_engine = p_engine; }
void setWheel (Wheel * p_wheel,
int p_pos) {
if ( p_pos < 4) { m_wheels[p_pos] = p_wheel; }
}
void setSeats (int p_seats) { m_seats = p_seats; }
private:
Wheel * m_wheels[4];
Engine* m_engine;
Body * m_body;
int m_seats;
};
// ------ BUILDER ------ //
class Builder{
public:
virtual Wheel* getWheel (void) { return nullptr; }
virtual Engine* getEngine(void) { return nullptr; }
virtual Body* getBody (void) { return nullptr; }
virtual int getSeats (void) { return 4; }
};
// ------ CONCRETE BUILDER 1 ------ //
class JeepBuilder : public Builder {
public:
Wheel* getWheel (void) override { return new Wheel (40); }
Engine* getEngine(void) override { return new Engine(500); }
Body* getBody (void) override { return new Body("SUV"); }
};
// ------ CONCRETE BUILDER 2 ------ //
class NissanBuilder : public Builder {
public:
Wheel* getWheel (void) override { return new Wheel (12); }
Engine* getEngine(void) override { return new Engine(90); }
Body* getBody (void) override { return new Body("hatchback"); }
};
// ------ DIRECTOR ------ //
class Director {
public:
Director(Builder* p_builder = nullptr) : m_builder(p_builder) {}
void setBuilder(Builder* p_builder) { m_builder = p_builder; }
Car* buildCar(void) {
if ( !m_builder ) { return nullptr; }
Car* car = new Car();
car->setBody ( m_builder->getBody () );
car->setEngine( m_builder->getEngine() );
for ( int i = 0; i < 4; i++ )
car->setWheel(m_builder->getWheel(), i);
car->setSeats ( m_builder->getSeats () );
return car;
}
private:
Builder* m_builder;
};
int main() {
Car* car;
Director director;
JeepBuilder jeepBuilder;
NissanBuilder nissanBuilder;
std::cout<<"--- Jeep ---"<<std::endl;
director.setBuilder(&jeepBuilder);
car = director.buildCar();
if ( car ) {
car->specifications();
delete(car);
}
std::cout<<std::endl;
std::cout<<"--- Nissan ---"<<std::endl;
director.setBuilder(&nissanBuilder);
car = director.buildCar();
if ( car ) {
car->specifications();
delete(car);
}
return 0;
}