天氣預(yù)報(bào)項(xiàng)目需求1)氣象站可以將每天測(cè)量到的濕度、溫度、氣壓等等以公告的形式發(fā)布出去(比如發(fā)布到自己的網(wǎng)站或第三方)。 通過對(duì)氣象站項(xiàng)目的分析,設(shè)計(jì)出一個(gè)WeatherData類 package com.example.demo.observer; //顯示當(dāng)前天氣情況(可以理解成是氣象站自己的網(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()); } //當(dāng)數(shù)據(jù)有更新時(shí),就調(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; /** * 觀察者接口,有觀察者來實(shí)現(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; //顯示當(dāng)前天氣情況(可以理解成是氣象站自己的網(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. 當(dāng)數(shù)據(jù)有更新時(shí),就主動(dòng)的調(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(); } //當(dāng)數(shù)據(jù)有更新時(shí),就調(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(); } /** * 注冊(cè)一個(gè)觀察者 */ @Override public void registerObserver(Observer observer) { // TODO Auto-generated method stub observers.add(observer); } /** * 移除一個(gè)觀察者 */ @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)建一個(gè)WeatherData WeatherData weatherData = new WeatherData(); // 創(chuàng)建觀察者 CurrentConditions currentConditions = new CurrentConditions(); BaiduSite baiduSite = new BaiduSite(); // 注冊(cè)到weatherData weatherData.registerObserver(currentConditions); weatherData.registerObserver(baiduSite); // 測(cè)試 System.out.println(" 通知各個(gè)注冊(cè)的觀察者,看看信息 "); weatherData.setData(10f, 11f, 12f); } } 觀察者模式的好處 :
|
|