观察者的本质含义如下面图:
把上述行为抽像出来即是如下类图:
把上面类图以一下例子来实现。这个例子讲的是,一个气象站的公告电子牌,如果气象站的数据有更新,则各电子公告牌也应该更新,这时气像站是主题,各个电子公告牌是观察者。
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 class WeatherData implements Subject {
private ArrayList observers;
private float temperature;
private float humidity;
private float pressure;
public WeatherData() {
observers = new ArrayList();
}
public void registerObserver(Observer o) {
observers.add(o);
}
public void removeObserver(Observer o) {
int i = observers.indexOf(o);
if (i >= 0) {
observers.remove(i);
}
}
public void notifyObservers() {
for (int i = 0; i < observers.size(); i++) {
Observer observer = (Observer)observers.get(i);
observer.update(temperature, humidity, pressure);
}
}
public void measurementsChanged() {
notifyObservers();
}
public void setMeasurements(float temperature, float humidity, float pressure) {
this.temperature = temperature;
this.humidity = humidity;
this.pressure = pressure;
measurementsChanged();
}
}
public class CurrentConditionsDisplay implements Observer {
private float temperature;
private float humidity;
private Subject weatherData;
public CurrentConditionsDisplay(Subject weatherData) {
this.weatherData = weatherData;
weatherData.registerObserver(this);
}
public void update(float temperature, float humidity, float pressure) {
this.temperature = temperature;
this.humidity = humidity;
display();
}
public void display() {
System.out.println("Current conditions: " + temperature
+ "F degrees and " + humidity + "% humidity");
}
}
public class WeatherStation {
public static void main(String[] args) {
WeatherData weatherData = new WeatherData();
CurrentConditionsDisplay currentDisplay =
new CurrentConditionsDisplay(weatherData);
weatherData.setMeasurements(80, 65, 30.4f);
weatherData.setMeasurements(82, 70, 29.2f);
weatherData.setMeasurements(78, 90, 29.2f);
}
}
观察者模式用在 GUI 的事件处理中,如
JButton button = new JButton("Should I do it?");
button.addActionListener(new AngelListener());
button.addActionListener(new DevilListener());
JButton是一个主题,它注册了一些观察者,如 AngelListerner和DevilListener,其中一个观察者的实现如下:
class AngelListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
System.out.println("Don't do it, you might regret it!");
}
}
这个观察者实现了ActionListener接口,另外注意一下 actionPerformed(ActionEvent event) 方法中的 ActionEvent 对象,为什么会有这么一个参数呢,这是因为观察者模式有两种,一种是“推”,一种是“拉”.
Java 内置的主题必须继承 Observable类,此类有两个重载的notifyObservers方法,分别为:
notifyObservers();
notifyObservers(Object obj); //此方法在通知观察者时,传递一个 Object 对象过去
Java内置的观察者必须实现 Observer接口,此接口中的方法如下:
看到 update 中的 Object arg 对象了吗,这就是主题推送过来的,观察者如需要什么数据就可以从 Object 中获得了。
这就像actionPerformed(ActionEvent event) 方法中的 ActionEvent 对象一样。