1、概述
观察者模式是软件设计模式的一种。在此种模式中,一个目标对象管理所有相依于它的观察者对象,并且在它本身的状态改变时主动发出通知。这通常通过呼叫各观察者所提供的方法来实现。此种模式通常被用来实时事件处理系统。
2、定义
又被称为发布-订阅(Publish/Subscribe)模式,它定义了一种一对多的依赖关系,让多个观察者对象同时监听某一个主题对象。这个主题对象在状态变化时,会通知所有的观察者对象,使他们能够自动更新自己。
3、结构
在观察者模式中有如下角色:
- Subject:抽象主题(抽象被观察者),抽象主题角色把所有观察者对象保存在一个集合里,每个主题都可以有任意数量的观察者,抽象主题提供一个接口,可以增加和删除观察者对象。
- ConcreteSubject:具体主题(具体被观察者),该角色将有关状态存入具体观察者对象,在具体主题的内部状态发生改变时,给所有注册过的观察者发送通知。
- Observer:抽象观察者,是观察者的抽象类,它定义了一个更新接口,使得在得到主题更改通知时更新自己。
- ConcrereObserver:具体观察者,实现抽象观察者定义的更新接口,以便在得到主题更改通知时更新自身的状态。
4、实现
4.1 抽象主题
public interface Subject {
/**
* 添加观察者
* @param observer
*/
void addObserver(Observer observer);
/**
* 移除观察者
* @param observer
*/
void removeObserver(Observer observer);
/**
* 通知观察者
*/
void notifyObserver();
}
4.2 具体主题
package com.zengqingfa.designpattern.observer.standard;
import java.util.ArrayList;
import java.util.List;
/**
*
* @fileName: ConcreteSubject
* @author: zengqf3
* @date: 2021-4-23 15:06
* @description: 具体主题(被观察者)
*/
public class ConcreteSubject implements Subject {
/**
* 观察者列表
*/
private List<Observer> observerList;
public ConcreteSubject() {
observerList = new ArrayList<>();
}
@Override
public void addObserver(Observer observer) {
observerList.add(observer);
}
@Override
public void removeObserver(Observer observer) {
observerList.remove(observer);
}
@Override
public void notifyObserver() {
for (Observer observer : observerList) {
observer.update("standard标准观察者");
}
}
/**
* 触发更新
*/
public void triggle() {
System.out.println("被观察者事件发生改变");
this.notifyObserver();
}
}
4.3 抽象观察者
public interface Observer {
/**
* 更新
*/
void update(String notice);
}
4.4 具体观察者
public class ConcreteObserver1 implements Observer {
@Override
public void update(String notice) {
System.out.println("观察者ConcreteObserver1收到消息:"+notice);
}
}
public class ConcreteObserver2 implements Observer {
@Override
public void update(String notice) {
System.out.println("观察者ConcreteObserver2收到消息:"+notice);
}
}
4.5 测试
public class Client {
public static void main(String[] args) {
/**
* 被观察者事件发生改变
* 观察者ConcreteObserver1收到消息:standard标准观察者
* 观察者ConcreteObserver2收到消息:standard标准观察者
*/
ConcreteSubject subject = new ConcreteSubject();
subject.addObserver(new ConcreteObserver1());
subject.addObserver(new ConcreteObserver2());
subject.triggle();
}
}
4.6 uml结构
5、优缺点
5.1 优点
1)降低了目标与观察者之间的耦合关系,两者之间是抽象耦合关系
2)被观察者发送通知,所有注册的观察者都会收到信息【可以实现广播机制】
5.2 缺点
1)如果观察者非常多的话,那么所有的观察者收到被观察者发送的通知会耗时
2)如果被观察者有循环依赖的话,那么被观察者发送通知会使观察者循环调用,会导致系统崩溃
6、适用场景
1)一个抽象模型有两个方面,其中一个方面依赖于另一个方面。将这些方面封装在独立的对象中使它们可以各自独立地改变和复用。
2)一个对象的改变将导致其他一个或多个对象也发生改变,而不知道具体有多少对象将发生改变,可以降低对象之间的耦合度。
3)一个对象必须通知其他对象,而并不知道这些对象是谁。
4)需要在系统中创建一个触发链,A对象的行为将影响B对象,B对象的行为将影响C对象……,可以使用观察者模式创建一种链式触发机制
7、源码中的设计模式
7.1 JDK 提供的观察者接口
在JDK的 java.util 包中,提供了 Observable 类以及 Observer 接口,它们构成了JDK对观察者模式的支持。
观察者Observer
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
* @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);
}
Observable 类则为目标类
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
*/
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();
}
}
具体主题
public class ConcreteSubject extends Observable {
/**
* 触发更新
*/
public void triggle(String notice) {
System.out.println("被观察者事件发生改变");
setChanged();
notifyObservers(notice);
}
}
具体观察者
public class ConcreteObserver1 implements Observer {
@Override
public void update(Observable o, Object arg) {
System.out.println("观察者ConcreteObserver1收到消息:" + arg);
}
}
public class ConcreteObserver2 implements Observer {
@Override
public void update(Observable o, Object arg) {
System.out.println("观察者ConcreteObserver2收到消息:" + arg);
}
}
测试
public class Client {
public static void main(String[] args) {
/**
* 被观察者事件发生改变
* 观察者ConcreteObserver2收到消息:jdk观察者模式
* 观察者ConcreteObserver1收到消息:jdk观察者模式
*/
ConcreteSubject subject = new ConcreteSubject();
subject.addObserver(new ConcreteObserver1());
subject.addObserver(new ConcreteObserver2());
subject.triggle("jdk观察者模式");
}
}
uml图结构
7.2 Guava EventBus 中的观察者模式
Guava 中的 EventBus 封装了友好的 “生产/消费模型”,通过非常简单的方式,实现了观察者模式中的监听注册,事件分发。
使用了 Guava EventBus 之后,如果需要订阅消息,不需要实现任何接口,只需在监听方法上加上 @Subscribe 注解即可,EventBus 提供了 register 和 unregister 方法用于注册与取消注册事件,当 EventBus 调用 post 方法时将把事件分发给注册的对象
引入依赖
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>29.0-jre</version>
</dependency>
抽象观察者
public interface Observer {
/**
* 更新
*/
void update(String notice);
}
具体观察者
public class ConcreteObserver1 implements Observer {
@Subscribe
@Override
public void update(String notice) {
System.out.println("观察者ConcreteObserver1收到消息:" + notice);
}
}
public class ConcreteObserver2 implements Observer {
@Override
@Subscribe
public void update(String notice) {
System.out.println("观察者ConcreteObserver2收到消息:" + notice);
}
}
抽象主题
public interface Subject {
/**
* 注册观察者
* @param observer
*/
void register(Observer observer);
/**
* 移除观察者
* @param observer
*/
void unregister(Observer observer);
}
具体主题
package com.zengqingfa.designpattern.observer.guava;
import com.google.common.eventbus.EventBus;
/**
*
* @fileName: ConcreteSubject
* @author: zengqf3
* @date: 2021-4-23 15:06
* @description: 具体主题(被观察者)
*/
public class ConcreteSubject implements Subject{
private EventBus eventBus;
public ConcreteSubject() {
eventBus = new EventBus();
}
/**
* 触发更新
*/
public void triggle() {
System.out.println("被观察者事件发生改变");
this.eventBus.post("guava 观察者模式");
}
@Override
public void register(Observer observer) {
this.eventBus.register(observer);
}
@Override
public void unregister(Observer observer) {
this.eventBus.unregister(observer);
}
}
测试
public class Client {
public static void main(String[] args) {
/**
* 被观察者事件发生改变
* 观察者ConcreteObserver1收到消息:guava 观察者模式
* 观察者ConcreteObserver2收到消息:guava 观察者模式
*/
ConcreteSubject subject=new ConcreteSubject();
subject.register(new ConcreteObserver1());
subject.register(new ConcreteObserver2());
subject.triggle();
}
}
uml结构
7.3 Spring ApplicationContext 事件机制中的观察者模式
spring的事件机制是从java的事件机制拓展而来,ApplicationContext 中事件处理是由 ApplicationEvent 类和 ApplicationListener 接口来提供的。如果一个Bean实现了 ApplicationListener 接口,并且已经发布到容器中去,每次 ApplicationContext 发布一个 ApplicationEvent 事件,这个Bean就会接到通知
- ApplicationContext:事件源,其中的 publishEvent()方法用于触发容器事件
- ApplicationEvent:事件本身,自定义事件需要继承该类,可以用来传递数据
- ApplicationListener:事件监听器接口,事件的业务逻辑封装在监听器里面
引入依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.0.RELEASE</version>
</dependency>
spring配置文件
@Configuration
@ComponentScan(basePackages = "com.zengqingfa.designpattern.observer.spring")
public class AppConfig {
}
事件
package com.zengqingfa.designpattern.observer.spring;
import org.springframework.context.ApplicationEvent;
import org.springframework.stereotype.Component;
/**
*
* @fileName: Notice
* @author: zengqf3
* @date: 2021-4-23 16:04
* @description:
* * ApplicationContext:事件源,其中的 publishEvent()方法用于触发容器事件
* * ApplicationEvent:事件本身,自定义事件需要继承该类,可以用来传递数据
* *
*/
public class Notice extends ApplicationEvent {
/**
* Create a new {@code ApplicationEvent}.
* @param source the object on which the event initially occurred or with
* which the event is associated (never {@code null})
*/
public Notice(Object source) {
super(source);
}
}
观察者
package com.zengqingfa.designpattern.observer.spring;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
/**
*
* @fileName: ConcreteObserver1
* @author: zengqf3
* @date: 2021-4-23 15:10
* @description:
* ApplicationListener:事件监听器接口,事件的业务逻辑封装在监听器里面
*/
@Component
public class ConcreteObserver1 implements ApplicationListener<Notice> {
@Override
public void onApplicationEvent(Notice event) {
System.out.println("观察者ConcreteObserver1收到消息:" + event);
}
}
package com.zengqingfa.designpattern.observer.spring;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
/**
*
* @fileName: ConcreteObserver2
* @author: zengqf3
* @date: 2021-4-23 15:10
* @description:
* ApplicationListener:事件监听器接口,事件的业务逻辑封装在监听器里面
*/
@Component
public class ConcreteObserver2 implements ApplicationListener<Notice> {
@Override
public void onApplicationEvent(Notice event) {
System.out.println("观察者ConcreteObserver2收到消息:" + event);
}
}
主动触发事件
package com.zengqingfa.designpattern.observer.spring;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
*
* @fileName: Client
* @author: zengqf3
* @date: 2021-4-23 15:17
* @description:
*/
public class Client {
public static void main(String[] args) {
/**
* 观察者ConcreteObserver1收到消息:com.zengqingfa.designpattern.observer.spring.Notice[source=spring 观察者模式]
* 观察者ConcreteObserver2收到消息:com.zengqingfa.designpattern.observer.spring.Notice[source=spring 观察者模式]
*/
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
//ApplicationContext:事件源,其中的 publishEvent()方法用于触发容器事件
//主动触发
context.publishEvent(new Notice("spring 观察者模式"));
}
}
容器启动触发
package com.zengqingfa.designpattern.observer.spring;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
/**
*
* @fileName: ApplicationContextHolder
* @author: zengqf3
* @date: 2021-5-13 13:55
* @description:
*/
@Component
public class ApplicationContextHolder implements ApplicationContextAware {
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
//容器启动时进行触发
applicationContext.publishEvent(new Notice("spring 观察者模式"));
}
}
package com.zengqingfa.designpattern.observer.spring;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
*
* @fileName: Client
* @author: zengqf3
* @date: 2021-4-23 15:17
* @description:
*/
public class Client {
public static void main(String[] args) {
/**
* 观察者ConcreteObserver1收到消息:com.zengqingfa.designpattern.observer.spring.Notice[source=spring 观察者模式]
* 观察者ConcreteObserver2收到消息:com.zengqingfa.designpattern.observer.spring.Notice[source=spring 观察者模式]
*/
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
}
}