美文网首页
简易EventBus实现

简易EventBus实现

作者: goodl | 来源:发表于2017-06-12 09:20 被阅读0次

EventBus:

import java.lang.reflect.Method;  
import java.util.ArrayList;  
import java.util.HashMap;  
import java.util.Iterator;  
  
public class EventBus {  
    private static volatile EventBus defaultInstance;  
    private final HashMap<Class<?>, ArrayList<SubscriberMethod>> events;  
  
    private EventBus() {  
        events = new HashMap<Class<?>, ArrayList<SubscriberMethod>>();  
    }  
  
    public static EventBus instance() {  
        if (defaultInstance == null) {  
            synchronized (EventBus.class) {  
                if (defaultInstance == null) {  
                    defaultInstance = new EventBus();  
                }  
            }  
        }  
        return defaultInstance;  
    }  
  
    public void register(Object subscriber, Class<?> eventType) throws NoSuchMethodException {  
        register(subscriber, "onEvent", eventType);  
    }  
  
    public synchronized void register(Object subscriber, String method, Class<?> eventType)  
            throws NoSuchMethodException {  
        SubscriberMethod subscriberMethod = getSubscriberMethod(subscriber, method, eventType);  
        if (events.containsKey(eventType)) {  
            events.get(eventType).add(subscriberMethod);  
        } else {  
            ArrayList<SubscriberMethod> methods = new ArrayList<SubscriberMethod>();  
            methods.add(subscriberMethod);  
            events.put(eventType, methods);  
        }  
    }  
  
    public void unregister(Object subscriber, Class<?> eventType) throws NoSuchMethodException {  
        unregister(subscriber, "onEvent", eventType);  
    }  
  
    public synchronized void unregister(Object subscriber, String method, Class<?> eventType)  
            throws NoSuchMethodException {  
        if (events.containsKey(eventType)) {  
            SubscriberMethod sm = getSubscriberMethod(subscriber, method, eventType);  
            ArrayList<SubscriberMethod> list = events.get(eventType);  
            Iterator<SubscriberMethod> iterator = list.iterator();  
            while (iterator.hasNext()) {  
                SubscriberMethod subscriberMethod = iterator.next();  
                if (subscriberMethod.equals(sm)) {  
                    iterator.remove();  
                }  
            }  
        }  
    }  
  
    private SubscriberMethod getSubscriberMethod(Object subscriber, String method,  
            Class<?> eventType) throws NoSuchMethodException {  
        Method m = subscriber.getClass().getDeclaredMethod(method, eventType);  
        if (!m.isAccessible()) {  
            m.setAccessible(true);  
        }  
        return new SubscriberMethod(subscriber, m, eventType);  
    }  
  
    public void post(Object event) {  
        try {  
            ArrayList<SubscriberMethod> list = events.get(event.getClass());  
            if (list != null) {  
                Iterator<SubscriberMethod> iterator = list.iterator();  
                while (iterator.hasNext()) {  
                    SubscriberMethod sm = iterator.next();  
                    sm.method.invoke(sm.subscriber, event);  
                }  
            }  
        } catch (Exception e) {  
            Log.e("EventBus post error", e);  
        }  
    }  
  
}  

SubscriberMethod

import java.lang.reflect.Method;  
  
final class SubscriberMethod {  
    final Object subscriber;  
    final Method method;  
    final Class<?> eventType;  
    /** Used for efficient comparison */  
    String methodString;  
  
    SubscriberMethod(Object subscriber, Method method, Class<?> eventType) {  
        this.subscriber = subscriber;  
        this.method = method;  
        this.eventType = eventType;  
    }  
  
    @Override  
    public boolean equals(Object other) {  
        if (other instanceof SubscriberMethod) {  
            checkMethodString();  
            SubscriberMethod otherSubscriberMethod = (SubscriberMethod) other;  
            otherSubscriberMethod.checkMethodString();  
            // Don't use method.equals because of  
            // http://code.google.com/p/android/issues/detail?id=7811#c6  
            return methodString.equals(otherSubscriberMethod.methodString);  
        } else {  
            return false;  
        }  
    }  
  
    private synchronized void checkMethodString() {  
        if (methodString == null) {  
            // Method.toString has more overhead, just take relevant parts of  
            // the method  
            StringBuilder builder = new StringBuilder(128);  
            builder.append(subscriber.getClass().getName());  
            builder.append('#').append(method.getName());  
            builder.append('(').append(eventType.getName());  
            methodString = builder.toString();  
        }  
    }  
 
}  

相关文章

  • 简易EventBus实现

    EventBus: SubscriberMethod

  • 手写事件总线eventBus

    在vue中,我们有时会用eventBus进行简易组件通信,那么这个eventBus究竟是如何实现的呢?eventB...

  • JS简易实现eventBus

    一、 源码 二、 绑定eventBus.on(eventName, function[, obj]); 三 触发e...

  • 自定义简易EventBus

    本章通过自定义一个简易的EventBus来了解EventBus的框架思路; EventBus的设计思路是通过向内存...

  • EventBus源码解析

    知识点汇总: 一:EventBus框架概述 二:EventBus的注册实现原理 三:EventBus的事件分发实现...

  • [Flutter]EventBus的使用和底层实现分析

    什么是EventBus EventBus是全局事件总线,底层通过Stream来实现;它可以实现不同页面的跨层访问,...

  • EventBus的使用

    什么是EventBus EventBus是全局事件总线,底层通过Stream来实现;它可以实现不同页面的跨层访问,...

  • EventBus

    一、EventBus的原理 EventBus是全局事件总线,底层通过Stream来实现;它可以实现不同页面的跨层访...

  • 组件间通信

    组件间通信 EventBus实现通信在Activity注册EventBus,在Activity写入消息订阅接收消息...

  • 一篇讲明白EventBus

    先说EventBus是什么: EventBus是 基于 订阅/发布 模式实现的 基于事件的异步分发处理系统。 ...

网友评论

      本文标题:简易EventBus实现

      本文链接:https://www.haomeiwen.com/subject/ntzkqxtx.html