观察者模式原理
Internet气象站项目,普通的OO设计方案有问题。
- Internet气象站项目:
- 提供温度、气压和湿度的接口
- 测量数据更新时需试试通知给第三方
- 需要设计开放型API,便于其他第三方公司也能接入气象站获取数据
- WeatherData类
在第三方获取数据的时候,datachange()函数里就要去通知相应的类,造成一个代码的重复。那么在这里的一个解决的办法就是将被通知的类抽象,在气象站类里进行注册,气象站会向在列表中的对象发放通知提醒,这样气象站就不需要重新编译停下来。
原理
- 观察者模式就像定牛奶
- 奶站 subject
- 用户 observer
- Subject:登记注册、移除和通知
- Observer:接收输入
- 观察者模式:对象之间多对一依赖的一种设计方案,被依赖的对象为Subject,依赖的对象为Observer,Subject通知Observer变化。
用观察者重新设计的方案
首先定义subject接口,用于注册删除通知其他观察者
public interface Subject {
public void registerObserver(Observer o);
public void removeObserver(Observer o);
public void notifyObservers();
}
然后定义Observer接口,用于接收更新的数据
public interface Observer {
public void update(float mTemperatrue,float mPressure,float mHumidity);
}
定义subject的气象站实现类 用于发送数据 注册观察者
在这里用了一个ArrayList数组来保存观察者,相当于一个注册表的功能
public class WeatherDataSt implements Subject{
private float mTemperatrue;
private float mPressure;
private float mHumidity;
private ArrayList mObservers;
public WeatherDataSt()
{
mObservers=new ArrayList();
}
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
定义两个观察者实现类,update用于接收数据,display用于打印
public class ForcastConditions 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.mTemperatrue=mTemperatrue;
this.mPressure=mPressure;
this.mHumidity=mHumidity;
display();
}
public void display()
{
System.out.println("**明天温度:"+(mTemperatrue+Math.random())+"**");
System.out.println("**明天气压:"+(mPressure+10*Math.random())+"**");
System.out.println("**明天湿度:"+(mHumidity+Math.random())+"**");
}
}
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 + "***");
}
}
下民是一个测试类
public class InternetWeather {
public static void main(String[] args) {
CurrentConditions mCurrentConditions;
ForcastConditions mForcastConditions;
WeatherDataSt mWeatherDataSt;
mWeatherDataSt=new WeatherDataSt();
mCurrentConditions=new CurrentConditions();
mForcastConditions=new ForcastConditions();
mWeatherDataSt.registerObserver(mCurrentConditions);
mWeatherDataSt.registerObserver(mForcastConditions);
mWeatherDataSt.setData(30, 150, 40);
mWeatherDataSt.removeObserver(mCurrentConditions);
mWeatherDataSt.setData(40, 250, 50);
}
}
其实观察者模式的想法就是基于插销原理,实现一个主类一直运行,然后获取数据的接口类一个可插拔解耦。
Java中的观察者模式
Java中其实也有已经实现的观察者的类,那么subject类集成Observable类,变成一个主站,其他两个接收数据的子类实现Observer接口,成为一个观察者,主站可以调用nofityObservers方法来通知观察者。那么观察者这里接收数据,java给了两个选择,一种是主站推数据,一种是观察者拉数据,选择自己需要的数据进行获取。
下面贴出示例代码 不做解释
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;
}
}
}
public class ForcastConditions 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("***Tomorrow mTemperatrue:" +(mTemperatrue+1)+"***");
System.out.println("***Tomorrow mPressure:" +(mPressure+1)+"***");
System.out.println("***Tomorrow mHumidity:" +(mHumidity+1)+"***");
}
}
public class ForcastConditions 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("***Tomorrow mTemperatrue:" +(mTemperatrue+1)+"***");
System.out.println("***Tomorrow mPressure:" +(mPressure+1)+"***");
System.out.println("***Tomorrow mHumidity:" +(mHumidity+1)+"***");
}
}
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+"***");
}
}
public class InternetWeather {
public static void main(String[] args) {
CurrentConditions mCurrentConditions;
ForcastConditions mForcastConditions;
WeatherData mWeatherData;
mCurrentConditions=new CurrentConditions();
mForcastConditions=new ForcastConditions();
mWeatherData=new WeatherData();
mWeatherData.addObserver(mCurrentConditions);
mWeatherData.addObserver(mForcastConditions);
mWeatherData.setData(30, 150, 40);
mWeatherData.deleteObserver(mCurrentConditions);
mWeatherData.setData(35, 150, 60);
}
}
观察者模式关键点
- 松耦合 高内聚 隔离影响
- java内置观察者使用注意点。Observable是一个类,只能继承,而java又是单继承的,所以要注意这一点。