对象之间是多对一依赖的一种设计方案,被依赖的对象为Subject,依赖对象为Observer,Subject通知Observer变化
image.png
Subject: 负责注册——通知——撤销注册
Observer:将自己注册到被观察对象(Subject)中,被观察对象将观察者存放在一个容器(Container)里。
例子:
设计模型:气象站
特点: 气象站负责温度,气压,湿度的收集。等到变化时通知公告板,广播公告等。
public interface Subject {
public void registerObserver(Observer o);
public void removeObserver(Observer o);
public void notifyObservers();
}
public interface Observer {
public void update(float mTemperatrue,float mPressure,float mHumidity);
}
//气象站
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 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) {
// 注册
mObservers.add(o);
}
@Override
public void removeObserver(Observer o) {
// 撤销注册
if(mObservers.contains(o))
{mObservers.remove(o);}
}
@Override
public void notifyObservers() {
//遍历所有观察者,通知天气参数发生变化
for(int i=0,len=mObservers.size();i<len;i++){
mObservers.get(i).update(mTemperatrue, mPressure, mHumidity);
}
}
}
public class CurrentConditions implements Observer {
private float mTemperatrue;
private float mPressure;
private float mHumidity;
@Override
public void update(float mTemperatrue, float mPressure, float mHumidity) {
// 接收气象站的数据并展示
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 ForcastConditions implements Observer{
private float mTemperatrue;
private float mPressure;
private float mHumidity;
@Override
public void update(float mTemperatrue, float mPressure, float mHumidity) {
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 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);
}
}
通过这种设计,将对天气感兴趣的公告板分离出来,自由扩展,这样气象站内部就不用维护各种公告板,可以作为一个独立的服务运行。
松耦合,高内聚,隔离影响
网友评论