观察者模式: 定义了对象之间的一对多依赖,这样一来。当一个对象(被观察者)改变状态时,它的全部依赖者(观察者)都会收到通知并自己主动更新。
在观察者模式中,会改变的是主题的状态,以及观察者的数目和类型。
用这个模式, 你能够改变依赖主题状态的对象。却不必改变主题。这叫提前规划。
主题和观察者都使用接口:观察者利用主题的接口向主题注冊。而主题利用观察者的接口通知观察者。这样能够让两者之间运作正常,有同一时候具有松耦合的长处。
观察者模式利用“组合”将很多观察者组合进主题中。对象之间的这样的关系不是通过继承产生的,而是在执行时利用组合的方式而产生的。
以下来看观察者设计模式的一个完整Demo
package observer; /** * 被观察者接口 * @author Arvon * */ public interface Subject { public void registerObserver(Observer o); public void removeObserver(Observer o); public void notifyObservers(); }
package observer; /** * 观察者接口 * @author Administrator * */ public interface Observer { public void update(float temp,float humidity, float pressure); }
package observer; /** * 布告板接口 * @author Administrator * */ public interface DisplayElement { public void display(); }
package observer; import java.util.ArrayList; import java.util.List; /** * 被观察者的实现类 * 主题 * @author Administrator * */ public class WeatherData implements Subject { private List<Observer> observersList; private float pressure; private float humidity; private float temp; public WeatherData() { super(); observersList = new ArrayList<Observer>(); } @Override public void registerObserver(Observer o) { observersList.add(o); } @Override public void removeObserver(Observer o) { int i = observersList.indexOf(o); if(i>=0) observersList.remove(i); } @Override public void notifyObservers() { for (Observer observer : observersList) { observer.update(temp, humidity, pressure); } } public void setMeasurements(float temp, float humidity, float pressure){ this.temp = temp; this.humidity = humidity; this.pressure = pressure; messurementsChanged(); } /** * 当新的天气数据送来时。通知观察者 */ private void messurementsChanged() { // TODO Auto-generated method stub notifyObservers(); } }
package observer; /** * 眼下天气状况 布告板 * @author Administrator * */ public class CurrentConditionsDisplay implements Observer, DisplayElement { private float pressure; private float humidity; private float temp; private WeatherData weatherData; public CurrentConditionsDisplay(WeatherData weatherData) { super(); this.weatherData =weatherData; weatherData.registerObserver(this); } @Override public void display() { System.out.println("Current conditions:" + temp + "F Degrees and " + humidity + "% humidity"); } @Override public void update(float temp, float humidity, float pressure) { this.temp = temp; this.humidity = humidity; this.pressure = pressure; display(); } }
package observer; /** * 气候统计 * @author Administrator * */ public class StatisticsDisplay implements DisplayElement, Observer { @Override public void update(float temp, float humidity, float pressure) { // TODO Auto-generated method stub } @Override public void display() { // TODO Auto-generated method stub } }
package observer; /** * 天气预报 * @author Administrator * */ public class ForecastDisplay implements DisplayElement, Observer { @Override public void update(float temp, float humidity, float pressure) { // TODO Auto-generated method stub } @Override public void display() { // TODO Auto-generated method stub } }
package observer; /** * 測试类 * @author Administrator * */ public class WeatherStation { public static void main(String[] args) { WeatherData weatherData = new WeatherData(); CurrentConditionsDisplay cDisplay = new CurrentConditionsDisplay(weatherData); weatherData.setMeasurements(80.0f, 65, 30.4f); } }
程序的输出 : Current conditions:80.0F Degrees and 65.0% humidity