Java内置的观察者
1、Observable有注册,移除,通知的方法
2、Observer:接收输入
注意:Observable是类而不是接口
例子:就是一个气象站,多个公司获取气象站的信息。
Observable:气象站
ForcastConditions
相关博客:http://www.cnblogs.com/java-my-life/archive/2012/05/16/2502279.html
参考视频:http://www.jikexueyuan.com/course/528_4.html?ss=1
1、Observable有注册,移除,通知的方法
2、Observer:接收输入
注意:Observable是类而不是接口
例子:就是一个气象站,多个公司获取气象站的信息。
Observable:气象站
Observer:多个公司(观察者)
项目结构:
WeatherData
<span style="font-size:18px;">package com.java.hexter.internetweather.jv;
import java.util.Observable;
/**
* 气象站,被观察者,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;
}
}
}
</span>
CurrentConditions
<span style="font-size:18px;">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 + "***");
}
}
</span>
ForcastConditions
<span style="font-size:18px;">package com.java.hexter.internetweather.jv;
import java.util.Observable;
import java.util.Observer;
import com.java.hexter.internetweather.jv.WeatherData.Data;
/**
* 观察者
*
*
*/
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) + "***");
}
}
</span>
InternetWeather
<span style="font-size:18px;">package com.java.hexter.internetweather.jv;
/**
*
*测试类
*
*/
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);//通知
}
}
</span>
源码下载:http://download.csdn.net/detail/zhaihaohao1/8890473相关博客:http://www.cnblogs.com/java-my-life/archive/2012/05/16/2502279.html
参考视频:http://www.jikexueyuan.com/course/528_4.html?ss=1