观察者模式(observer pattern)(订报)

 

定义:定义了对象之间的一对多依赖,这样以来,当一个对象改变状态时,他的所有依赖者都会受到通知并自动更新。

 

笔记:关键是一对多,就像订阅报纸一样,订阅了的就可以收到报纸,没有订阅的就收不到咯。

 

 

观察者接口

package com.tj.observer.observers; public interface Observer { void update(float temp,float humidity, float pressure); }  

 

打印接口

package com.tj.observer.observers; public interface DisplayElement { void display(); }  

 

观察者实现类1,当前天气状况

/** * */ package com.tj.observer.observers; import com.tj.observer.subject.Subject; /** * @author 2172980000502 * */ public class CurrentConditionsDisplay implements Observer, DisplayElement { private float temperature; private float humidity; public CurrentConditionsDisplay(Subject weatherData) { // TODO Auto-generated constructor stub weatherData.registerObserver(this); } /** * @see com.tj.observer.observers.Observer#update(float, float, float) */ @Override public void update(float temp, float humidity, float pressure) { // TODO Auto-generated method stub this.temperature = temp; this.humidity = humidity; display(); } /** * @see com.tj.observer.observers.DisplayElement#display() */ @Override public void display() { // TODO Auto-generated method stub System.out.println("CurrentConditionsDisplay: start."); System.out.println("temperature : " + temperature + " degrees"); System.out.println("humidity : " + humidity + "%"); System.out.println("CurrentConditionsDisplay: end."); System.out.println("---------------------------"); } }  

 

观察者实现类2,天气预报

/** * */ package com.tj.observer.observers; import com.tj.observer.subject.WeatherData; /** * @author 2172980000502 * */ public class ForecastDisplay implements Observer, DisplayElement { private float currentPressure = 29.92f; private float lastPressure; public ForecastDisplay(WeatherData weatherData) { // TODO Auto-generated constructor stub weatherData.registerObserver(this); } /** * @see com.tj.observer.observers.Observer#update(float, float, float) */ @Override public void update(float temp, float humidity, float pressure) { // TODO Auto-generated method stub lastPressure = currentPressure; currentPressure = pressure; display(); } /** * @see com.tj.observer.observers.DisplayElement#display() */ @Override public void display() { // TODO Auto-generated method stub System.out.println("ForecastDisplay: start."); if (currentPressure > lastPressure) { System.out.println("Improving weather on the way!"); } else if (currentPressure == lastPressure) { System.out.println("More of the same"); } else if (currentPressure < lastPressure) { System.out.println("Watch out for cooler, rainy weather"); } System.out.println("ForecastDisplay: end."); System.out.println("---------------------------"); } }  

 

观察者实现类3,天气热度

/** * */ package com.tj.observer.observers; import com.tj.observer.subject.WeatherData; /** * @author 2172980000502 * */ public class HeatIndexDisplay implements Observer, DisplayElement { float heatIndex = 0.0f; public HeatIndexDisplay(WeatherData weatherData) { // TODO Auto-generated constructor stub weatherData.registerObserver(this); } /** * @see com.tj.observer.observers.Observer#update(float, float, float) */ @Override public void update(float temp, float humidity, float pressure) { // TODO Auto-generated method stub heatIndex = computeHeatIndex(temp, humidity); display(); } /** * @see com.tj.observer.observers.DisplayElement#display() */ @Override public void display() { // TODO Auto-generated method stub System.out.println("Heat index is " + heatIndex); System.out.println("---------------------------"); } private float computeHeatIndex(float t, float rh) { float index = (float) ((16.923 + (0.185212 * t) + (5.37941 * rh) - (0.100254 * t * rh) + (0.00941695 * (t * t)) + (0.00728898 * (rh * rh)) + (0.000345372 * (t * t * rh)) - (0.000814971 * (t * rh * rh)) + (0.0000102102 * (t * t * rh * rh)) - (0.000038646 * (t * t * t)) + (0.0000291583 * (rh * rh * rh)) + (0.00000142721 * (t * t * t * rh)) + (0.000000197483 * (t * rh * rh * rh)) - (0.0000000218429 * (t * t * t * rh * rh)) + 0.000000000843296 * (t * t * rh * rh * rh)) - (0.0000000000481975 * (t * t * t * rh * rh * rh))); return index; } }  

 

观察者实现类4,天气统计

/** * */ package com.tj.observer.observers; import com.tj.observer.subject.WeatherData; /** * @author 2172980000502 * */ public class StatisticsDisplay implements Observer, DisplayElement { private float maxTemp = 0.0f; private float minTemp = 200; private float tempSum = 0.0f; private int numReadings; public StatisticsDisplay(WeatherData weatherData) { // TODO Auto-generated constructor stub weatherData.registerObserver(this); } /** * @see com.tj.observer.observers.Observer#update(float, float, float) */ @Override public void update(float temp, float humidity, float pressure) { // TODO Auto-generated method stub tempSum += temp; numReadings++; if (temp > maxTemp) { maxTemp = temp; } if (temp < minTemp) { minTemp = temp; } display(); } /** * @see com.tj.observer.observers.DisplayElement#display() */ @Override public void display() { // TODO Auto-generated method stub System.out.println("Avg/Max/Min temperature = " + (tempSum / numReadings) + "/" + maxTemp + "/" + minTemp); System.out.println("---------------------------"); } }  

 

主题(被观察者)接口

package com.tj.observer.subject; import com.tj.observer.observers.Observer; public interface Subject { void registerObserver(Observer o); void removeObserver(Observer o); void notifyObservers(); }  

 

主题(被观察者)实现类

/** * */ package com.tj.observer.subject; import java.util.ArrayList; import com.tj.observer.observers.Observer; /** * @author 2172980000502 * */ public class WeatherData implements Subject { private ArrayList<Observer> observers; private float temperature; private float humidity; private float pressure; public WeatherData() { // TODO Auto-generated constructor stub observers = new ArrayList<Observer>(); } /** * @see com.tj.observer.subject.Subject#notifyObservers() */ @Override public void notifyObservers() { // TODO Auto-generated method stub for (int i = 0; i < observers.size(); i++) { Observer observer = (Observer) observers.get(i); observer.update(temperature, humidity, pressure); } } /** * @see com.tj.observer.subject.Subject#registerObserver(com.tj.observer.observers.Observer) */ @Override public void registerObserver(Observer o) { // TODO Auto-generated method stub observers.add(o); } /** * @see com.tj.observer.subject.Subject#removeObserver(com.tj.observer.observers.Observer) */ @Override public void removeObserver(Observer o) { // TODO Auto-generated method stub int i = observers.indexOf(o); if (i >= 0) { observers.remove(i); } } public void measurementsChanged() { notifyObservers(); } public void setMeasurements(float temperature, float humidity, float pressure) { this.temperature = temperature; this.humidity = humidity; this.pressure = pressure; measurementsChanged(); } // other WeatherData methods here public float getTemperature() { return temperature; } public float getHumidity() { return humidity; } public float getPressure() { return pressure; } }  

 

应用入口

package com.tj.observer; import com.tj.observer.observers.CurrentConditionsDisplay; import com.tj.observer.observers.ForecastDisplay; import com.tj.observer.observers.HeatIndexDisplay; import com.tj.observer.observers.StatisticsDisplay; import com.tj.observer.subject.WeatherData; public class WeatherStation { public static void main(String[] args) { WeatherData weatherData = new WeatherData(); new CurrentConditionsDisplay(weatherData); new StatisticsDisplay(weatherData); new ForecastDisplay(weatherData); new HeatIndexDisplay(weatherData); weatherData.setMeasurements(80, 65, 30.4f); System.out.println("****************************************************************************************"); weatherData.setMeasurements(82, 70, 29.2f); System.out.println("****************************************************************************************"); weatherData.setMeasurements(78, 90, 29.2f); } } 

你可能感兴趣的:(String,Class,float,interface,Constructor,methods)