-
Notifications
You must be signed in to change notification settings - Fork 5
/
facade.cpp
81 lines (69 loc) · 1.87 KB
/
facade.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
// Facade design pattern
#include <iostream>
// complex systems
class Engine {
public:
void start() const { std::cout << "Starting the engine.\n"; }
void stop() const { std::cout << "Stopping the engine.\n"; }
};
class Hand_Brake {
public:
void pull() const { std::cout << "Pulling the hand brake.\n"; }
void release() const { std::cout << "Releasing the hand brake.\n"; }
};
class Gear_Box {
public:
void park() const { std::cout << "Setting the gearbox to PARK.\n"; }
void drive() const { std::cout << "Setting the gearbox to DRIVE.\n"; }
void reverse() const { std::cout << "Setting the gearbox to REVERSE.\n"; }
void neutral() const { std::cout << "Setting the gearbox to NEUTRAL.\n"; }
};
// facade interface
struct IFacade {
virtual void start() const = 0;
virtual void stop() const = 0;
virtual ~IFacade() = default;
};
// concrete facades
class Car_Facade : public IFacade {
Engine engine_{};
Hand_Brake hand_brake_{};
Gear_Box gear_box_{};
public:
void start() const {
std::cout << "STARTING THE CAR\n";
engine_.start();
hand_brake_.release();
gear_box_.drive();
std::cout << '\n';
}
void stop() const {
std::cout << "STOPPING THE CAR\n";
gear_box_.park();
hand_brake_.pull();
engine_.stop();
std::cout << '\n';
}
};
class Scooter_Facade : public IFacade {
Engine engine_{};
public:
void start() const {
std::cout << "STARTING THE SCOOTER\n";
engine_.start();
std::cout << '\n';
}
void stop() const {
std::cout << "STOPPING THE SCOOTER\n";
engine_.stop();
std::cout << '\n';
}
};
int main() {
Car_Facade car_facade;
Scooter_Facade scooter_facade;
car_facade.start();
car_facade.stop();
scooter_facade.start();
scooter_facade.stop();
}