-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathairTrafficControl.cpp
More file actions
144 lines (117 loc) · 3.04 KB
/
airTrafficControl.cpp
File metadata and controls
144 lines (117 loc) · 3.04 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
142
143
144
/*
* C++ Design Patterns: Mediator
* The following source code was based on: github.com/JakubVojvoda
*
*
* Source code is licensed under MIT License
* (for more details see LICENSE)
*
*/
#include <iostream>
#include <vector>
#include <string>
class trafficControl;
/*
* Vehicle class (abstract)
* each Vehicle communicates with (/through) its mediator (/the air traffic tower)
* (with other Vehicles)
*/
class Vehicle
{
public:
Vehicle(trafficControl* const m, const unsigned int i) :
mediator(m), id(i) {}
virtual ~Vehicle() {}
unsigned int getID()
{
return id;
}
virtual void send_new_altitude(std::string) = 0;
virtual void receive(std::string) = 0;
protected:
trafficControl* mediator;
unsigned int id;
};
// airVehicle class implements Vehicle (abstract) class
class airVehicle : public Vehicle
{
public:
airVehicle(trafficControl* const m, const unsigned int i) :
Vehicle(m, i) {}
~airVehicle() {}
void send_new_altitude(std::string msg);
void receive(std::string msg)
{
std::cout << "New altitude: '" << msg << "' received by Vehicle " << id << std::endl;
}
};
/*
* trafficControl (abstract) class is the Mediator
* defines an interface for communication among Vehicle objects
*/
class trafficControl
{
public:
virtual ~trafficControl() {}
virtual void add(Vehicle* const c) = 0;
virtual void distribute(Vehicle* const sender, std::string msg) = 0;
protected:
trafficControl() {}
};
/*
* Tower implements (abstract) trafficControl class
* implements cooperative behavior by coordinating Vehicle objects
*
*/
class Tower : public trafficControl
{
public:
~Tower()
{
std::vector<Vehicle*>().swap(vehicle_group);
// free memory
}
void add(Vehicle* const c)
{
vehicle_group.push_back(c);
}
void distribute(Vehicle* const sender, std::string msg)
{
for (unsigned int i = 0; i < vehicle_group.size(); i++)
{
if (vehicle_group.at(i)->getID() != sender->getID())
{
vehicle_group.at(i)->receive(msg);
}
}
}
private:
std::vector<Vehicle*> vehicle_group;
};
void airVehicle::send_new_altitude(std::string msg)
{
std::cout << std::endl << "Message '" << msg << "' sent by Vehicle " << id << std::endl;
mediator->distribute(this, msg);
}
int main()
{
trafficControl* mediator = new Tower();
Vehicle* aircraft01 = new airVehicle(mediator, 1);
Vehicle* aircraft02 = new airVehicle(mediator, 2);
Vehicle* drone01 = new airVehicle(mediator, 3);
Vehicle* helicopter01 = new airVehicle(mediator, 4);
mediator->add(aircraft01);
mediator->add(aircraft02);
mediator->add(drone01);
mediator->add(helicopter01);
aircraft01->send_new_altitude("3500m");
aircraft02->send_new_altitude("5000m");
helicopter01->send_new_altitude("50m");
// free memory
delete mediator;
delete aircraft01;
delete aircraft02;
delete drone01;
delete helicopter01;
return 0;
}