本文参考并摘抄了https://www.cnblogs.com/java-my-life/archive/2012/05/16/2502279.html部分内容
1.概念
观察者模式是对象的行为模式,又叫发布-订阅(Publish/Subscribe)模式、模型-视图(Model/View)模式、源-监听器(Source/Listener)模式或从属者(Dependents)模式。观察者模式定义了一种一对多的依赖关系,让多个观察者对象同时监听某一个主题对象。这个主题对象在状态上发生变化时,会通知所有观察者对象,使它们能够自动更新自己
以上摘抄自上述博客
在实际使用过程中,叫观察者模式说法最多,发布/订阅者模式次之,别的说法很少听到,作为了解内容即可。
2.使用场景
脱离了使用场景谈设计模式都是耍流氓
例如:我们每天都要观察时间,到点下班对不对?(到点下班是不可能的,这辈子不可能的,手动悲伤)
在这种场景下,我们每个员工都是观察者,而观察的对象只有一个:时间。这就是典型的观察者模式应用场景。
3.类结构图
下图是你在网上经常能查看到的观察者模式的类结构图
在理解观察者模式之前,表示被这个名字坑了很久很久很久...
因为听到观察者模式的时候,我感觉这个模式里应该有这么几个对象:
- 被观察者
- 观察者
或者如果按发布/订阅者模式来分: - 发布者
- 订阅者
在上图中,观察者好理解,Subject是什么角色?
一种模式有两种命名方式,那么这两种命名方式之间的对应关系是: - 被观察者---对应发布者---对应Subject
- 观察者---对应订阅者
4.实现方式
- 定义观察者,观察者是一个接口,里面只有一个方法
/**
* 观察者接口,也就是订阅者接口
*/
public interface MyObserver {
//更新方法
void updata();
}
- 定义被观察者,被观察者是一个普通的java类
/**
* 被观察者类,也就是Subject类,Publisher类
*/
public class MyObserverable {
//观察者对象列表
private ArrayList<MyObserver> observers = new ArrayList<>();
//添加观察者
public void attach(MyObserver myObserver) {
observers.add(myObserver);
}
// 去除观察者
public void detach(MyObserver myObserver) {
observers.remove(myObserver);
}
//通知观察者
public void notifyObservers() {
for (MyObserver o : observers) {
o.updata();
}
}
}
观察者模式使用非常广泛,不可能每次使用都需要我们自己去定义相应的类和接口。所以java为我们内置了观察者模式
5.java为我们内置了观察者模式
- 发布者,也就是被观察者,Observable,是一个类,位于util包下
- 订阅者,也就是观察者,Observer,是一个接口,同样位于util包下
Observable中的代码java已经为我们写好,使用的时候直接继承即可,
源码如下:
public class Observable {
private boolean changed = false;
private Vector<Observer> 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
*/
// Android-changed: Call out to hasChanged() to figure out if something changes.
// Upstream code avoids calling the nonfinal hasChanged() from the synchronized block,
// but that would break compatibility for apps that override that method.
// if (!changed)
if (!hasChanged())
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();
}
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);
}
6.怎么使用
以本篇“使用场景”中的例子来说明:
/**
* 工作时间类
* 也就被观察者类/发布者类
* 继承自java的Observable类
*/
public class WorkTimer extends Observable {
//这个类里有关观察者模式需要的方法都被封装在了Observable中
//如果不需要处理别的逻辑,那么这个类只是一个空类
}
/**
* 普通员工类
* 也就是观察者类/订阅者类
* 实现Observer接口
*/
public class Employee implements Observer {
private static final String TAG = "Employee";
@Override
public void update(Observable o, Object arg) {
//收到更新提示后,即可在这个方法中处理自己的逻辑
//参数1:被观察者类
//参数2:任意参数,一般跟业务相关的数据
//java在这个参数中返回了被观察者,是为了预防我们有别的需要
Log.d(TAG, "update: 我是普通员工,到点就要下班");
//例如,我只希望收到一次通知
o.deleteObserver(this);
}
}
/**
* 经理类,属于观察者
*/
public class Manager implements Observer {
private static final String TAG = "Manager";
@Override
public void update(Observable o, Object arg) {
Log.d(TAG, "update: 我是经理,到点后还得开会");
}
}
使用代码示例:
//初始化被观察者类
WorkTimer timer = new WorkTimer();
//初始化普通员工类
Employee employeeFrist = new Employee();
EmployeeSecond employeeSecond = new EmployeeSecond();
//初始化经理类
Manager manager = new Manager();
//添加到观察者列表
timer.addObserver(employeeFrist);
timer.addObserver(employeeSecond);
timer.addObserver(manager);
//被观察者更新
timer.notifyObservers("2018年11月");
//后来,Manager离职了,将其从观察者列表中去掉
timer.deleteObserver(manager);
//更新的时候,经理类不会再收到提示
timer.notifyObservers("2018年12月");
写在最后
在观察者模式中,概念有些模糊,而网上的人很少(自少我没有看到)把这几个概念严格对应起来的人,下面专门说明一下,相信你会有些收获。
被观察者(Observable),也叫发布者(publisher),也叫主题(Subject)
观察者(Observer),也就是订阅者(Subscribe)