【观察者模式】定义了对象之间的一对多依赖,这样一来,当一个对象改变状态时,它的所有依赖者都会收到通知并自动更新。
观察者模式提供了一种对象设计,让主题和观察者之间松耦合。
关于观察者的一切,主题只知道观察者实现了某个接口。主题不需要知道观察者的具体类是谁,做了些什么或其他任何细节。任何时候我们都可以增加新的观察者。因为主题唯一依赖的东西是一个实现Observer接口的对象列表,所以我们可以随时增加观察者。
有新类型的观察者出现,主题的代码不需要修改。假如我们有个新的具体类需要当观察者,我们不需要为了兼容新类型而修改主题的代码。
观察者模式的类图
[img]http://zzuwxf.iteye.com/upload/attachment/63830/a4f08795-a557-30dc-b64c-12a564305c63.jpg[/img]
实现:
1)定义一个主题接口Subject
public interface Subject {
public void registerObserver(Observer observer);
public void removeObserver(Observer observer);
public void notifyObserver();
}
2)定义一个观察者接口Observer
public interface Observer {
public void update(float temp, float humidity, float pressure);
}
3)定义一个具体的观察者类WeatherData(提供数据,注册观察者等)
public class WeatherData implements Subject{
private float temp;
private float humidity;
private float pressure;
private List<Observer> observers = new ArrayList<Observer>();
public void notifyObserver() {
Iterator<Observer> iter = observers.iterator();
while(iter.hasNext()){
Observer o = iter.next();
o.update(temp, humidity, pressure);
}
}
public void registerObserver(Observer observer) {
observers.add(observer);
}
public void removeObserver(Observer observer) {
observers.remove(observer);
}
public void measurementsChanged(){
notifyObserver();
}
public void setMeasurements(float temp, float humidity, float pressure){
this.temp = temp;
this.humidity = humidity;
this.pressure = pressure;
measurementsChanged();
}
}
4)一个具体的观察者CurrentConditionsDisplay
public class CurrentConditionsDisplay implements Observer{
private float temp;
private float humidity;
public void update(float temp, float humidity, float pressure) {
this.temp = temp;
this.humidity = humidity;
display();
}
}
5)测试程序WeatherStation
public class WeatherStation {
public static void main(String[] args){
WeatherData weatherData = new WeatherData();
CurrentConditionsDisplay currentConditionsDisplay= new CurrentConditionsDisplay();
weatherData.registerObserver(currentConditionsDisplay);
weatherData.setMeasurements(80, 65, 30.4f);
weatherData.setMeasurements(82, 71, 20.4f);
weatherData.setMeasurements(78, 67, 14.4f);
}
}
上面展示的例子实际上是主题类以push的方式给观察者数据,而观察者没有选择余地,观察者模式也是观察者以pull的方式从主题那里取得数据。只需要把update方法的参数改为主题的实例即可。
观察者模式提供了一种对象设计,让主题和观察者之间松耦合。
关于观察者的一切,主题只知道观察者实现了某个接口。主题不需要知道观察者的具体类是谁,做了些什么或其他任何细节。任何时候我们都可以增加新的观察者。因为主题唯一依赖的东西是一个实现Observer接口的对象列表,所以我们可以随时增加观察者。
有新类型的观察者出现,主题的代码不需要修改。假如我们有个新的具体类需要当观察者,我们不需要为了兼容新类型而修改主题的代码。
观察者模式的类图
[img]http://zzuwxf.iteye.com/upload/attachment/63830/a4f08795-a557-30dc-b64c-12a564305c63.jpg[/img]
实现:
1)定义一个主题接口Subject
public interface Subject {
public void registerObserver(Observer observer);
public void removeObserver(Observer observer);
public void notifyObserver();
}
2)定义一个观察者接口Observer
public interface Observer {
public void update(float temp, float humidity, float pressure);
}
3)定义一个具体的观察者类WeatherData(提供数据,注册观察者等)
public class WeatherData implements Subject{
private float temp;
private float humidity;
private float pressure;
private List<Observer> observers = new ArrayList<Observer>();
public void notifyObserver() {
Iterator<Observer> iter = observers.iterator();
while(iter.hasNext()){
Observer o = iter.next();
o.update(temp, humidity, pressure);
}
}
public void registerObserver(Observer observer) {
observers.add(observer);
}
public void removeObserver(Observer observer) {
observers.remove(observer);
}
public void measurementsChanged(){
notifyObserver();
}
public void setMeasurements(float temp, float humidity, float pressure){
this.temp = temp;
this.humidity = humidity;
this.pressure = pressure;
measurementsChanged();
}
}
4)一个具体的观察者CurrentConditionsDisplay
public class CurrentConditionsDisplay implements Observer{
private float temp;
private float humidity;
public void update(float temp, float humidity, float pressure) {
this.temp = temp;
this.humidity = humidity;
display();
}
}
5)测试程序WeatherStation
public class WeatherStation {
public static void main(String[] args){
WeatherData weatherData = new WeatherData();
CurrentConditionsDisplay currentConditionsDisplay= new CurrentConditionsDisplay();
weatherData.registerObserver(currentConditionsDisplay);
weatherData.setMeasurements(80, 65, 30.4f);
weatherData.setMeasurements(82, 71, 20.4f);
weatherData.setMeasurements(78, 67, 14.4f);
}
}
上面展示的例子实际上是主题类以push的方式给观察者数据,而观察者没有选择余地,观察者模式也是观察者以pull的方式从主题那里取得数据。只需要把update方法的参数改为主题的实例即可。