zl程序教程

您现在的位置是:首页 >  其他

当前栏目

【EventBus】事件通信框架 ( 实现几个关键的封装类 | 消息中心 | 订阅注解 | 订阅方法封装 | 订阅对象-方法封装 | 线程模式 )

2023-09-27 14:28:10 时间





一、消息中心



此处暂时只实现一个单例类 , 后续 注册订阅者 , 处理事件传递 , 取消注册订阅者 , 等功能在该单例类的基础上扩展 ;

package com.eventbus_demo.myeventbus;

public class MyEventBus {

    /**
     * 全局单例
     */
    private static MyEventBus instance;
    private MyEventBus() {

    }
    public static MyEventBus getInstance() {
        if (instance == null) {
            instance = new MyEventBus();
        }
        return instance;
    }

}




二、订阅方法时的注解



定义一个注解 , 该注解用于修饰方法 ElementType.METHOD , 在运行时 , 用户调用 register 注册订阅者时 , 会分析哪个方法中存在该注解 , 将有注解的方法保存起来 , 以便之后的调用 ;

该注解需要保存到运行时 RetentionPolicy.RUNTIME ;

package com.eventbus_demo.myeventbus;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)     // 该注解保留到运行时
@Target(ElementType.METHOD)             // 该注解作用于方法
public @interface MySubscribe {
    /**
     * 注解属性, 设置线程模式, 默认是 POSTING,
     *      即在发布线程调用订阅方法
     * @return
     */
    MyThreadMode threadMode() default MyThreadMode.POSTING;
}




三、订阅方法封装



将 订阅方法 , 订阅方法的线程模式 , 订阅方法接收的事件类型 , 封装到类中 ;

package com.eventbus_demo.myeventbus;

import java.lang.reflect.Method;

/**
 * 该类中用于保存订阅方法相关信息
 */
public class MySubscriberMethod {
    /**
     * 订阅方法
     */
    private final Method method;
    /**
     * 订阅方法的线程模式
     */
    private final MyThreadMode threadMode;
    /**
     * 订阅方法接收的事件类型
     */
    private final Class<?> eventType;

    public MySubscriberMethod(Method method, MyThreadMode threadMode, Class<?> eventType) {
        this.method = method;
        this.threadMode = threadMode;
        this.eventType = eventType;
    }

    public Method getMethod() {
        return method;
    }

    public MyThreadMode getThreadMode() {
        return threadMode;
    }

    public Class<?> getEventType() {
        return eventType;
    }
}





四、订阅对象-方法封装



再次进行封装 , 将 订阅者对象 和 订阅方法 , 封装到一个类中 , 这个类对象是 注册 , 取消注册 , 事件调用 操作的基本单元 ;

获取到该类的对象 , 就可以执行订阅方法 ;

package com.eventbus_demo.myeventbus;

/**
 * 封装 订阅者对象 与 订阅方法
 */
public class MySubscription {
    /**
     * 订阅者对象
     */
    private final Object subscriber;
    /**
     * 订阅方法
     */
    private final MySubscriberMethod subscriberMethod;

    public MySubscription(Object subscriber, MySubscriberMethod subscriberMethod) {
        this.subscriber = subscriber;
        this.subscriberMethod = subscriberMethod;
    }

    public Object getSubscriber() {
        return subscriber;
    }

    public MySubscriberMethod getSubscriberMethod() {
        return subscriberMethod;
    }
}




五、线程模式



仿照 EventBus 的线程模式 , 直接照搬过来 ;

线程模式用法参考 【EventBus】Subscribe 注解分析 ( Subscribe 注解属性 | threadMode 线程模型 | POSTING | MAIN | MAIN_ORDERED | ASYNC) ;

package com.eventbus_demo.myeventbus;

/**
 * 直接使用 EventBus 中的现成模式
 */
public enum MyThreadMode {
    POSTING,
    MAIN,
    MAIN_ORDERED,
    BACKGROUND,
    ASYNC
}