Skip to content

Commit 59214a1

Browse files
committed
监听者模式普通版完成
1 parent 4db0063 commit 59214a1

9 files changed

Lines changed: 347 additions & 142 deletions

File tree

.idea/workspace.xml

Lines changed: 202 additions & 135 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,38 @@
11
package com.tao.ch2observer.weather;
22

3+
/**
4+
* 实时天气预报展示牌
5+
*/
36
public class CurrentDisplay implements Oberver, DisplayElement {
7+
8+
private WeatherData weatherData;
9+
10+
private float temperature;
11+
private float humidity;
12+
private float pressure;
13+
14+
public CurrentDisplay() {
15+
}
16+
17+
/**
18+
* 构造方法注册到主题
19+
* @param weatherData
20+
*/
21+
public CurrentDisplay(WeatherData weatherData) {
22+
this.weatherData = weatherData;
23+
weatherData.addObserver(this);
24+
}
25+
426
@Override
527
public void display() {
6-
28+
System.out.println("实时天气预报:\n温度:" + temperature + "\n湿度:" + humidity + "\n气压" + pressure + "\n\n" );
729
}
830

931
@Override
1032
public void update(float temperature, float humidity, float pressure) {
11-
33+
this.temperature = temperature;
34+
this.humidity = humidity;
35+
this.pressure = pressure;
36+
display();
1237
}
1338
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.tao.ch2observer.weather;
22

3+
/**
4+
* 展示牌接口
5+
*/
36
public interface DisplayElement {
47
void display();
58
}
Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,41 @@
11
package com.tao.ch2observer.weather;
22

3+
/**
4+
* 气压预报
5+
*/
36
public class ForecastDisplay implements Oberver, DisplayElement {
7+
8+
private float lastPressure = 400f;
9+
private float pressure;
10+
11+
private WeatherData weatherData;
12+
13+
public ForecastDisplay() {
14+
}
15+
16+
/**
17+
* 注册该监听者到主题
18+
* @param weatherData
19+
*/
20+
public ForecastDisplay(WeatherData weatherData) {
21+
this.weatherData = weatherData;
22+
weatherData.addObserver(this);
23+
}
24+
425
@Override
526
public void display() {
6-
27+
if (lastPressure > pressure) {
28+
System.out.println("天气将更加下雨了,请注意收被子\n\n");
29+
}else if(lastPressure < pressure) {
30+
System.out.println("天气将更热,请注意中暑\n\n");
31+
}else {
32+
System.out.println("天气将不发生改变\n\n");
33+
}
734
}
835

936
@Override
1037
public void update(float temperature, float humidity, float pressure) {
11-
38+
this.pressure = pressure;
39+
display();
1240
}
1341
}

ch2-observer/src/main/java/com/tao/ch2observer/weather/Oberver.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.tao.ch2observer.weather;
22

3+
/**
4+
* 观察者接口
5+
*/
36
public interface Oberver {
47

58
void update(float temperature, float humidity, float pressure);
Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,52 @@
11
package com.tao.ch2observer.weather;
22

3+
/**
4+
* 温度统计接口
5+
*/
36
public class StatisDisplay implements Oberver, DisplayElement {
7+
8+
private float maxTemper = 38.0f;
9+
private float minTemper = 25.0f;
10+
private float sumTemper = 100.0f;
11+
private float avgTemper = 0.0f;
12+
private float currTemper;
13+
private int count = 3;
14+
15+
private WeatherData weatherData;
16+
17+
public StatisDisplay() {
18+
}
19+
20+
/**
21+
* 注册监听者到主题
22+
* @param weatherData
23+
*/
24+
public StatisDisplay(WeatherData weatherData) {
25+
this.weatherData = weatherData;
26+
weatherData.addObserver(this);
27+
}
28+
429
@Override
530
public void display() {
6-
31+
System.out.println("温度统计:\n当前温度:" + currTemper + "\n最高温度:" + maxTemper + "\n最低温度:" + minTemper + "\n平均温度" + avgTemper);
732
}
833

934
@Override
1035
public void update(float temperature, float humidity, float pressure) {
1136

37+
currTemper = temperature;
38+
39+
avgTemper = (sumTemper+temperature) / ++count;
40+
41+
if (temperature > maxTemper) {
42+
maxTemper = temperature;
43+
}
44+
45+
if (temperature < minTemper) {
46+
minTemper = temperature;
47+
}
48+
49+
display();
50+
1251
}
1352
}

ch2-observer/src/main/java/com/tao/ch2observer/weather/Subject.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.tao.ch2observer.weather;
22

3+
/**
4+
* 主题接口
5+
*/
36
public interface Subject {
47

58
void addObserver(Oberver oberver);

ch2-observer/src/main/java/com/tao/ch2observer/weather/WeatherData.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package com.tao.ch2observer.weather;
22

3-
import org.omg.CORBA.Object;
4-
53
import java.util.ArrayList;
64
import java.util.List;
75

6+
/**
7+
* 主题
8+
* 天气预报数据中心
9+
*/
810
public class WeatherData implements Subject {
911

1012
private List<Oberver> oberverList;
@@ -16,6 +18,19 @@ public WeatherData() {
1618
this.oberverList = new ArrayList<>();
1719
}
1820

21+
/**
22+
* 更新完天气数据之后,通知监听者更新数据
23+
* @param temperature
24+
* @param humidity
25+
* @param pressure
26+
*/
27+
public void updateData(float temperature,float humidity,float pressure) {
28+
this.temperature = temperature;
29+
this.humidity = humidity;
30+
this.pressure = pressure;
31+
this.notifyObservers();
32+
}
33+
1934
@Override
2035
public void addObserver(Oberver oberver) {
2136
oberverList.add(oberver);
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.tao.ch2observer.weather;
2+
3+
public class main {
4+
5+
public static void main(String[] args) {
6+
7+
//初始化weatherData
8+
WeatherData weatherData = new WeatherData();
9+
10+
//将监听者注册到主题
11+
CurrentDisplay currentDisplay = new CurrentDisplay(weatherData);
12+
ForecastDisplay forecastDisplay = new ForecastDisplay(weatherData);
13+
StatisDisplay statisDisplay = new StatisDisplay(weatherData);
14+
15+
//主题更新数据,进行推送
16+
weatherData.updateData(30,80,400);
17+
weatherData.updateData(40,90,500);
18+
weatherData.updateData(25,70,300);
19+
}
20+
21+
22+
}

0 commit comments

Comments
 (0)