观察者模式

观察者模式定义了对象之间的一对多依赖关系,当对象改变状态,其他依赖者都会收到通知。


主题接口

1
2
3
4
5
6
7
8
public interface Subject {

public void registerObserver(Observer o);

public void removeObserver(Observer o);

public void notifyObservers();
}

观察者接口

1
2
3
4
public interface Observer {

public void update(float temp, float humidity, float pressure);
}

ConcreteSubject

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
@Data
public class WeatherData implements Subject {

private ArrayList<Observer> observers;
private float temperature;
private float humidity;
private float pressure;

public WeatherData() {
observers = new ArrayList<Observer>();
}

@Override
public void registerObserver(Observer o) {

observers.add(o);
}

@Override
public void removeObserver(Observer o) {

observers.remove(o);
}

@Override
public void notifyObservers() {

for (Observer o : observers) {
o.update(temperature, humidity, pressure);
}
}

public void measurementsChanged() {
notifyObservers();
}

public void setMeasurements(float temperature, float humidity, float pressure) {
this.temperature = temperature;
this.humidity = humidity;
this.pressure = pressure;
measurementsChanged();
}

}

ConcreteObserver

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class ForecastDisplay implements Observer, DisplayElement {
private float currentPressure = 29.92f;
private float lastPressure;

public void update(float temp, float humidity, float pressure) {
lastPressure = currentPressure;
currentPressure = pressure;

display();
}

public void display() {
System.out.print("Forecast: ");
if (currentPressure > lastPressure) {
System.out.println("Improving weather on the way!");
} else if (currentPressure == lastPressure) {
System.out.println("More of the same");
} else if (currentPressure < lastPressure) {
System.out.println("Watch out for cooler, rainy weather");
}
}
}
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
public class StatisticsDisplay implements Observer, DisplayElement {
private float maxTemp = 0.0f;
private float minTemp = 200;
private float tempSum= 0.0f;
private int numReadings;

public void update(float temp, float humidity, float pressure) {
tempSum += temp;
numReadings++;

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

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

display();
}

public void display() {
System.out.println("Avg/Max/Min temperature = " + (tempSum / numReadings)
+ "/" + maxTemp + "/" + minTemp);
}
}

Test

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
@GetMapping("/observer")
public RestResponse<Object> observer() {

// 主题
WeatherData weatherData=new WeatherData();

// 对象
Observer forecastDisplay=new ForecastDisplay();
Observer statisticsDisplay=new StatisticsDisplay();

// 注册观察者
weatherData.registerObserver(forecastDisplay);
weatherData.registerObserver(statisticsDisplay);

weatherData.setMeasurements(1, 2, 3);
weatherData.setMeasurements(4, 5, 6);

return RestResponse.success();
}
1
2
3
4
Forecast: Watch out for cooler, rainy weather
Avg/Max/Min temperature = 1.0/1.0/1.0
Forecast: Improving weather on the way!
Avg/Max/Min temperature = 2.5/4.0/1.0

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×