JAVA设计格局之观看者格局

气象站系统:气象站能够透过物理装置取得气象音信,WeatherData对象足以由此气象站提供的表面接口获取气象音讯。

要求:WeatherData拿到最新数据时,需要实时更新多少个通告板的场景数据。两个公告板分别是:近期情景、气象总计、天气预报。

 

Java,一、整理分析:

1.WeatherData有多少个getter方法getTemperature(),getHumidity(),getPressure()分别得到温度、湿度、气压值;

2.当数码变动时,WeatherData有个方法measurementsChanged()会被调用(我们不关注咋样被调用),此时需立异六个布告板的气象消息;

3.系列需可扩充,可天天新增或者去除通知板。

 

率先次尝试:

 1 public class WeatherData {
 2     
 3     public void measurementsChanged() {
 4         float temperature = getTemperature();
 5         float humidity = getHumidity();
 6         float pressure = getPressure();
 7         //目前状况布告板更新
 8         currentConditionsDisplay.update(temperature, humidity, pressure);
 9         //气象统计布告板更新
10         statisticsDisplay.update(temperature, humidity, pressure);
11         //天气预报布告板更新
12         forecastDisplay.update(temperature, humidity, pressure);
13     }
14     
15     //WeatherData的其他方法
16     
17 }

留存的题材:针对落实编程,会难以扩充和保障,如若急需新增或者去除布告板,必须修改该程序。

 

诸如报社,订阅报纸后,报社会定期给你邮寄报纸,截止订阅后报社便停下邮寄。大家把报社改称为“主旨Subject”,把订阅者改成为“观望者 Observer”来钻探观看者形式。

观望者情势定义:定义了对象期间的一对多倚重,这样一来,当一个目标改变时,它的富有依赖者都会收下公告并自动更新。

大旨不需要通晓观看者具体是什么人,只需要了解寓目者实现了Observer接口,并不爱惜观看者具体做了怎么着事,这种相互对象间的松耦合是规划中追求的准绳,下边看核心Subject和寓目者Observer接口代码:

1 /*观察者*/
2 public interface Observer {
3     public void update(float temp, float humidity, float pressure);
4 }

1 /*主题*/
2 public interface Subject {
3     /*注册成为观察者*/
4     public void registerObserver(Observer o);
5     /*移除观察者*/
6     public void removeObserver(Observer o);
7     /*通知所有观察者*/
8     public void notifyObservers();
9 }   

1 public interface DisplayElement {
2     /**
3      * 布告板都实现该接口,调用display来展示气象信息
4      */
5     public void display();
6 }

前些天让WeatherData实现主题接口:

 1 public class WeatherData implements Subject {
 2     private List<Observer> observerList;
 3     private float temperature;
 4     private float humidity;
 5     private float pressure;
 6     
 7     public WeatherData() {
 8         observerList = new ArrayList<Observer>();
 9     }
10     public void registerObserver(Observer o) {
11         observerList.add(o);
12     }
13 
14     public void removeObserver(Observer o) {
15         int i = observerList.indexOf(o);
16         if (i >= 0) {
17             observerList.remove(i);
18             System.out.println(o.getClass().getName() + " is removed.");
19         }
20     }
21 
22     public void notifyObservers() {
23         for (Observer o : observerList) {
24             o.update(temperature, humidity, pressure);
25         }
26     }
27     /*数据更新时,通知所有观察者*/
28     public void measurementsChanged() {
29         notifyObservers();
30     }
31 
32 }

通知板实现观看者接口:

 1 /*目前状况布告板*/
 2 public class CurrentConditionsDisplay implements Observer, DisplayElement {
 3 
 4     private float temperature;
 5     private float humidity;
 6     private Subject weatherData;
 7     
 8     public CurrentConditionsDisplay(Subject subject) {
 9         this.weatherData = subject;
10         weatherData.registerObserver(this);
11     }
12     
13     public void removeObserver() {
14         weatherData.removeObserver(this);
15     }
16 
17     public void update(float temp, float humidity, float pressure) {
18         this.temperature = temp;
19         this.humidity = humidity;
20         display();
21     }
22     
23     public void display() {
24         System.out.println("CurrentConditionsDisplay conditions:" + temperature + "F degrees and " + humidity + "% humidity");
25     }
26 
27 }
28 
29 /*天气预报布告板*/
30 public class ForecastDisplay implements Observer, DisplayElement {
31 
32     private float temperature;
33     private float pressure;
34     private Subject weatherData;
35     
36     public ForecastDisplay(Subject subject) {
37         this.weatherData = subject;
38         weatherData.registerObserver(this);
39     }
40     
41     public void removeObserver() {
42         weatherData.removeObserver(this);
43     }
44 
45     public void update(float temp, float humidity, float pressure) {
46         this.temperature = temp;
47         this.pressure = pressure;
48         display();
49     }
50     
51     public void display() {
52         System.out.println("ForecastDisplay conditions:" + temperature + "F degrees and " + pressure + "Pa");
53     }
54 
55 }
56 
57 /*气象状况布告板略*/

代码测试:

 1 public class ObserverTest {
 2     
 3     @Test
 4     public void test() {
 5         WeatherData weatherDate = new WeatherData();
 6         CurrentConditionsDisplay ccDisplay = new CurrentConditionsDisplay(weatherDate);
 7         ForecastDisplay fcDisplay = new ForecastDisplay(weatherDate);
 8         weatherDate.setMeasurements(12.3f, 38.6f, 130f);
 9         ccDisplay.removeObserver();
10         weatherDate.setMeasurements(12.3f, 38.6f, 130f);
11         fcDisplay.removeObserver();
12         weatherDate.setMeasurements(12.3f, 38.6f, 130f);
13         System.out.println("notify ok");
14     }
15 }

运行结果:

1 CurrentConditionsDisplay conditions:12.3F degrees and 38.6% humidity
2 ForecastDisplay conditions:12.3F degrees and 130.0Pa
3 com.project.design.observer.CurrentConditionsDisplay is removed.
4 ForecastDisplay conditions:12.3F degrees and 130.0Pa
5 com.project.design.observer.ForecastDisplay is removed.
6 notify ok

 

Java
JDK中也存在已经定义好的观望者形式,在java.util包中(Observable(大旨),
Observer(旁观者)),我们有趣味可以友善询问下。java中的观察者可以手动从核心中拉取数据,核心也足以主动去推送数据。

相关文章