-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest3_ObsDesign.cpp
105 lines (86 loc) · 2.82 KB
/
test3_ObsDesign.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
96
97
98
99
100
101
102
103
104
105
#include <chrono>
#include <iostream>
#include <random>
#include <unistd.h>
#include <vector>
// Interface for Observer: Display Unit observes
class IDisplayUnit {
std::string mName;
public:
virtual void notify(int16_t temperature){};
virtual std::string printName() const = 0;
virtual ~IDisplayUnit(){};
};
class ConsoleDisplay : public IDisplayUnit {
std::string mConsoleName;
public:
ConsoleDisplay(const std::string name) { mConsoleName = name; }
// Notify method called from Observer.
void notify(int16_t temperature) override {
std::cout << "Temperature updated: " << temperature << "°C" << std::endl;
}
std::string printName() const override { return mConsoleName; }
};
class CarHeadUnit : public IDisplayUnit {
std::string mConsoleName;
std::string mCarName;
public:
CarHeadUnit(const std::string carName) {
mCarName = carName;
mConsoleName = carName + " HeadUnit";
}
void notify(int16_t temperature) override {
std::cout << "Temperature updated: " << temperature << "°C" << std::endl;
}
std::string printName() const override { return mConsoleName; }
std::string carName() const { return mCarName; }
};
// Subject: notifies observers as soon as something changes
// TODO: use smart pointers instead of raw pointers.
class WeatherStation {
std::vector<IDisplayUnit *> mObservers;
std::int16_t mTemperature;
public:
void setTemperature(std::int16_t temperature) {
mTemperature = temperature;
auto start = std::chrono::high_resolution_clock::now();
notify_observers();
auto stop = std::chrono::high_resolution_clock::now();
auto duration =
std::chrono::duration_cast<std::chrono::microseconds>(stop - start);
// To get the value of duration use the count()
// member function on the duration object
std::cout << duration.count() << " us." << std::endl;
}
void registerObserver(IDisplayUnit *dispUnit) {
mObservers.push_back(dispUnit);
}
void notify_observers() {
for (auto *obs : mObservers) {
if (auto carUnit = dynamic_cast<CarHeadUnit *>(obs)) {
std::cout << "Car name: " << carUnit->carName() << std ::endl;
}
std::cout << "Notifiying " << obs->printName() << std::endl;
obs->notify(mTemperature);
}
}
};
int main() {
ConsoleDisplay disp1("Living room"), disp2("Kitchen");
CarHeadUnit disp3("Nissan"), disp4("Toyota");
WeatherStation station;
station.registerObserver(&disp1);
station.registerObserver(&disp2);
station.registerObserver(&disp3);
station.registerObserver(&disp4);
// Generate random temperatures from -55 to +55 C.s
std::random_device rd;
std::mt19937 e2(rd());
std::uniform_int_distribution<int> dist(-55, 55);
for (auto i = 0; i <= 50; i++) {
auto curr_temperature = dist(e2);
station.setTemperature(curr_temperature);
usleep(200000);
}
return 0;
}