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
145
146
147
#include <iostream>
#include <vector>
#include <algorithm>
#include <memory>

// 抽象观察者接口
class Observer {
public:
virtual ~Observer() = default;
virtual void update(float temperature, float humidity, float pressure) = 0;
};

// 抽象主题接口
class Subject {
public:
virtual ~Subject() = default;
virtual void addObserver(std::shared_ptr<Observer> observer) = 0;
virtual void removeObserver(std::shared_ptr<Observer> observer) = 0;
virtual void notifyObservers() = 0;
};

// 具体主题实现
class WeatherData : public Subject {
private:
std::vector<std::shared_ptr<Observer>> observers;
float temperature;
float humidity;
float pressure;

public:
void addObserver(std::shared_ptr<Observer> observer) override {
observers.push_back(observer);
}

void removeObserver(std::shared_ptr<Observer> observer) override {
auto it = std::find(observers.begin(), observers.end(), observer);
if (it != observers.end()) {
observers.erase(it);
}
}

void notifyObservers() override {
for (auto& observer : observers) {
observer->update(temperature, humidity, pressure);
}
}

void setMeasurements(float temp, float humidity, float pressure) {
this->temperature = temp;
this->humidity = humidity;
this->pressure = pressure;
measurementsChanged();
}

private:
void measurementsChanged() {
notifyObservers();
}

public:
float getTemperature() const { return temperature; }
float getHumidity() const { return humidity; }
float getPressure() const { return pressure; }
};

// 具体观察者实现 - 当前条件显示
class CurrentConditionsDisplay : public Observer {
private:
float temperature;
float humidity;
std::shared_ptr<Subject> weatherData;

public:
CurrentConditionsDisplay(std::shared_ptr<Subject> weatherData)
: weatherData(weatherData) {
weatherData->addObserver(shared_from_this());
}

void update(float temperature, float humidity, float pressure) override {
this->temperature = temperature;
this->humidity = humidity;
display();
}

void display() const {
std::cout << "Current conditions: " << temperature
<< "F degrees and " << humidity << "% humidity" << std::endl;
}
};

// 具体观察者实现 - 统计数据显示
class StatisticsDisplay : public Observer, public std::enable_shared_from_this<StatisticsDisplay> {
private:
float maxTemp = 0.0f;
float minTemp = 200.0f;
float tempSum = 0.0f;
int numReadings = 0;
std::shared_ptr<Subject> weatherData;

public:
StatisticsDisplay(std::shared_ptr<Subject> weatherData)
: weatherData(weatherData) {
weatherData->addObserver(shared_from_this());
}

void update(float temperature, float humidity, float pressure) override {
tempSum += temperature;
numReadings++;

if (temperature > maxTemp) {
maxTemp = temperature;
}

if (temperature < minTemp) {
minTemp = temperature;
}

display();
}

void display() const {
std::cout << "Avg/Max/Min temperature = " << (tempSum / numReadings)
<< "/" << maxTemp << "/" << minTemp << std::endl;
}
};

// 使用示例
int main() {
// 创建主题
auto weatherData = std::make_shared<WeatherData>();

// 创建观察者
auto currentDisplay = std::make_shared<CurrentConditionsDisplay>(weatherData);
auto statisticsDisplay = std::make_shared<StatisticsDisplay>(weatherData);

// 模拟数据变化
std::cout << "First weather update:" << std::endl;
weatherData->setMeasurements(80, 65, 30.4f);

std::cout << "\nSecond weather update:" << std::endl;
weatherData->setMeasurements(82, 70, 29.2f);

std::cout << "\nThird weather update:" << std::endl;
weatherData->setMeasurements(78, 90, 29.2f);

return 0;
}

重点:一对多