什么是观察者模式
观察者模式定义了对象之间一种一对多依赖关系,使得每当一个对象状态发生改变时,其相关依赖对象都能收到通知并自动刷新。
观察者模式主要包含以下几个角色:
Subject(目标):指被观察的对象,在目标中定义了一个观察者集合,一个观察目标可以接受任意数量的观察者来观察。提供了一系列方法来增加,删除观察者对象,同时也定义了通知方法。目标类可以是接口也可以是抽象类。
ConcreteSubject(具体目标):目标类的子类,通常包含经常发生改变的数据,当他的状态发生改变时,向他的各个观察者发出通知。
Observer(观察者):对观察目标的改变做出反应,观察者一般定义为接口,该接口声明了更新数据的方法。
ConcreteObserver(具体观察者):维护了一个指定具体目标对象的引用,他存储具体观察者的有关状态,这些状态和具体目标的状态要保持一致。
观察者模式的优缺点
优点
- 可以实现表示层和数据逻辑层的分离,定义了稳定的消息更新传递机制,并抽象了更新接口,使得可以有各种各样的表示层充当具体观察者。
- 在观察目标和观察者之间建立了一个抽象的耦合。观察目标只需要维持一个抽象观察者的集合,无需了解具体观察者。
- 支持广播通信,观察目标会向所有已注册的观察者对象发送通知。
- 符合开闭原则,增加新的观察者对象无需修改代码。
缺点
- 如果一个观察目标对象有很多直接或间接的观察者,通知到所有的观察者会很耗时间。
- 如果在观察者和观察对象之间存在循环依赖就会触发循环调用。
观察者模式的应用场景
- 一个抽象模型有两个方面,一方面依赖于另一方面,将这两方面封装在独立的对象中使他们可以各自独立的改变。
- 一个对象的改变将导致一个或多个对象也发生改变。
观察者模式的案例
// 抽象目标类
public interface Subject {
void registerObserver(Observer o);
void removeObserver(Observer o);
void notifyObservers();
}
// 具体目标类
public class WeatherData implements Subject {
private float temperatrue;
private float pressure;
private float humidity;
private ArrayList<Observer> observers;
public WeatherData() {
observers = new ArrayList<Observer>();
}
public float getTemperature() {
return temperatrue;
}
public float getPressure() {
return pressure;
}
public float getHumidity() {
return humidity;
}
public void dataChange() {
//调用 接入方的 update
notifyObservers();
}
public void setData(float temperature, float pressure, float humidity) {
this.temperatrue = temperature;
this.pressure = pressure;
this.humidity = humidity;
//调用dataChange, 将最新的信息 推送给 接入方 currentConditions
dataChange();
}
@Override
public void registerObserver(Observer o) {
observers.add(o);
}
@Override
public void removeObserver(Observer o) {
if (observers.contains(o)) {
observers.remove(o);
}
}
@Override
public void notifyObservers() {
for (Observer observer : observers) {
observer.update(this.temperatrue, this.pressure, this.humidity);
}
}
}
// 抽象观察者
public interface Observer {
void update(float temperature, float pressure, float humidity);
}
// 具体观察者
public class BaiduSite implements Observer {
private float temperature;
private float pressure;
private float humidity;
@Override
public void update(float temperature, float pressure, float humidity) {
this.temperature = temperature;
this.pressure = pressure;
this.humidity = humidity;
display();
}
public void display() {
System.out.println("===百度网站====");
System.out.println("***百度网站 气温 : " + temperature + "***");
System.out.println("***百度网站 气压: " + pressure + "***");
System.out.println("***百度网站 湿度: " + humidity + "***");
}
}
public class CurrentConditions implements Observer {
private float temperature;
private float pressure;
private float humidity;
@Override
public void update(float temperature, float pressure, float humidity) {
this.temperature = temperature;
this.pressure = pressure;
this.humidity = humidity;
display();
}
public void display() {
System.out.println("***Today mTemperature: " + temperature + "***");
System.out.println("***Today mPressure: " + pressure + "***");
System.out.println("***Today mHumidity: " + humidity + "***");
}
}
public static void main(String[] args) {
//创建一个WeatherData
WeatherData weatherData = new WeatherData();
//创建观察者
CurrentConditions currentConditions = new CurrentConditions();
BaiduSite baiduSite = new BaiduSite();
//注册到weatherData
weatherData.registerObserver(currentConditions);
weatherData.registerObserver(baiduSite);
//测试
System.out.println("通知各个注册的观察者, 看看信息");
weatherData.setData(10f, 100f, 30.3f);
weatherData.removeObserver(currentConditions);
//测试
System.out.println();
System.out.println("通知各个注册的观察者, 看看信息");
weatherData.setData(10f, 100f, 30.3f);
}
观察者模式在源码中的应用
Observer
// 抽象观察者
public interface Observer {
/**
* This method is called whenever the observed object is changed. An
* application calls an <tt>Observable</tt> object's
* <code>notifyObservers</code> method to have all the object's
* observers notified of the change.
*
* @param o the observable object.
* @param arg an argument passed to the <code>notifyObservers</code>
* method.
*/
void update(Observable o, Object arg);
}
// 具体目标类
public class Observable {
private boolean changed = false;
// 维护了观察者集合
private Vector<Observer> obs;
/** Construct an Observable with zero Observers. */
public Observable() {
obs = new Vector<>();
}
public synchronized void addObserver(Observer o) {
if (o == null)
throw new NullPointerException();
if (!obs.contains(o)) {
obs.addElement(o);
}
}
/**
* Deletes an observer from the set of observers of this object.
* Passing <CODE>null</CODE> to this method will have no effect.
* @param o the observer to be deleted.
*/
public synchronized void deleteObserver(Observer o) {
obs.removeElement(o);
}
public void notifyObservers(Object arg) {
/*
* a temporary array buffer, used as a snapshot of the state of
* current Observers.
*/
Object[] arrLocal;
synchronized (this) {
/* We don't want the Observer doing callbacks into
* arbitrary code while holding its own Monitor.
* The code where we extract each Observable from
* the Vector and store the state of the Observer
* needs synchronization, but notifying observers
* does not (should not). The worst result of any
* potential race-condition here is that:
* 1) a newly-added Observer will miss a
* notification in progress
* 2) a recently unregistered Observer will be
* wrongly notified when it doesn't care
*/
if (!changed)
return;
arrLocal = obs.toArray();
clearChanged();
}
for (int i = arrLocal.length-1; i>=0; i--)
((Observer)arrLocal[i]).update(this, arg);
}
......
}