* 主題
*/
public interface Subject {
public void registerObserver(Observer o);
public void removeObserver(Observer o);
public void notifyObservers();
}
* 觀察者
*/
public interface Observer {
public void update(float temp, float humidity, float pressure);
}
* 佈告板
*/
public interface DisplayElement {
public void display();
}
//創建主題
WeatherData weatherData = new WeatherData();
//創建三個觀察者
CurrentConditionsDisplay currentDisplay = new CurrentConditionsDisplay(weatherData);
StatisticsDisplay statisticsDisplay = new StatisticsDisplay(weatherData);
ForecastDisplay forecastDisplay = new ForecastDisplay(weatherData);
HeatIndexDisplay heatIndexDisplay = new HeatIndexDisplay(weatherData);
//進行氣象測量,主題的狀態會因爲測量值而改變
weatherData.setMeasurements(80, 65, 30.4f);
weatherData.setMeasurements(82, 70, 29.2f);
weatherData.setMeasurements(78, 90, 29.2f);
}
本文出自 “熔 巖” 博客,請務必保留此出處http://lavasoft.blog.51cto.com/62575/201617