观察者模式-气象站

观察者模式,在对象之间定义一对多的依赖,这样一来,当一个对象改变状态,依赖它的对象都会收到通知,并自动更新。

1、代码结构:


[java]  view plain copy
  1. package observer;  
  2.   
  3. /** 
  4.  * 观察者接口 
  5.  */  
  6. public interface Observer {  
  7.       
  8.     /** 
  9.      * 当气象观测值改变时,主题会把这些状态值当作方法的参数,传递给观察者 
  10.      * @param temp 温度 
  11.      * @param humidity 湿度 
  12.      * @param pressure 气压 
  13.      */  
  14.     public void update(float temp, float humidity, float pressure);  
  15. }  

[java]  view plain copy
  1. package observer;  
  2.   
  3. /** 
  4.  * 布告板 
  5.  */  
  6. public interface DisplayElement {  
  7.   
  8.     /** 
  9.      * 当布告板需要显示时,调用此方法 
  10.      */  
  11.     public void display();  
  12. }  

[java]  view plain copy
  1. package observer;  
  2.   
  3. /** 
  4.  * 主题 
  5.  */  
  6. public interface Subject {  
  7.       
  8.     /** 
  9.      * 注册一个观察者 
  10.      * @param o 
  11.      */  
  12.     public void registerObserver(Observer o);  
  13.       
  14.     /** 
  15.      * 删除一个观察者 
  16.      * @param o 
  17.      */  
  18.     public void removeObserver(Observer o);  
  19.       
  20.     /** 
  21.      * 当主题改变的时,这个方法会被调用,以通知所有的观察者 
  22.      */  
  23.     public void notifyObservers();  
  24. }  

[java]  view plain copy
  1. package observer;  
  2.   
  3. import java.util.ArrayList;  
  4.   
  5. /** 
  6.  * 天气数据推送 
  7.  */  
  8. public class WeatherData implements Subject{  
  9.   
  10.     private ArrayList<Observer> observers;//观察者们  
  11.     private float temperature;//温度  
  12.     private float humidity;//湿度  
  13.     private float pressure;//气压  
  14.       
  15.     public WeatherData(){  
  16.         observers = new ArrayList<Observer>();  
  17.     }  
  18.       
  19.     @Override  
  20.     public void registerObserver(Observer o) {  
  21.         observers.add(o);  
  22.     }  
  23.   
  24.     @Override  
  25.     public void removeObserver(Observer o) {  
  26.         observers.remove(0);  
  27.     }  
  28.   
  29.     @Override  
  30.     public void notifyObservers() {  
  31.         for(Observer o : observers){  
  32.             //推送给观察者们  
  33.             o.update(temperature, humidity, pressure);  
  34.         }  
  35.     }  
  36.       
  37.     //检测改变  
  38.     public void measurementsChanged(){  
  39.         notifyObservers();  
  40.     }  
  41.   
  42.     //也可以让观察者自己来取  
  43.     public ArrayList<Observer> getObservers() {  
  44.         return observers;  
  45.     }  
  46.   
  47.     //也可以让观察者自己来取  
  48.     public float getTemperature() {  
  49.         return temperature;  
  50.     }  
  51.   
  52.     //也可以让观察者自己来取  
  53.     public float getPressure() {  
  54.         return pressure;  
  55.     }  
  56.       
  57.     public void setMeasurements(float temperature, float humidity, float pressure){  
  58.         this.temperature = temperature;  
  59.         this.humidity = humidity;  
  60.         this.pressure = pressure;  
  61.         measurementsChanged();  
  62.     }  
  63.   
  64. }  

[java]  view plain copy
  1. package observer;  
  2.   
  3. public class CurrentConditionsDisplay implements Observer, DisplayElement{  
  4.     private float temperature;//温度  
  5.     private float humidity;//湿度  
  6.     private float pressure;//气压  
  7.     private Subject weatherData;  
  8.       
  9.     public CurrentConditionsDisplay(Subject weatherData){  
  10.         this.weatherData = weatherData;  
  11.         weatherData.registerObserver(this);  
  12.     }  
  13.   
  14.     @Override  
  15.     public void update(float temp, float humidity, float pressure) {  
  16.         this.temperature = temp;  
  17.         this.humidity = humidity;  
  18.         this.pressure = pressure;  
  19.         this.display();  
  20.     }  
  21.   
  22.     @Override  
  23.     public void display() {  
  24.         System.out.println("Current conditions:"+temperature +"#" + humidity +"#"+ pressure);  
  25.     }  
  26. }  

[java]  view plain copy
  1. package observer;  
  2.   
  3. /** 
  4.  * 气象站 
  5.  */  
  6. public class WeatherStation {  
  7.     public static void main(String[] args) {  
  8.         WeatherData weatherData = new WeatherData();  
  9.           
  10.         CurrentConditionsDisplay currentDisplay = new CurrentConditionsDisplay(weatherData);  
  11.           
  12.         weatherData.setMeasurements(806578);  
  13.         weatherData.setMeasurements(111111111);  
  14.         weatherData.setMeasurements(22222222);  
  15.           
  16.     }  
  17.     /** 
  18.      * 运行结果: 
  19.      *  Current conditions:80.0#65.0#78.0 
  20.         Current conditions:11.0#111.0#1111.0 
  21.         Current conditions:22.0#222.0#222.0 
  22.      */  
  23. }  

java也有内置的观察者模式,但是这边是使用观察者去拉数据的方式,而不是把数据推给观察者

源代码很少,可以去翻阅查询,在java.util包下面

1、代码结构:


[java]  view plain copy
  1. package observer1;  
  2.   
  3. /** 
  4.  * 布告板 
  5.  */  
  6. public interface DisplayElement {  
  7.   
  8.     /** 
  9.      * 当布告板需要显示时,调用此方法 
  10.      */  
  11.     public void display();  
  12. }  

[java]  view plain copy
  1. package observer1;  
  2.   
  3. import java.util.Observable;  
  4.   
  5. /** 
  6.  * 天气数据,继承了‘可观察者’类 
  7.  */  
  8. public class WeatherData extends Observable{  
  9.   
  10.     private float temperature;//温度  
  11.     private float humidity;//湿度  
  12.     private float pressure;//气压  
  13.       
  14.     //检测改变  
  15.     public void measurementsChanged(){  
  16.         setChanged();//设置已经改变,这样的手动设置可以控制信息的手动推送  
  17.         notifyObservers();  
  18.     }  
  19.       
  20.     public void setMeasurements(float temperature, float humidity, float pressure){  
  21.         this.temperature = temperature;  
  22.         this.humidity = humidity;  
  23.         this.pressure = pressure;  
  24.         measurementsChanged();  
  25.     }  
  26.   
  27.     //也可以让观察者自己来取  
  28.     public float getTemperature() {  
  29.         return temperature;  
  30.     }  
  31.   
  32.     //也可以让观察者自己来取  
  33.     public float getPressure() {  
  34.         return pressure;  
  35.     }  
  36.   
  37.     //也可以让观察者自己来取  
  38.     public float getHumidity() {  
  39.         return humidity;  
  40.     }  
  41. }  

[java]  view plain copy
  1. package observer1;  
  2.   
  3. import java.util.Observable;  
  4. import java.util.Observer;  
  5.   
  6. /** 
  7.  * 显示当前的气象信息 
  8.  */  
  9. public class CurrentConditionsDisplay implements Observer,DisplayElement{  
  10.     Observable observable;//可观察者  
  11.     private float temperature;//温度  
  12.     private float humidity;//湿度  
  13.     private float pressure;//气压  
  14.   
  15.     public CurrentConditionsDisplay(Observable observable){  
  16.         this.observable = observable;  
  17.         observable.addObserver(this);  
  18.     }  
  19.       
  20.     @Override  
  21.     public void update(Observable o, Object arg) {  
  22.         if(o instanceof WeatherData){  
  23.             WeatherData weatherData = (WeatherData) o;  
  24.             //从气象数据中进行取得数据  
  25.             this.temperature = weatherData.getTemperature();  
  26.             this.humidity = weatherData.getHumidity();  
  27.             this.pressure = weatherData.getPressure();  
  28.             display();  
  29.         }  
  30.     }  
  31.   
  32.     @Override  
  33.     public void display() {  
  34.         System.out.println("Current conditions:"+temperature +"#" + humidity +"#"+ pressure);  
  35.     }  
  36. }  

[java]  view plain copy
  1. package observer1;  
  2.   
  3. /** 
  4.  * 气象站 
  5.  */  
  6. public class WeatherStation {  
  7.     public static void main(String[] args) {  
  8.         WeatherData weatherData = new WeatherData();  
  9.           
  10.         CurrentConditionsDisplay currentDisplay = new CurrentConditionsDisplay(weatherData);  
  11.           
  12.         weatherData.setMeasurements(806578);  
  13.         weatherData.setMeasurements(111111111);  
  14.         weatherData.setMeasurements(22222222);  
  15.           
  16.     }  
  17.     /** 
  18.      * 运行结果: 
  19.      *  Current conditions:80.0#65.0#78.0 
  20.         Current conditions:11.0#111.0#1111.0 
  21.         Current conditions:22.0#222.0#222.0 
  22.      */  
  23. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值