观察者模式

目录

1、基本概念

2、Spring 事件驱动模型中的三种角色

(1)、事件角色

(2)、 事件监听者角色

(3)、事件发布者角色

3、Spring 的事件流程总结

案例

输出


1、基本概念

观察者模式是一种对象行为型设计模式,表示的是一种对象与对象之间具有的依赖关系。当一个对象发生改变的时候,这个对象所依赖的对象也会做出反应。

Spring 事件驱动模型就是观察者模式中很经典的一个应用。Spring 事件驱动模型非常有用,在很多场景都可以起到解耦系统代码的作用。

2、Spring 事件驱动模型中的三种角色

(1)、事件角色

ApplicationEvent (org.springframework.context 包下) 充当事件的角色,

Spring 中默认存在以下事件,他们都是对 ApplicationContextEvent 的实现(继承自ApplicationContextEvent):

  • ContextStartedEventApplicationContext 启动后触发的事件;
  • ContextStoppedEventApplicationContext 停止后触发的事件;
  • ContextRefreshedEventApplicationContext 初始化或刷新完成后触发的事件;
  • ContextClosedEventApplicationContext 关闭后触发的事件。

观察者模式_第1张图片

(2)、 事件监听者角色

ApplicationListener 充当了事件监听者的角色,它是一个接口,里面只定义了一个 onApplicationEvent() 方法来处理 ApplicationEventApplicationListener 接口类的源码如下,所以在 Spring 中只要实现实现 onApplicationEvent() 方法即可完成监听事件:

package org.springframework.context;
import java.util.EventListener;
@FunctionalInterface
public interface ApplicationListener extends EventListener {
    void onApplicationEvent(E var1);
}

(3)、事件发布者角色

ApplicationEventPublisher 充当了事件的发布者,它也是一个接口。

@FunctionalInterface
public interface ApplicationEventPublisher {
    default void publishEvent(ApplicationEvent event) {
        this.publishEvent((Object)event);
    }

    void publishEvent(Object var1);
}

ApplicationEventPublisher 接口的 publishEvent() 这个方法在 AbstractApplicationContext 类中被实现,阅读这个方法的实现,可以发现实际上事件是通过 ApplicationEventMulticaster 来广播出去的。

3、Spring 的事件流程总结

  1. 定义一个事件:实现一个继承自 ApplicationEvent 的类,并且写相应的构造函数;
  2. 定义一个事件监听者:实现 ApplicationListener 接口,重写 onApplicationEvent() 方法;
  3. 使用事件发布者发布消息: 可以通过 ApplicationEventPublisherpublishEvent() 方法发布消息。

案例

// 定义事件,继承自 ApplicationEvent 并且重写相应的构造函数
public class DemoEvent extends ApplicationEvent{
    private static final long serialVersionUID = 1L;

    private String message;

    public DemoEvent(Object source,String message){
        super(source);
        this.message = message;
    }

    public String getMessage() {
         return message;
    }

// 定义事件监听者,实现 ApplicationListener 接口,重写 onApplicationEvent() 方法
@Component
public class DemoListener implements ApplicationListener{

    //使用 onApplicationEvent 接收消息
    @Override
    public void onApplicationEvent(DemoEvent event) {
        String msg = event.getMessage();
        System.out.println("接收到的信息是:"+msg);
    }

}
// 发布事件,通过 ApplicationEventPublisher  的 publishEvent() 方法发布消息
@Component
public class DemoPublisher {

    @Autowired
    ApplicationContext applicationContext;

    public void publish(String message){
        //发布事件
        applicationContext.publishEvent(new DemoEvent(this, message));
    }
}

输出

当调用 DemoPublisher 的 publish() 方法,比如 demoPublisher.publish("hello"),控制台就会打印出:接收到的信息是:hello 。

你可能感兴趣的:(观察者模式)