纵有疾风起
人生不言弃

设计模式–观察者模式

Internet气象站项目:

  提供温度、气压和湿度的接口

  测量数据更新时需时时通知给第三方 需要设计开放型API,便于其他第三方公司也能接入气象站获取数据

WeatherData类

  设计模式–观察者模式插图

一个通常的设计方案

  设计模式–观察者模式插图(1)

代码演示:

WeatherData类

package com.java.hexter.internetweather;public class WeatherData {        private float mTemperatrue;    private float mPressure;    private float mHumidity;    private CurrentConditions mCurrentConditions;    public WeatherData(CurrentConditions mCurrentConditions)    {      this. mCurrentConditions= mCurrentConditions;    }        public float getTemperature()    {        return mTemperatrue;       }        public float getPressure()    {        return mPressure;        }        public float getHumidity()    {        return mHumidity;      }    public void dataChange()    {        mCurrentConditions.update(getTemperature(), getPressure(), getHumidity());    }        public void setData(float mTemperature,float mPressure,float mHumidity)    {        this.mTemperatrue=mTemperature;        this.mPressure=mPressure;        this.mHumidity=mHumidity;        dataChange();    }    }

 代表第三方的类

package com.java.hexter.internetweather;public class CurrentConditions {        private float mTemperature;    private float mPressure;    private float mHumidity;        public void update(float mTemperature,float mPressure,float mHumidity)    {        this.mTemperature=mTemperature;        this.mPressure=mPressure;        this.mHumidity=mHumidity;        display();    }        public void display()    {        System.out.println("***Today mTemperature: "+mTemperature+"***");        System.out.println("***Today mPressure: "+mPressure+"***");        System.out.println("***Today mHumidity: "+mHumidity+"***");    }}

有些问题

  1)其他第三方公司接入气象站获取数据的问题

  2)无法在运行时动态的添加第三方

观察者模式原理

观察者模式就像定牛奶业务

  1)奶站,Subject

  2)用户,Observer

Subject:登记注册、移除和通知

  设计模式–观察者模式插图(2)

Observer:接收输入

  设计模式–观察者模式插图(3)

观察者模式: 对象之间多对一依赖的一种设计方案,被依赖的对象为Subject,依赖的对象为Observer,Subject通知Observer变化

用观察者模式设计重新设计的方案

  设计模式–观察者模式插图(4)

代码演示: 

package com.java.hexter.internetweather.observer;public interface Subject {    public void registerObserver(Observer o);    public void removeObserver(Observer o);    public void notifyObservers();}
package com.java.hexter.internetweather.observer;public interface Observer {    public void update(float mTemperatrue,float mPressure,float mHumidity);}
package com.java.hexter.internetweather.mode;import java.util.ArrayList;import com.java.hexter.internetweather.observer.Observer;import com.java.hexter.internetweather.observer.Subject;public class WeatherDataSt implements Subject{        private float mTemperatrue;    private float mPressure;    private float mHumidity;    private ArrayList<Observer> mObservers;    public WeatherDataSt()    {        mObservers=new ArrayList<Observer>();    }        public float getTemperature()    {        return mTemperatrue;            }    public float getPressure()    {        return mPressure;        }        public float getHumidity()    {        return mHumidity;        }    public void dataChange()    {        notifyObservers();    }        public void setData(float mTemperatrue,float mPressure,float mHumidity)    {        this.mTemperatrue=mTemperatrue;        this.mPressure=mPressure;        this.mHumidity=mHumidity;        dataChange();    }    @Override    public void registerObserver(Observer o) {        // TODO Auto-generated method stub        mObservers.add(o);    }    @Override    public void removeObserver(Observer o) {        // TODO Auto-generated method stub        if(mObservers.contains(o))        {mObservers.remove(o);}    }    @Override    public void notifyObservers() {        // TODO Auto-generated method stub        for(int i=0,len=mObservers.size();i<len;i++)        {            mObservers.get(i).update(getTemperature(), getPressure(), getHumidity());        }    }}
package com.java.hexter.internetweather.mode;import com.java.hexter.internetweather.observer.Observer;public class CurrentConditions implements Observer {//可以有多个子类代表多个第三方    private float mTemperatrue;    private float mPressure;    private float mHumidity;    @Override    public void update(float mTemperatrue, float mPressure, float mHumidity) {        // TODO Auto-generated method stub        this.mHumidity = mHumidity;        this.mPressure = mPressure;        this.mTemperatrue = mTemperatrue;        display();    }    public void display() {        System.out.println("***Today mTemperatrue:" + mTemperatrue + "***");        System.out.println("***Today mPressure:" + mPressure + "***");        System.out.println("***Today mHumidity:" + mHumidity + "***");    }}

 

Java内置的观察者 Observable Observer

用Java内置观察者重新设计该项目

内置观察者的注意点   Observable是类而不是接口

代码如下:

package com.java.hexter.internetweather.jv;import java.util.Observable;public class WeatherData extends Observable{    private float mTemperatrue;    private float mPressure;    private float mHumidity;        public float getTemperature()    {        return mTemperatrue;    }        public float getPressure()    {        return mPressure;      }        public float getHumidity()    {        return mHumidity;        }            public void dataChange()    {        this.setChanged();        this.notifyObservers(new Data(getTemperature(),getPressure(),getHumidity()));            }            public void setData(float mTemperatrue,float mPressure,float mHumidity)    {        this.mTemperatrue=mTemperatrue;        this.mPressure=mPressure;        this.mHumidity=mHumidity;        dataChange();    }        public class Data    {        public float mTemperatrue;        public float mPressure;        public float mHumidity;        public Data(float mTemperatrue,float mPressure,float mHumidity)        {            this.mTemperatrue=mTemperatrue;            this.mPressure=mPressure;            this.mHumidity=mHumidity;        }    }    }
package com.java.hexter.internetweather.jv;import java.util.Observable;import java.util.Observer;import com.java.hexter.internetweather.jv.WeatherData.Data;public class CurrentConditions implements Observer {        private float mTemperatrue;    private float mPressure;    private float mHumidity;    @Override    public void update(Observable arg0, Object arg1) {        // TODO Auto-generated method stub        this.mTemperatrue=((Data)(arg1)).mTemperatrue;        this.mPressure=((Data)(arg1)).mPressure;        this.mHumidity=((Data)(arg1)).mHumidity;        display();    }        public void display()    {        System.out.println("***Today mTemperatrue:" +mTemperatrue+"***");        System.out.println("***Today mPressure:" +mPressure+"***");        System.out.println("***Today mHumidity:" +mHumidity+"***");    }        }

 

文章转载于:https://www.cnblogs.com/lm970585581/p/9591777.html

原著是一个有趣的人,若有侵权,请通知删除

未经允许不得转载:起风网 » 设计模式–观察者模式

分享到: 生成海报
avatar

评论 抢沙发

评论前必须登录!

立即登录   注册

切换注册

登录

忘记密码 ?

切换登录

注册

我们将发送一封验证邮件至你的邮箱, 请正确填写以完成账号注册和激活