-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path9.observer_pattern.h
123 lines (109 loc) · 3.42 KB
/
9.observer_pattern.h
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
//
// Created by Tianyi Zhang on 12/19/20.
//
#ifndef DESIGN_PATTERN_9_OBSERVER_PATTERN_H
#define DESIGN_PATTERN_9_OBSERVER_PATTERN_H
#include <memory>
#include <vector>
#include <iostream>
struct IObserver{
virtual void getUpdates() = 0;
virtual ~IObserver(){};
};
struct IObserverable{
virtual void add(std::shared_ptr<IObserver> observer) = 0;
virtual void remove(std::shared_ptr<IObserver> observer) = 0;
virtual void notify() = 0;
};
struct AlertStation : public IObserverable{
void add(std::shared_ptr<IObserver> observer) override{
observerList.push_back(observer);
}
void remove(std::shared_ptr<IObserver> observer) override{
for(int i=0; i<observerList.size(); i++) {
if(observerList[i] == observer)
observerList.erase(observerList.begin() + i);
}
}
void setAlert(bool alertStatus){
Alert = alertStatus;
notify();
}
bool Alert;
private:
void notify() override{
for(auto observer : observerList)
observer->getUpdates();
}
std::vector<std::shared_ptr<IObserver>> observerList;
};
struct WeatherStation : public IObserverable, public IObserver{
WeatherStation() = default;
WeatherStation(std::shared_ptr<AlertStation> alertStation){
this->alertStation = alertStation;
}
void add(std::shared_ptr<IObserver> observer) override{
observerList.push_back(observer);
}
void remove(std::shared_ptr<IObserver> observer) override{
for(int i=0; i<observerList.size(); i++) {
if(observerList[i] == observer)
observerList.erase(observerList.begin() + i);
}
}
void setNewTemp(double newTemperature, int timeSerial){
Temperature = newTemperature;
TimeSerial = timeSerial;
notify();
}
double Temperature;
int TimeSerial;
void getUpdates() override{
this->Alert = alertStation->Alert;
notify();
}
bool Alert;
private:
void notify() override{
for(auto observer : observerList)
observer->getUpdates();
}
std::vector<std::shared_ptr<IObserver>> observerList;
std::shared_ptr<AlertStation> alertStation;
};
struct MonitorDisplay : public IObserver{
MonitorDisplay(std::shared_ptr<WeatherStation> weatherStation){
this->weatherStation = weatherStation;
}
void getUpdates() override{
this->displayTemperature = weatherStation->Temperature;
this->timeSerial = weatherStation->TimeSerial;
this->alert = weatherStation->Alert;
display();
}
private:
void display(){
std::cout<<"Monitor display Temperature : "<< displayTemperature << "; TimeSerial : " << timeSerial << std::endl;
std::cout<<"Alert status: "<< alert<<std::endl;
}
double displayTemperature;
double timeSerial;
bool alert;
std::shared_ptr<WeatherStation> weatherStation;
};
struct PhoneDisplay : public IObserver{
PhoneDisplay(std::shared_ptr<WeatherStation> weatherStation){
this->weatherStation = weatherStation;
}
void getUpdates() override{
this->displayTemperature = weatherStation->Temperature;
display();
}
private:
void display(){
std::cout<<"Phone display Temperature : "<< displayTemperature << std::endl;
}
double displayTemperature;
std::shared_ptr<WeatherStation> weatherStation;
};
#endif //DESIGN_PATTERN_9_OBSERVER_PATTERN_H