设计模式之二:观察者模式(java内置(气象站模拟流程))

观察者模式:定义了对象之间的一对多依赖,这样一来,当一个对象改变状态时,它的所有依赖者都会收到通知并自动更新。 当你试图勾勒观察者模式时,可以利用报纸订阅服务,以及出版者和订阅者比拟这一切。

设计原则:为了交互对象之间的松耦合设计为努力。

注意:java内置的Observable是一个类,要想一个类成为可观察者,就必须继承Observable类,但这样就限制了这个类的复用潜力了。

工程名为:SubjectAndObseverInJDK,下载目录:http://www.cnblogs.com/jrsmith/admin/Files.aspx , SubjectAndObseverInJDK.zip

 1 package com.jyu.implement;
 2 
 3 import java.util.Observable;
 4 
 5 /**
 6  * 可观察者
 7  * @author JRSmith
 8  *
 9  */
10 public class WeatherData  extends Observable {
11 
12     private float temperature;
13     private float humidity;
14     private float pressure;
15     
16     public WeatherData(){    }
17     
18     /**注意:我们没有调用notifyObservers()传送数据对象,这表示我们采用的作法是"拉"*/
19     public void measurementChanged(){
20         setChanged();//超类方法:指示状态已经改变
21         notifyObservers();//超类方法
22     }
23     
24     public void setMeasurements(float temperature, float humidity, float pressure){
25         this.temperature = temperature;
26         this.humidity = humidity;
27         this.pressure = pressure;
28         measurementChanged();
29     }
30 
31     /**
32      * @return the temperature
33      */
34     public float getTemperature() {
35         return temperature;
36     }
37 
38     /**
39      * @return the humidity
40      */
41     public float getHumidity() {
42         return humidity;
43     }
44 
45     /**
46      * @return the pressure
47      */
48     public float getPressure() {
49         return pressure;
50     }
51 
52 }
 1 package com.jyu.implement;
 2 
 3 import java.util.Observable;
 4 import java.util.Observer;
 5 
 6 import com.jyu.interfaces.DisplayElement;
 7 
 8 /**
 9  * 观察者
10  * @author JRSmith
11  *
12  */
13 public class CurrentConditionDisplay implements Observer, DisplayElement {
14 
15     Observable observable;
16     private float temperature;
17     private float humidity;
18     
19     /**构造器需要weatherData对象(也就是主题)作为注册之用*/
20     public CurrentConditionDisplay(Observable observable){
21         this.observable = observable;
22         observable.addObserver(this);
23     }
24     
25     @Override
26     public void update(Observable obs,Object arg) {
27         if(obs instanceof WeatherData){
28             WeatherData weatherData = (WeatherData)obs;
29             this.temperature = weatherData.getTemperature();
30             this.humidity = weatherData.getHumidity();
31             display();
32         }
33     }
34 
35     @Override
36     public void display() {
37         System.out.println("Currnet conditions:"+temperature+"F degree and "+humidity+"%humidity");
38     }
39 }
View Code
 1 package com.jyu.interfaces;
 2 
 3 /**
 4  * 布告板接口
 5  * @author JRSmith
 6  *
 7  */
 8 public interface DisplayElement {
 9     /**当布告板需要显示时,调用此方法*/
10     public void display();
11 }
View Code
 1 package com.jyu.test;
 2 
 3 import com.jyu.implement.CurrentConditionDisplay;
 4 import com.jyu.implement.WeatherData;
 5 
 6 public class WeatherStation {
 7 
 8     /**
 9      * @param args
10      */
11     public static void main(String[] args) {
12         WeatherData weatherData = new WeatherData();
13         
14         CurrentConditionDisplay currentConditionDisplay = new CurrentConditionDisplay(weatherData);
15         
16         weatherData.setMeasurements(80, 65, 30.4f);
17     }
18 
19 }
原文地址:https://www.cnblogs.com/damonhuang/p/2682794.html