java事件驱动模式(观察者模式)

2019-12-17  本文已影响0人  slowwalkerlcr

Java设计模式——事件驱动模式(观察者模式)

角色

事件

事件类一般继承自java.util.EventObject类,封装了事件源以及跟事件有关的信息

source:事件源
getSource():获取事件源

public class EventObject implements java.io.Serializable {

    private static final long serialVersionUID = 5516075349620653480L;

    /**
     * The object on which the Event initially occurred
     */
    protected transient Object  source;

    /**
     * Constructs a prototypical Event.
     *
     * @param    source    The object on which the Event initially occurred.
     * @exception  IllegalArgumentException  if source is null.
     */
    public EventObject(Object source) {
        if (source == null)
            throw new IllegalArgumentException("null source");

        this.source = source;
    }

    /**
     * The object on which the Event initially occurred.
     *
     * @return   The object on which the Event initially occurred.
     */
    public Object getSource() {
        return source;
    }

    /**
     * Returns a String representation of this EventObject.
     *
     * @return  A a String representation of this EventObject.
     */
    public String toString() {
        return getClass().getName() + "[source=" + source + "]";
    }
}

事件源

每个事件包含一个事件源,事件源是事件发生的地方,由于事件源的某项属性或状态改变时,就会生成相应的事件对象,然后将事件对象通知给所有监听该事件源的监听器。

事件监听器

事件监听器一般需要实现java.util.EventListener接口

public interface EventListener {
}

EventListener 是个空接口,监听器必须要有回调方法供事件源回调,这个回调方法可以在继承或者实现EventListener 接口的时候自定义。

实现源码


/**
 * @description 事件监听器
 * @author: 
 * @version: 1.0
 * @date: 2019/12/17 17:02
 */
public interface ApplicationListener extends EventListener {
    /**
     * 监听器响应事件
     * @param event
     */
    void onApplicationEvent(ApplicationEvent event);
}

/**
 * @description 事件
 * @author: 
 * @version: 1.0
 * @date: 2019/12/17 17:03
 */
public class ApplicationEvent extends EventObject {
    /**
     * Constructs a prototypical Event.
     *
     * @param source The object on which the Event initially occurred.
     * @throws IllegalArgumentException if source is null.
     */
    public ApplicationEvent(Object source) {
        super(source);
    }
}
/**
 * @description 事件Bus总线
 * @author: 
 * @version: 1.0
 * @date: 2019/12/17 17:05
 */
public class ApplicationEventBus {

    /**
     * 存放所有的监听器
     */
    Set<ApplicationListener> listeners ;

    public ApplicationEventBus() {
        this.listeners = new HashSet<>();
    }

    /**
     *  添加监听器
     * @param listener
     */
    public void addApplicationListener(ApplicationListener listener){
        this.listeners.add(listener);
    }

    /**
     *  发布事件
     * @param event
     */
    public void publishEvent(ApplicationEvent event){
        if(CollectionUtils.isEmpty(listeners)){
            return;
        }
        for(ApplicationListener listener:listeners){
            listener.onApplicationEvent(event);
        }
    }
}

测试类

public static void main(String[] args) {

        ApplicationEventBus applicationEventBus= new ApplicationEventBus();

        /**
         * 添加监听事件源为整型的监听器l
         */
        applicationEventBus.addApplicationListener(event -> {
            Object source = event.getSource();
            if(source instanceof Integer){
                int row = ((Integer) source).intValue();
                System.out.println("检测到事件源为整型:事件源变为" + row);
            }
        });

        /**
         * 添加监听事件源为整型的监听器2
         */
        applicationEventBus.addApplicationListener(event -> {
            Object source = event.getSource();
            if(source instanceof String){
                String row = (String)source;
                System.out.println("检测到事件源为字符串类型:事件源变为" + row);
            }
        });
       /**
         * 添加监听事件源为整型的监听器3
         */
        ApplicationListener listener = new ApplicationListener() {
            @Override
            public void onApplicationEvent(ApplicationEvent event) {
                Object source = event.getSource();
                if(source instanceof String){
                    String row = (String)source;
                    System.out.println("检测到事件源为字符串类型:事件源变为" + row);
                }
            }
        };
        applicationEventBus.addApplicationListener(listener);


        applicationEventBus.publishEvent(new ApplicationEvent("envet sources"));

    }
上一篇 下一篇

猜你喜欢

热点阅读