[EventBus源码解析] EventBus.register 方法详述
前情概要
在上一篇中,介绍了EventBus的基本使用方法,以及一部分进阶技巧。本篇及以后的几篇blog将会集中解析EventBus.java,看看作者是如何优雅地实现这个看似简单的事件分发/接收机制。
本篇概述
剖析register的过程,let's get started!
方法签名
完整的register方法签名如下,我们通常调用的register(this)其实最终调用到的register(this, false, 0),另外,使用registerSticky(this)进行调用,其实最终也是走到同一个方法中。
private synchronized void register(Object subscriber, boolean sticky, int priority) {
List<SubscriberMethod> subscriberMethods = subscriberMethodFinder.findSubscriberMethods(subscriber.getClass());
for (SubscriberMethod subscriberMethod : subscriberMethods) {
subscribe(subscriber, subscriberMethod, sticky, priority);
}
}
- 声明为synchronized,是因为其中调用的subscribe方法需要在同步块中执行;
- 这个register方法其实只做了一件事:将目标class的所有onEvent方法取出(通过反射),逐一对事件订阅(通过subscribe)
subscribe翻译过来就是“订阅、订购”,是本次重点解析的内容,下面来揭开她神秘的面纱:
subscribe/订阅
对于熟悉Design Patterns的人来说,第一眼看到EventBus,脑海中浮现的一定是观察者模式/Observer,这个模式的实现方式,无非就是——事件的分发者拥有一个订阅者的列表,每次事件发生时,分发者遍历并通知列表中的订阅者。订阅者可以自由地加入/退出订阅列表。
register是一个订阅的过程,故在subscribe中所做的无非就是“将目标类的目标方法加入到订阅者列表”这件事。让我们结合代码来看。
private void subscribe(Object subscriber, SubscriberMethod subscriberMethod, boolean sticky, int priority) {
Class<?> eventType = subscriberMethod.eventType;
CopyOnWriteArrayList<Subscription> subscriptions = subscriptionsByEventType.get(eventType);
Subscription newSubscription = new Subscription(subscriber, subscriberMethod, priority);
if (subscriptions == null) {
subscriptions = new CopyOnWriteArrayList<Subscription>();
subscriptionsByEventType.put(eventType, subscriptions);
} else {
if (subscriptions.contains(newSubscription)) {
throw new EventBusException("Subscriber " + subscriber.getClass() + " already registered to event "
+ eventType);
}
}
// Starting with EventBus 2.2 we enforced methods to be public (might change with annotations again)
// subscriberMethod.method.setAccessible(true);
int size = subscriptions.size();
for (int i = 0; i <= size; i++) {
if (i == size || newSubscription.priority > subscriptions.get(i).priority) {
subscriptions.add(i, newSubscription);
break;
}
}
List<Class<?>> subscribedEvents = typesBySubscriber.get(subscriber);
if (subscribedEvents == null) {
subscribedEvents = new ArrayList<Class<?>>();
typesBySubscriber.put(subscriber, subscribedEvents);
}
subscribedEvents.add(eventType);
if (sticky) {
if (eventInheritance) {
// Existing sticky events of all subclasses of eventType have to be considered.
// Note: Iterating over all events may be inefficient with lots of sticky events,
// thus data structure should be changed to allow a more efficient lookup
// (e.g. an additional map storing sub classes of super classes: Class -> List<Class>).
Set<Map.Entry<Class<?>, Object>> entries = stickyEvents.entrySet();
for (Map.Entry<Class<?>, Object> entry : entries) {
Class<?> candidateEventType = entry.getKey();
if (eventType.isAssignableFrom(candidateEventType)) {
Object stickyEvent = entry.getValue();
checkPostStickyEventToSubscription(newSubscription, stickyEvent);
}
}
} else {
Object stickyEvent = stickyEvents.get(eventType);
checkPostStickyEventToSubscription(newSubscription, stickyEvent);
}
}
}
Event是分发者/订阅者解耦的关键,两者就是通过Event的class进行通信的。故先取出目标Event的订阅列表(subscriptionsByEventType),将新规则加入进去,如果重复register则会抛出Exception。加入的过程中,会根据优先级决定插入到List中的位置(int值越大,位置越靠前,优先级也就越高)。接下来还会将subscriber(也就是目标class)与新的eventType的对应关系加入到typesBySubscriber中,这个Map在后续的查找中会用到。
对于sticky的的情况,若该eventBus支持事件继承(eventInheritance == true),则将eventType及其祖先类的所有Event都重新分发一遍,若不支持则只分发目标eventType。(此时分发的Event皆来自于stickyEvents)
unregister/注销
本篇最后分析一下注销的过程,代码很简单。
public synchronized void unregister(Object subscriber) {
List<Class<?>> subscribedTypes = typesBySubscriber.get(subscriber);
if (subscribedTypes != null) {
for (Class<?> eventType : subscribedTypes) {
unubscribeByEventType(subscriber, eventType);
}
typesBySubscriber.remove(subscriber);
} else {
Log.w(TAG, "Subscriber to unregister was not registered before: " + subscriber.getClass());
}
}
unregister时,首先将eventtype - subscriber的绑定关系逐一解除(处理subscriptionsByEventType),最后把subscriber - eventTypes解绑。
下期预告
剖析post过程
[EventBus源码解析] EventBus.register 方法详述的更多相关文章
- [EventBus源码解析] EventBus.post 方法详述
前情概要 上一篇blog我们了解了EventBus中register/unregister的过程,对EventBus如何实现观察者模式有了基本的认识.今天我们来看一下它是如何分发一个特定事件的,即po ...
- EventBus源码解析 源码阅读记录
EventBus源码阅读记录 repo地址: greenrobot/EventBus EventBus的构造 双重加锁的单例. static volatile EventBus defaultInst ...
- 【Android】EventBus 源码解析
EventBus 源码解析 本文为 Android 开源项目实现原理解析 中 EventBus 部分项目地址:EventBus,分析的版本:ccc2771,Demo 地址:EventBus Demo分 ...
- Spring源码解析之八finishBeanFactoryInitialization方法即初始化单例bean
Spring源码解析之八finishBeanFactoryInitialization方法即初始化单例bean 七千字长文深刻解读,Spirng中是如何初始化单例bean的,和面试中最常问的Sprin ...
- Android EventBus源码解析 带你深入理解EventBus
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/40920453,本文出自:[张鸿洋的博客] 上一篇带大家初步了解了EventBus ...
- 源码解析-EventBus
示例使用 时序图 源码解读 EventBus 使用 官网定义:EventBus 是一个使用 Java 写的观察者模式,解耦的 Android 开源库.EventBus 只需要几行代码即可解耦简化代码, ...
- 多线程爬坑之路-Thread和Runable源码解析之基本方法的运用实例
前面的文章:多线程爬坑之路-学习多线程需要来了解哪些东西?(concurrent并发包的数据结构和线程池,Locks锁,Atomic原子类) 多线程爬坑之路-Thread和Runable源码解析 前面 ...
- [Java多线程]-Thread和Runable源码解析之基本方法的运用实例
前面的文章:多线程爬坑之路-学习多线程需要来了解哪些东西?(concurrent并发包的数据结构和线程池,Locks锁,Atomic原子类) 多线程爬坑之路-Thread和Runable源码解析 前面 ...
- EventBus源码解析
用例 本文主要按照如下例子展开: //1. 新建bus对象,默认仅能在主线程上对消息进行调度 Bus bus = new Bus(); // maybe singleton //2. 新建类A(sub ...
随机推荐
- Lantern卫星接收器:为你提供免费上网服务
包括笔者在内,许多现代人的日常生活都无法离开网络,因为在网络上我们几乎可以找到任何我们需要的信息.但你是否有想过在户外无网络信号的情况下如何接收网络数据呢?一个名为Outernet Inc.的公司为我 ...
- What is Requirement ?
The IEEE 610 standard defines a requirement as: (1). a condition or capability needed by a user to s ...
- 【技术无关】GPS转北斗卫星定位 系统调研
前言 陆地交通运输是当前GPS卫星定位系统最大的应用领域,我省自08年实施卫星定位系统建设以来,在车辆监控和调度方面发挥了突出的作用:主要功能包括车辆跟踪.线路规划和导航.信息查询.交通指挥.紧急援助 ...
- Python开发库
在我多年的 Python 编程经历以及在 Github 上的探索漫游过程中,我发掘到一些很不错的 Python 开发包,这些包大大简化了开发过程,而本文就是为了向大家推荐这些开发包. 请注意我特别排除 ...
- Git 一次性 pull push 所有的分支
/********************************************************************************* * Git 一次性 pull pu ...
- Merge Intervals
Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...
- border-image(转载)
本文转自:http://www.zhangxinxu.com/wordpress/2010/01/css3-border-image%E8%AF%A6%E8%A7%A3%E3%80%81%E5%BA% ...
- sscanf格式化输出
char DesChar[20] = {0}; char* SouChar= "1cZCD23456abEFdedfB"; sscanf(SouChar,"%[^A-Z] ...
- vb6 关闭显示器
Option Explicit Private Const WM_SYSCOMMAND As Long = &H112 Private Const SC_MONITORPOWER As Lon ...
- Ubuntu14.04搭建Caffe(仅CPU)
一直以来都没有写博客的习惯,后来发现以前做的工作如果不注意及时整理和记录往往丢失的很快.对我而言这是一篇具有重要意义的文章,好的习惯要持之以恒,以后的日子我会常驻博客园!由于本人水平有限,智商略低,欢 ...