设计模式之观察者observer模式
来源:互联网 发布:道路里程数据库 编辑:程序博客网 时间:2024/06/07 05:55
认识观察者模式:
报社和杂志的订阅的者的关系。
出版者+订阅者= 观察者模式
也就是:主题+观察者=观察者模式
1 主题管理着某些数据
2 观察者已经订阅主题以便在主题数据改变的时候收到更新
3 当主题内的数据改变的时候就会通知观察者 也是说当数据改变的时候,新的数据就会以某种形式送到观察者的手上
观察者模式的概念:
定义了对象之间的一对多关系,当一个对象改变状态的时候,它的所有依赖者都会受到通知并得到更新。
一对多的关联:
一是指具有状态的主题,多指的是观察者。
依赖关联是如何产生的?
主题是真正具有数据的人,观察者是主题的依赖者,在数据变化更新的时候更新。
当两个对象之间松耦合,它们依然可以交互,但是不太清楚彼此之间的细节。
观察者提供了一个对象设计,让主题和观察者之间共耦合。
关于观察者的一切,主题只是知道观察者实现了某个接口,主题不需要知道观察者的具体类是谁,做了什么或者是其他的细节。
任何时候我们都可以添加新的观察者。
同时当有新类型的观察者出现的时候,主题代码不需要改变。
设计原则:
为了交互对象之间的松耦合设计而努力。
松耦合的设计之所以能让我们建立哟弹性的OO系统,能够应对变化,是因为对象之间的相互依赖程度都降到了最低。
Java内置的观察者模式的如何运作:
观察者如何发出通知:
1 先调用setChanged()方法,标记已经改变的事实。
2 然后调用notifyObservers或者notifObservers(Object arg)
主题的源代码:
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.Observable的缺点:
Observable是一个类而不是一个接口,违反了我们的OO设计原则:针对接口编程,而不是针对实现编程。
Observable将关键的地方保护起来了,setChanged()被定义成protected,违背了第二个设计原则:多用组合,少用继承
jdk的另一个例子:按钮的监听也是采用了观察者模式。还有JavaBean,RMI。
OO基础:抽象,继承,封装,多态
OO原则:
封装变化
多用组合,少用继承
针对接口编程,不针对实现编程
为交互对象之间的松耦合设计而努力
问题1 设计原则:找出程序中会变化的方面,然后将其和固定不变的部分分离。
在观察者模式中,会改变的是主题的状态,以及观察者的数据和类型。
问题2 设计原则:针对接口编程,不针对实现编程
主题和观察者都使用接口。观察则使用主题的接口向主题注册,而主题利用观察则会的接口通知观察者。
设计问题3 设计原则:多用组合,少用继承。
观察者模式利用组合将许多观察者组合进主题中。
- 设计模式之Observer(观察者)
- 设计模式之Observer 观察者
- 设计模式之观察者模式(Observer模式)
- Java设计模式之Observer(观察者)模式
- Java设计模式之Observer 观察者模式
- 设计模式之Observer观察者模式
- 设计模式之observer(观察者模式)
- 设计模式读书笔记之观察者模式(Observer)
- 设计模式之Observer(观察者模式)
- 设计模式之Observer(观察者模式)
- Java设计模式之Observer-观察者模式
- 设计模式之(十四)观察者模式Observer
- 设计模式之观察者模式(Observer Pattern)
- 【设计模式】之 Observer 观察者模式
- 设计模式之七 --- 观察者模式(Observer)
- 【设计模式】之观察者模式(Observer)
- 设计模式之--观察者模式(Observer)
- 设计模式之七 --- 观察者模式(Observer)
- 遍历Json
- hql的使用之小细节
- 关于android源码4.3 CTS测试的问题
- Android 4.2 Input Event事件处理流程<一>---应用注册
- B - Friends(8.4.1)
- 设计模式之观察者observer模式
- 关于tcpjava网络编程服务器端收不到信息
- 实模式与保护模式解惑之(一)——二者的起源与区别
- js ==与===区别(两个等号与三个等号)
- Tree Recovery(过遍历确定二叉树结构)
- 使用异步 I/O 大大提高应用程序的性能
- 华为编程大赛——高精度加减法
- 同是流行MVC框架,比较Strtus2和SpringMVC的区别
- windows 64位安装scikit learn2.7