观察者设计模式(Observer)

Posted by 余腾 on 2019-08-19
Estimated Reading Time 3 Minutes
Words 575 In Total
Viewed Times

什么是观察者设计模式?

观察者设计模式 —> 行为型模式

观察者模式:对象之间多对一依赖的一种设计方案,被依赖的对象为 Subject,依赖的对象为 Observer。

Subject 通知 Observer 变化。

  • Observer:接收输入

  • Subject:登记注册、移除和通知

    • registerObserver:注册
    • removeObserver:移除
    • notifyObservers():通知所有注册用户,可以更新数据,让主动获取,也可以 Subject 时时推送。


Subject

1
2
3
4
5
public interface Subject {
void registerObserver(Observer o);
void removeObserver(Observer o);
void notifyObservers();
}

Observer

1
2
3
public interface Observer {
void update(float temperature, float pressure, float humidity);
}

WeatherData

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
import java.util.ArrayList;

public class WeatherData implements Subject {
private float temperature;
private float pressure;
private float humidity;
private ArrayList<Observer> observers;//观察者集合


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

public float getTemperature() {
return temperature;
}

public float getPressure() {
return pressure;
}

public float getHumidity() {
return humidity;
}

//主动推送
public void dataChange() {
notifyObservers();
}

public void setData(float temperature, float pressure, float humidity) {
this.temperature = temperature;
this.pressure = pressure;
this.humidity = humidity;
dataChange();
}

@Override
public void registerObserver(Observer o) {
observers.add(o);
}

@Override
public void removeObserver(Observer o) {
if (observers.contains(o)) {
observers.remove(o);
}
}

@Override
public void notifyObservers() {
for (int i = 0; i < observers.size(); i++) {
observers.get(i).update(this.temperature, this.pressure, this.humidity);
}
}
}

CurrentConditions

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public class CurrentConditions implements Observer {

private float temperature;
private float pressure;
private float humidity;

public void update(float temperature, float pressure, float humidity) {
this.temperature = temperature;
this.pressure = pressure;
this.humidity = humidity;
display();
}

public void display() {
System.out.println("=========CurrentConditions===========");
System.out.println("Today Temperature: " + temperature);
System.out.println("Today Pressure: " + pressure);
System.out.println("Today Humidity: " + humidity);
}
}

OtherConditions

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public class OtherConditions implements Observer {

private float temperature;
private float pressure;
private float humidity;

public void update(float temperature, float pressure, float humidity) {
this.temperature = temperature;
this.pressure = pressure;
this.humidity = humidity;
display();
}

public void display() {
System.out.println("=========OtherConditions===========");
System.out.println("Other Today Temperature: " + temperature);
System.out.println("Other Today Pressure: " + pressure);
System.out.println("Other Today Humidity: " + humidity);
}
}

Client

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
public class Client {

public static void main(String[] args) {

//创建 Subject WeatherData 数据源
WeatherData weatherData = new WeatherData();
//创建观察者 Observer
CurrentConditions conditions = new CurrentConditions();
//把观察者注册进 Subject
weatherData.registerObserver(conditions);

//再注册进一个
weatherData.registerObserver(new OtherConditions());

System.out.println("========更新数据,并通知观察者========");
weatherData.setData(20L,160L,50L);

System.out.println();
System.out.println("=======Remove CurrentConditions=======");
weatherData.removeObserver(conditions);
weatherData.notifyObservers();
}
}

//TODO
========更新数据,并通知观察者========
=========CurrentConditions===========
Today Temperature: 20.0
Today Pressure: 160.0
Today Humidity: 50.0
=========OtherConditions===========
Other Today Temperature: 20.0
Other Today Pressure: 160.0
Other Today Humidity: 50.0

=======Remove CurrentConditions=======
=========OtherConditions===========
Other Today Temperature: 20.0
Other Today Pressure: 160.0
Other Today Humidity: 50.0

观察者模式 JDK 应用源码分析

感谢阅读


If you like this blog or find it useful for you, you are welcome to comment on it. You are also welcome to share this blog, so that more people can participate in it. If the images used in the blog infringe your copyright, please contact the author to delete them. Thank you !