java观察者模式
来源:互联网 发布:淘宝盈奥肌肉科技真假 编辑:程序博客网 时间:2024/05/21 17:03
今天在查看项目中Map模块,学到了java观察者模式,再次记录下,以便以后查阅。
JDK1.2后,Java提供了对观察者模式的支持接口和实现类。
其中接口 java.util.Observer 用来指定观察者,观察者必须实现 void update(Observable o, Object arg) 方法。
而 java.util.Observable 用来指定观察物(被观察者、可被观察的),并且提供了一系列的方法。读者可以很轻易的使用这个接口和实现类来实现观察者模式。
java.util.Observer 只有一个简单的方法 void update(Observable o, Object arg)
其中,参数 Observable o 用于指定触发 update 方法的对象, Object arg 用于指定触发 update 方法时候的附加参数。
如果有桌面应用开发的读者应该很了解,这跟事件处理机制是完全一样的,其中 Observable o 可被看作事件源。 Object arg 可被看作消息。
具体Map是这么用的,Map需要做一个放大缩小的功能。ZoomState作为被观察者,MapZoomView作为观察者,当ZoomState发声变化,MapZoomView就根据ZoomState的参数发生变化。
这边做一个小例子:
package com.jeff;import java.util.Observable;public class Publisher extends Observable {private String magazineName;public String getMagazineName() {return magazineName;}public void publish(String magazineName) {this.magazineName = magazineName;setChanged();notifyObservers(this);}}
package com.jeff;import java.util.Observable;import java.util.Observer;public class Reader implements Observer {@Overridepublic void update(Observable o, Object arg1) {Publisher p = (Publisher) o;System.out.println("我要订阅" + p.getMagazineName());}}
这边附上java.util.Observable的源码:
/** * @(#)Observable.java1.39 05/11/17 * * Copyright 2006 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */package java.util;/*** * This class represents an observable object, or "data" * in the model-view paradigm. It can be subclassed to represent an * object that the application wants to have observed. * <p> * An observable object can have one or more observers. An observer * may be any object that implements interface <tt>Observer</tt>. After an * observable instance changes, an application calling the * <code>Observable</code>'s <code>notifyObservers</code> method * causes all of its observers to be notified of the change by a call * to their <code>update</code> method. * <p> * The order in which notifications will be delivered is unspecified. * The default implementation provided in the Observable class will * notify Observers in the order in which they registered interest, but * subclasses may change this order, use no guaranteed order, deliver * notifications on separate threads, or may guarantee that their * subclass follows this order, as they choose. * <p> * Note that this notification mechanism is has nothing to do with threads * and is completely separate from the <tt>wait</tt> and <tt>notify</tt> * mechanism of class <tt>Object</tt>. * <p> * When an observable object is newly created, its set of observers is * empty. Two observers are considered the same if and only if the * <tt>equals</tt> method returns true for them. * * @author Chris Warth * @version 1.39, 11/17/05 * @see java.util.Observable#notifyObservers() * @see java.util.Observable#notifyObservers(java.lang.Object) * @see java.util.Observer * @see java.util.Observer#update(java.util.Observable, java.lang.Object) * @since JDK1.0 */public class Observable { private boolean changed = false; private Vector obs; /*** Construct an Observable with zero Observers. */ 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. * * @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. * @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> * * @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. * * @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) {/** * 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); } /*** * 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 <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. * * @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. * @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. * * @return the number of observers of this object. */ public synchronized int countObservers() {return obs.size(); }}
java.util.Observer的源码:
/** * @(#)Observer.java1.20 05/11/17 * * Copyright 2006 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */package java.util;/*** * A class can implement the <code>Observer</code> interface when it * wants to be informed of changes in observable objects. * * @author Chris Warth * @version 1.20, 11/17/05 * @see java.util.Observable * @since JDK1.0 */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);}
- java观察者模式
- java观察者模式
- Java 观察者模式
- Java实现观察者模式
- Java实现观察者模式
- Java 中的观察者模式
- Java观察者模式
- java Observer 观察者模式
- 观察者模式(Java)
- java 接口 观察者模式
- java观察者模式
- Java之观察者模式
- Java 观察者模式
- java 观察者模式
- java中的观察者模式
- java 观察者模式
- java观察者模式介绍
- java观察者模式
- Codeforces Beta Round #7, problem: (D) Palindrome Degree
- 在路上
- 品悟性能优化学习笔记一
- WinSock Tips
- Object-c---拷贝对象笔记
- java观察者模式
- jquery弹出框插件--jqModal
- index range scan,index fast full scan,index skip scan发生的条件
- 设计模式C++实现(4)——单例模式
- C++学习(5)——利用对象、引用、指针调用虚函数
- S5PV210 Linux 模拟SPI方式控制ADS7846触摸屏驱动
- DEDECMS 模块管理列表没有任何 模块
- 一个合格的程序员应该读过哪些书
- table 显示 滚动条