• Internet气象站项目:
  • 提供温度、气压和温度的接口
  • 测量数据更新时需实时的通知第三方
  • 需要设计开放性API,便于其他第三方公司也能接入气象站获取数据
  • 传统的设计方案:
package guanchazhe;


/**
* Created with IntelliJ IDEA.
* Description:
* User: wjx
* Date: 2019-04-14
* Time: 13:08
*/
public class CurrentConditions {
private float mTempature;
private float mPressure;
private float mHumidity;
public void update(float mTempature, float mPressure, float mHumidity){
this.mTempature = mTempature;
this.mPressure = mPressure;
this.mHumidity = mHumidity;
display();
}
public void display(){
System.out.println("温度"+mTempature);
System.out.println("气压"+mPressure);
System.out.println("湿度"+mHumidity);
}
}
package guanchazhe;




/**
* Created with IntelliJ IDEA.
* Description:
* User: wjx
* Date: 2019-04-14
* Time: 13:06
*/
public class Weather {
private float mTemperate;
private float mPressure;
private float mHumidity;
private CurrentConditions currentConditions;
public Weather(CurrentConditions currentConditions){
this.currentConditions = currentConditions;
}
public void dataChange(){
currentConditions.update(getmTemperate(),getmPressure(),getmHumidity());
}
public float getmTemperate() {
return mTemperate;
}




public float getmPressure() {
return mPressure;
}




public float getmHumidity() {
return mHumidity;
}




public void setData(float mTemperate, float mPressure, float mHumidity){
this.mTemperate = mTemperate;
this.mPressure = mPressure;
this.mHumidity = mHumidity;
dataChange();
}


}
package guanchazhe;


/**
* Created with IntelliJ IDEA.
* Description:
* User: wjx
* Date: 2019-04-14
* Time: 13:14
*/
public class InternetWeather {
public static void main(String[] args) {
CurrentConditions currentConditions = new CurrentConditions();
Weather weather = new Weather(currentConditions);
weather.setData(30,150,40);
}
}
  • 有些问题
              1)其他第三方公司接入气象站获取数据的问题
              2)无法在运行时动态的添加第三方
  • 观察者模式:
  • 订牛奶业务:到奶站告诉订牛奶登记注册,
  • 奶站,Subject
  • 用户,Observer
  • Subject: 登记注册、移除和通知
  • Observer: 接受输入
  • 观察者模式的使用:对象之间多对一依赖的一种设计方案,被依赖的对象为Subject, 依赖的对象为Observer, Subject通知Observer变化
  • interface
package guanchazhe.update;


/**
* Created with IntelliJ IDEA.
* Description:
* User: wjx
* Date: 2019-04-14
* Time: 14:04
*/
public interface Observer {
public void update(float mTemperature, float mPressure, float mHumidity);
}
package guanchazhe.update;


/**
* Created with IntelliJ IDEA.
* Description:
* User: wjx
* Date: 2019-04-14
* Time: 14:07
*/
public interface Subject {
public void registerObserver(Observer observer);//注册观察者
public void removeObserver(Observer observer);//删除观察者
public void notifyObservers();//通知观察者
}
  • Subject
package guanchazhe.update;


import java.util.ArrayList;


/**
* Created with IntelliJ IDEA.
* Description:
* User: wjx
* Date: 2019-04-14
* Time: 14:12
*/
public class Weather implements Subject {
private float mTemperate;
private float mPressure;
private float mHumidity;
//定义观察者列表
private ArrayList<Observer> mObservers;


public Weather(){
mObservers = new ArrayList<Observer>();
}


public void dataChange(){
//通知是要放到数据改变中
notifyObservers();
}


public float getmTemperate() {
return mTemperate;
}


public float getmPressure() {
return mPressure;
}


public float getmHumidity() {
return mHumidity;
}


public void setData(float mTemperate, float mPressure, float mHumidity){
this.mTemperate = mTemperate;
this.mPressure = mPressure;
this.mHumidity = mHumidity;
dataChange();
}


@Override
public void registerObserver(Observer observer) {
mObservers.add(observer);
}


@Override
public void removeObserver(Observer observer) {
if(mObservers.contains(observer)){
mObservers.remove(observer);
}
}


@Override
public void notifyObservers() {
//通知所有对象有新的信息,获取每一个对象更新
for(int i = 0;i<mObservers.size();i++){
mObservers.get(i).update(getmTemperate(), getmPressure(),getmHumidity());
}
}
}
  • 实现观察者
package guanchazhe.update;


/**
* Created with IntelliJ IDEA.
* Description:
* User: wjx
* Date: 2019-04-14
* Time: 14:08
*/
public class CurrentConditions implements Observer {
private float mTemperature;
private float mPressure;
private float mHumidity;


@Override
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("温度" + mTemperature);
System.out.println("气压"+ mPressure);
System.out.println("湿度" + mHumidity);
}
}
package guanchazhe.update;


/**
* Created with IntelliJ IDEA.
* Description:
* User: wjx
* Date: 2019-04-14
* Time: 14:11
*/
public class ForcastConditions implements Observer {
private float mTemperature;
private float mPressure;
private float mHumidity;


@Override
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("明温度" + mTemperature+1);
System.out.println("明气压"+ mPressure+1);
System.out.println("明湿度" + mHumidity+1);
}
}
  • 测试
package guanchazhe.update;


/**
* Created with IntelliJ IDEA.
* Description:
* User: wjx
* Date: 2019-04-14
* Time: 14:23
*/
public class InternetWeather {
public static void main(String[] args) {
CurrentConditions currentConditions;
ForcastConditions forcastConditions;
Weather weather;
weather = new Weather();
currentConditions = new CurrentConditions();
forcastConditions = new ForcastConditions();


//注册观察者
weather.registerObserver(currentConditions);
weather.registerObserver(forcastConditions);
weather.setData(30,150,40);
weather.removeObserver(forcastConditions);
weather.setData(30, 120,40);
}


}