观察者模式:对象之间多对一依赖的一种设计方案,被依赖的对象为Subject,依赖的对象为Observer,Subject通知Observer变化
1、Subject:登记注册、移除和通知
2、Observer:接收输入
例子:就是一个气象站,多个公司获取气象站的信息。
Subject:气象站
源码下载:
1、Subject:登记注册、移除和通知
2、Observer:接收输入
例子:就是一个气象站,多个公司获取气象站的信息。
Subject:气象站
Observer:多个公司(观察者)
项目结构:
Subject:
<span style="font-size:18px;">package com.java.hexter.internetweather.observer;
/**
* 气象站接口
*
*/
public interface Subject {
//注册
public void registerObserver(Observer o);
//移除
public void removeObserver(Observer o);
//通知
public void notifyObservers();
}
</span>
Observer:
<span style="font-size:18px;">package com.java.hexter.internetweather.observer;
/**
* 观察者接口
*
*
*/
public interface Observer {
//更新
public void update(float mTemperatrue, float mPressure, float mHumidity);
}
</span>
WeatherDataSt:
<span style="font-size:18px;">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());
}
}
}
</span>
CurrentConditions
<span style="font-size:18px;">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 + "***");
}
}</span>
ForcastConditions
<span style="font-size:18px;">package com.java.hexter.internetweather.mode;
import com.java.hexter.internetweather.observer.Observer;
/**
* 实现观察者接口,第2个观察者
*
*
*/
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()) + "**");
}
}
</span>
InternetWeather
<span style="font-size:18px;">package com.java.hexter.internetweather.mode;
/**
*
* 测试类
*
*/
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);
}
}
</span>
源码下载:
http://download.csdn.net/detail/zhaihaohao1/8890469
相关博客:http://www.cnblogs.com/java-my-life/archive/2012/05/16/2502279.html
参考视频:http://www.jikexueyuan.com/course/528_4.html?ss=1