JAVA设计形式之观察者形式

气象局系统:气象台能够经过物理装置获得气象新闻,WeatherData对象足以由此气象局提供的外表接口获取气象新闻。

要求:WeatherData获得最新数据时,供给实时更新七个公告板的情景数据。几个文告板分别是:近年来情景、气象总括、天气预测。

 

风度翩翩、收拾解析:

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”来研商观看者格局。

旁观者情势定义:定义了目的之间的豆蔻梢头对多信赖,那样一来,当三个对象改变时,它的持有信任者都会摄取公告并自动更新。

lovebet下载,宗旨不须要理解观察者具体是何人,只需求明白阅览者完成了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中的观看者能够手动从主旨中拉取数据,大旨也得以积极去推送数据。