观察者(observer)模式定义了一对多的依赖关系,让多个观察者对象能够同时监听某一主题对象。这个主题对象中的状态发生改变时,就会通知所有的观察者对象。
观察者模式的结构图:
结构中各个部分的含义:
- 抽象主题类(Subject):它把所有对观察者对象的引用都保存在一个聚集内,每个主题可以有任意多的观察者。
- 具体主题类(ConcreteSubject):具体主题,将有关状态存入具体观察者对象;当具体主题状态改变时,向所有观察者发出通知。
- 抽象观察者类(Observer):抽象观察者,为所有的具体观察者定义一个接口。
- 具体观察者类(ConcreteObserver):具体观察者,实现抽象观察者角色所要求的接口,以便更新本身的状态
源代码:
抽象主题类(Subject):
public interface Subject {
public void registerObserver(Observer o); //增加观察者
public void removeObserver(Observer o); //删除观察者
public void notifyObserver(String newState);//通知观察者
}
具体主题类(ConcreteSubject):
public class ConcreteSubject implements Subject{ private ArrayList<Observer> observers; //观察者集合 public ConcreteSubject() {
observers = new ArrayList<Observer>();
} @Override
public void registerObserver(Observer o) {
observers.add(o);
} @Override
public void removeObserver(Observer o) {
observers.remove(o);
} @Override
public void notifyObserver(String newstate) {
for (Observer observer : observers) {
observer.update(newstate);
}
} public void getChange(String newState){
notifyObserver(newState);
}
}
抽象观察者类(Observer):
public interface Observer {
public void update(String state);
}
抽象观察者类(ConcreteObserver):
public class ConcreteObserver implements Observer{ @Override
public void update(String state) {
System.out.println("更新后状态为:"+ state);
} }
客户端:
public class Client {
public static void main(String[] args) {
ConcreteSubject s = new ConcreteSubject();
Observer observer = new ConcreteObserver();
s.registerObserver(observer);
s.getChange("新状态");
}
}
运行结果:
更新后状态为:新状态
具体情境举例:气象站的温度变化更新:
抽象主题类(Subject):
public interface Subject {
public void registerObserver(Observer o);
public void removeObserver(Observer o);
public void notifyObservers();
}
具体主题类:
public class CurrentConditionDisplay implements Observer,DisplayElement{ private float temperature;
private float humidity;
private Subject weatherData; public CurrentConditionDisplay(Subject weatherData) {
this.weatherData = weatherData;
weatherData.registerObserver(this);
} @Override
public void display() {
System.out.println("当前气温:"+ temperature + "F 湿度为:"+ humidity + "%");
} @Override
public void update(float temperature, float humidity, float pressure) {
this.temperature = temperature;
this.humidity = humidity;
display();
} }
displayElement接口:
public interface DisplayElement {
public void display();
}
抽象观察者类(Observer):
public interface Observer {
public void update(float temperature, float humidity, float pressure);
}
具体观察者类(ConcreteObserver):
public class WeatherData implements Subject{ private ArrayList<Observer> observers; //观察者
//要更新的观察者的信息
private float temperature;
private float humidity;
private float pressure; public WeatherData() {
observers = new ArrayList<Observer>();
} @Override
public void registerObserver(Observer o) {
observers.add(o);
} @Override
public void removeObserver(Observer o) {
int i = observers.indexOf(o);
if (i >= 0 ) {
observers.remove(i);
}
} @Override
public void notifyObservers() {
// for(int i = 0; i < observers.size(); i++){
// Observer observer = (Observer) observers.get(i);
// observer.update(temperature, humidity, pressure);
// }
for (Observer o : observers) {
o.update(temperature, humidity, pressure);
}
} public void measurementsChanged(){
notifyObservers();
} public void setMeasurement(float temperature, float humidity, float pressure){
this.temperature = temperature;
this.humidity = humidity;
this.pressure = pressure;
measurementsChanged();
}
}
客户端:
public class WeatherStation {
public static void main(String[] args){
WeatherData weatherData = new WeatherData(); CurrentConditionDisplay currentConditionDisplay = new CurrentConditionDisplay(weatherData); weatherData.setMeasurement(80, 65, 30.4f);
weatherData.setMeasurement(82, 70, 29.2f);
weatherData.setMeasurement(78, 90, 29.2f);
}
}
运行结果:
当前气温:80.0F 湿度为:65.0%
当前气温:82.0F 湿度为:70.0%
当前气温:78.0F 湿度为:90.0%