在java中提供了观察者模式的被观察者类和观察者的接口,分别是class observable和interface observe
下面看下java中observable的源代码
<span style="font-size:14px;">/** * Observable is used to notify a group of Observer objects when a change * occurs. On creation, the set of observers is empty. After a change occurred, * the application can call the {@link #notifyObservers()} method. This will * cause the invocation of the {@code update()} method of all registered * Observers. The order of invocation is not specified. This implementation will * call the Observers in the order they registered. Subclasses are completely * free in what order they call the update methods. * * @see Observer */ public class Observable { List<Observer> observers = new ArrayList<Observer>(); boolean changed = false; /** * Constructs a new {@code Observable} object. */ public Observable() { } /** * Adds the specified observer to the list of observers. If it is already * registered, it is not added a second time. * * @param observer * the Observer to add. */ public void addObserver(Observer observer) { if (observer == null) { throw new NullPointerException(); } synchronized (this) { if (!observers.contains(observer)) observers.add(observer); } } /** * Clears the changed flag for this {@code Observable}. After calling * {@code clearChanged()}, {@code hasChanged()} will return {@code false}. */ protected void clearChanged() { changed = false; } /** * Returns the number of observers registered to this {@code Observable}. * * @return the number of observers. */ public int countObservers() { return observers.size(); } /** * Removes the specified observer from the list of observers. Passing null * won't do anything. * * @param observer * the observer to remove. */ public synchronized void deleteObserver(Observer observer) { observers.remove(observer); } /** * Removes all observers from the list of observers. */ public synchronized void deleteObservers() { observers.clear(); } /** * Returns the changed flag for this {@code Observable}. * * @return {@code true} when the changed flag for this {@code Observable} is * set, {@code false} otherwise. */ public boolean hasChanged() { return changed; } /** * If {@code hasChanged()} returns {@code true}, calls the {@code update()} * method for every observer in the list of observers using null as the * argument. Afterwards, calls {@code clearChanged()}. * <p> * Equivalent to calling {@code notifyObservers(null)}. */ public void notifyObservers() { notifyObservers(null); } /** * If {@code hasChanged()} returns {@code true}, calls the {@code update()} * method for every Observer in the list of observers using the specified * argument. Afterwards calls {@code clearChanged()}. * * @param data * the argument passed to {@code update()}. */ @SuppressWarnings("unchecked") public void notifyObservers(Object data) { int size = 0; Observer[] arrays = null; synchronized (this) { if (hasChanged()) { clearChanged(); size = observers.size(); arrays = new Observer[size]; observers.toArray(arrays); } } if (arrays != null) { for (Observer observer : arrays) { observer.update(this, data); } } } /** * Sets the changed flag for this {@code Observable}. After calling * {@code setChanged()}, {@code hasChanged()} will return {@code true}. */ protected void setChanged() { changed = true; } }</span>
1. 先调用setChanged(true)方法,标记状态已经改变的事实
2. 然后调用两种notifyObservers()方法中的一个:notifyObserves()和notifyObservers(data)
看notifyObservers函数的代码就知道如果没有setChanged(true)的话,消息将不会push给观察者。原来我在看代码的时候在想,既然已经调用了notifyObservers方法的话,就说明想要通知观察者了,为什么还要多setChanged这一步,不是多此一举么?
但是其实这样做是有必要的,下面举个例子
比如说当我们在android中使用传感器的时候,想要将传感器传回来的数据发送给观察者的时候,你会发现传感器的数据每秒都会更新很多遍(可能数据只有微小的变化,旺往往我们并不care这些小变化),不断的去调用notifyObservers,不断的通知观察者,我们并不希望看到这样的事情发生,如果我们希望当差距大于某个阀值的时候才更新,就可以调用setChanged,进行有效的更新。
也许你不会经常用到该功能,但是把这样的功能准备好,当需要时就可以马上使用,这样不是挺好的么。你也可以通过clearChanged方法来将changed状态设置为false。
存在即是合理的,所以以后看源码的时候,自身觉得很多不合理的地方一定要多加揣摩写源代码人的用意,这样会学到很多。