jdk的观察模式学习
Observer模式定义对象间的一对多的依赖关系,当一个对象(被观察者)的状态发生改变时, 所有依赖于它的对象(观察者)都得到通知并被自动更新。JDK里提供的observer设计模式的实现由java.util.Observable类和 java.util.Observer接口组成。从名字上可以清楚的看出两者在Observer 设计模式中分别扮演的角色:Observer是观察者角色,Observable是被观察目标(subject)角色。
?
import java.util.Observer;import java.util.Vector;/** * Observable 被观察对象 * <p> * 一个 observable 对象可以有一个或多个观察者observer。 * <p> * 观察者是实现Observer接口的任意对象。 * <p> * 一个 observable 实例改变后,调用 Observable 的 notifyObservers 方法的应用程序会通过调用观察者的 update 方法来通知观察者该实例发生了改变。 * <P> */public class Observable { /** 被观察对象是否改变 */ private boolean changed = false; /** * 一个 observable 对象可以有一个或多个观察者observer * <p> * 该集合是用来存放该被观察对象的观察对象 */ private final Vector obs; public Observable(){ obs = new Vector(); } /** * Adds an observer to the set of observers for this object, provided that it is not the same as some observer * already in the set. The order in which notifications will be delivered to multiple observers is not specified. * See the class comment. * <p> * 如果观察者与集合中已有的观察者不同, * <p> * 则向对象的观察者集中添加此观察者。 * <p> * 未指定向多个观察者发送通知的顺序。 * * @param o an observer to be added. * @throws NullPointerException if the parameter o is null. */ 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. * <p> * 从对象的观察者集合中删除某个观察者。 * <p> * 向此方法传递 null无效 * * @param o the observer to be deleted. */ public synchronized void deleteObserver(Observer o) { obs.removeElement(o); } /** * If this object has changed, as indicated by the <code>hasChanged</code> method, then notify all of its observers * and then call the <code>clearChanged</code> method to indicate that this object has no longer changed. * <p> * Each observer has its <code>update</code> method called with two arguments: this observable object and * <code>null</code>. In other words, this method is equivalent to: <blockquote><tt> * notifyObservers(null)</tt></blockquote> * <p> * 如果 hasChanged 方法指示对象已改变,则通知其所有观察者,并调用 clearChanged 方法来指示此对象不再改变。 * <p> * 每个观察者都有其 update 方法,其调用参数有两个:observable 对象和 null。换句话说,此方法等效于: notifyObservers(null) * * @see java.util.Observable#clearChanged() * @see java.util.Observable#hasChanged() * @see java.util.Observer#update(java.util.Observable, java.lang.Object) */ public void notifyObservers() { notifyObservers(null); } /** * If this object has changed, as indicated by the <code>hasChanged</code> method, then notify all of its observers * and then call the <code>clearChanged</code> method to indicate that this object has no longer changed. * <p> * Each observer has its <code>update</code> method called with two arguments: this observable object and the * <code>arg</code> argument. * <p> * 如果 hasChanged 方法指示对象已改变,则通知其所有观察者,并调用 clearChanged 方法来指示此对象不再改变。 * <p> * 每个观察者都有其 update 方法,其调用参数有两个:observable 对象和 arg 参数。 arg 可以是任意对象 * * @param arg any object. * @see java.util.Observable#clearChanged() * @see java.util.Observable#hasChanged() * @see java.util.Observer#update(java.util.Observable, java.lang.Object) */ public void notifyObservers(Object arg) { /* * 是当前观察者的快照 */ Object[] arrLocal; synchronized (this) { /* * 使用synchronized (this),可以避免在多线程的状况下新加的观察者和未被注册的观测者被通知 */ if (!changed) return; arrLocal = obs.toArray(); clearChanged(); } for (int i = arrLocal.length - 1; i >= 0; i--) ((Observer) arrLocal[i]).update(this, arg); } /** * Clears the observer list so that this object no longer has any observers. */ public synchronized void deleteObservers() { obs.removeAllElements(); } /** * Marks this <tt>Observable</tt> object as having been changed; the <tt>hasChanged</tt> method will now return * <p> * 标记此 Observable 对象为已改变的对象;现在 hasChanged 方法将返回 true。 * <p> * <tt>true</tt>. */ protected synchronized void setChanged() { changed = true; } /** * Indicates that this object has no longer changed, or that it has already notified all of its observers of its * most recent change, so that the <tt>hasChanged</tt> method will now return <tt>false</tt>. This method is called * automatically by the <code>notifyObservers</code> methods. * <p> * 指示对象不再改变,或者它已对其所有的观察者通知了最近的改变,所以 hasChanged 方法将返回 false。 * <p> * notifyObservers 方法自动调用此方法。 * <p> * * @see java.util.Observable#notifyObservers() * @see java.util.Observable#notifyObservers(java.lang.Object) */ protected synchronized void clearChanged() { changed = false; } /** * Tests if this object has changed. * * @return <code>true</code> if and only if the <code>setChanged</code> method has been called more recently than * the <code>clearChanged</code> method on this object; <code>false</code> otherwise. * <p> * 测试对象是否改变。当且仅当在此对象上最近调用了 setChanged 方法时才返回 true;否则返回 false。 * @see java.util.Observable#clearChanged() * @see java.util.Observable#setChanged() */ public synchronized boolean hasChanged() { return changed; } /** * Returns the number of observers of this <tt>Observable</tt> object. * <p> * 返回 Observable 对象的观察者数目 * * @return the number of observers of this object. */ public synchronized int countObservers() { return obs.size(); }}?
?
Oberver接口
这是个接口类,这个接口只有一个未实现的抽象方法update。实现该接口的对象成为观察者,该对象要实现update方法。注册了该对象(观察者)的对象(观察者)实例调用notifiyObservers方法后,观察者会自动执行update方法。
public interface Observer { /** * This method is called whenever the observed object is changed. An * application calls an <tt>Observable</tt> object's *只要改变了 observable 对象就调用此方法。 * <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);}??
?
?
?
?
?
?