天氣預(yù)報項目需求1)氣象站可以將每天測量到的濕度、溫度、氣壓等等以公告的形式發(fā)布出去(比如發(fā)布到自己的網(wǎng)站或第三方)。 通過對氣象站項目的分析,設(shè)計出一個WeatherData類 package com.example.demo.observer;
//顯示當前天氣情況(可以理解成是氣象站自己的網(wǎng)站)
public class CurrentConditions {
// 溫度,氣壓,濕度
private float temperature;
private float pressure;
private float humidity;
//更新 天氣情況,是由 WeatherData 來調(diào)用,我使用推送模式
public void update(float temperature, float pressure, float humidity) {
this.temperature = temperature;
this.pressure = pressure;
this.humidity = humidity; display();
}
//顯示
public void display() {
System.out.println("***Today mTemperature: " + temperature + "***");
System.out.println("***Today mPressure: " + pressure + "***");
System.out.println("***Today mHumidity: " + humidity + "***");
}
}
package com.example.demo.observer;
public class WeatherData {
private float temperatrue;
private float pressure;
private float humidity;
private CurrentConditions currentConditions; //加入新的第三方
public WeatherData(CurrentConditions currentConditions) {
this.currentConditions = currentConditions;
}
public float getTemperature() { return temperatrue;
}
public float getPressure() { return pressure;
}
public float getHumidity() { return humidity;
}
public void dataChange() {
//調(diào)用 接入方的 update
currentConditions.update(getTemperature(), getPressure(), getHumidity());
}
//當數(shù)據(jù)有更新時,就調(diào)用 setData
public void setData(float temperature, float pressure, float humidity) {
this.temperatrue = temperature;
this.pressure = pressure;
this.humidity = humidity;
//調(diào)用 dataChange, 將最新的信息 推送給 接入方 currentConditions
dataChange();
}
}
package com.example.demo.observer;
public class Client {
public static void main(String[] args) {
//創(chuàng)建接入方 currentConditions
CurrentConditions currentConditions = new CurrentConditions();
//創(chuàng)建 WeatherData 并將 接入方 currentConditions 傳遞到 WeatherData 中
WeatherData weatherData = new WeatherData(currentConditions);
//更新天氣情況
weatherData.setData(30, 150, 40);
//天氣情況變化
System.out.println("============天氣情況變化=============");
weatherData.setData(40, 160, 20);
}
}問題分析 : 觀察者模式類似訂牛奶業(yè)務(wù) package com.example.demo.observer.improve;
/**
* 觀察者接口,有觀察者來實現(xiàn)
* @author zhaozhaohai
*
*/
public interface Observer {
public void update(float temperatrue, float pressure, float humidity);
}
package com.example.demo.observer.improve;
public interface Subject {
public void registerObserver(Observer observer);
public void removeObserver(Observer observer);
public void notifyObservers();
}
package com.example.demo.observer.improve;
//顯示當前天氣情況(可以理解成是氣象站自己的網(wǎng)站)
public class CurrentConditions implements Observer{
// 溫度,氣壓,濕度
private float temperature;
private float pressure;
private float humidity;
//更新 天氣情況,是由 WeatherData 來調(diào)用,我使用推送模式
public void update(float temperature, float pressure, float humidity) {
this.temperature = temperature;
this.pressure = pressure;
this.humidity = humidity; display();
}
//顯示
public void display() {
System.out.println("***Today mTemperature: " + temperature + "***");
System.out.println("***Today mPressure: " + pressure + "***");
System.out.println("***Today mHumidity: " + humidity + "***");
}
}
package com.example.demo.observer.improve;
import java.util.ArrayList;
import java.util.List;
/**
* 類是核心
* 1. 包含最新的天氣情況信息
* 2. 含有 觀察者集合,使用ArrayList管理
* 3. 當數(shù)據(jù)有更新時,就主動的調(diào)用 ArrayList,通知所有的(接入方)就看到最新的信息。
* @author zhaozhaohai
*
*/
public class WeatherData implements Subject{
private float temperatrue;
private float pressure;
private float humidity;
private List<Observer> observers;
public WeatherData() {
this.observers = new ArrayList<Observer>();
}
public float getTemperature() { return temperatrue;
}
public float getPressure() { return pressure;
}
public float getHumidity() { return humidity;
}
public void dataChange() {
//調(diào)用 接入方的 update
//currentConditions.update(getTemperature(), getPressure(), getHumidity());
notifyObservers();
}
//當數(shù)據(jù)有更新時,就調(diào)用 setData
public void setData(float temperature, float pressure, float humidity) {
this.temperatrue = temperature;
this.pressure = pressure;
this.humidity = humidity;
//調(diào)用 dataChange, 將最新的信息 推送給 接入方 currentConditions
dataChange();
}
/**
* 注冊一個觀察者
*/
@Override
public void registerObserver(Observer observer) {
// TODO Auto-generated method stub
observers.add(observer);
}
/**
* 移除一個觀察者
*/
@Override
public void removeObserver(Observer observer) {
// TODO Auto-generated method stub
observers.remove(observer);
}
/**
* 遍歷所有的觀察者,并通知
*/
@Override
public void notifyObservers() {
// TODO Auto-generated method stub
observers.stream().forEach(item -> {
item.update(temperatrue, pressure, humidity);
});
}
}
package com.example.demo.observer.improve;
public class BaiduSite implements Observer {
// 溫度,氣壓,濕度
private float temperature;
private float pressure;
private float humidity;
//更新 天氣情況,是由 WeatherData 來調(diào)用,我使用推送模式
public void update(float temperature, float pressure, float humidity) {
this.temperature = temperature;
this.pressure = pressure;
this.humidity = humidity;
display();
}
//顯示
public void display() {
System.out.println("***百度網(wǎng)站 氣溫: " + temperature + "***");
System.out.println("***百度網(wǎng)站 氣壓: " + pressure + "***");
System.out.println("***百度網(wǎng)站 濕度: " + humidity + "***");
}
}
package com.example.demo.observer.improve;
public class Client {
public static void main(String[] args) {
// TODO Auto-generated method stub
// 創(chuàng)建一個WeatherData
WeatherData weatherData = new WeatherData();
// 創(chuàng)建觀察者
CurrentConditions currentConditions = new CurrentConditions();
BaiduSite baiduSite = new BaiduSite();
// 注冊到weatherData
weatherData.registerObserver(currentConditions);
weatherData.registerObserver(baiduSite);
// 測試
System.out.println(" 通知各個注冊的觀察者,看看信息 ");
weatherData.setData(10f, 11f, 12f);
}
}觀察者模式的好處 :
|
|
|