带有语法强类型检查的事件总线(EventBus)框架
近段时间,事件总线(EventBus)技术貌似很火,小弟也膜拜了一下,虽然思想与技术都非常先进,但总感觉有什么不妥,首先是那几个onEvent…方法老记不住,要是写错怎么办,其次是接收方对于接收的事件也没有类型约定,感觉不怎么严谨。
带着以上疑虑小弟研究了一下,看看能不能用泛型接口改进一下,还真被小弟研究出一点道道来,虽然只有一个大体框架,也在此抛砖引玉一下,希望能激发出更多好的想法。
废话不多说了,直接上代码,先上用例代码:
package com.test;
public class Test{
// 事件的订阅者,四种模式的事件都订阅了,并且每种模式的事件类型都不同
public static class StringEventReceiverimplements
EventBus.EventReceiver<String>,
EventBus.EventAsyncReceiver<Integer>,
EventBus.EventBackgroundThreadReceiver<Boolean>,
EventBus.EventMainThreadReceiver<Double> {
@Override
public void onEventMainThread(Double event) {
System.out.println("onEventMainThread:" +event);
}
@Override
public void onEventBackgroundThread(Boolean event) {
System.out.println("onEventBackgroundThread:" +event);
}
@Override
public void onEventAsync(Integer event) {
System.out.println("onEventAsync:" +event);
}
@Override
public void onEvent(String event) {
System.out.println("onEvent:" +event);
}
}
public static void main(String[] args) {
StringEventReceiver stringEventReceiver =new StringEventReceiver();
EventBus.getInstance().registerEventReceiver(stringEventReceiver,
String.class);
EventBus.getInstance().registerEventMainThreadReceiver(
stringEventReceiver, Double.class);
EventBus.getInstance().registerEventBackgroundThreadReceiver(
stringEventReceiver, Boolean.class);
EventBus.getInstance().registerAsyncReceiver(stringEventReceiver,
Integer.class);
// 订阅成功后,分别发送不同类型的事件
EventBus.getInstance().post("好学生");
EventBus.getInstance().post(0.1);
EventBus.getInstance().post(1);
EventBus.getInstance().post(true);
// 以下两种类型的事件没有订阅者
EventBus.getInstance().post(1.5f);
EventBus.getInstance().post(new Object());
}
}
以下是测试结果:
onEvent:好学生
onEventMainThread:0.1
onEventAsync:1
onEventBackgroundThread:true
下面是EventBus源代码,欢迎大家提出自己不同的见解:
package com.test;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
public class EventBus {
// 采用泛型接口的好处是,编译器自动帮你补全方法
// 如EventBus一样,四种模式的接收者接口
// 每种模式一个接口,按需实现
public interface EventReceiver<E> {
public void onEvent(E event);
}
public interface EventMainThreadReceiver<E> {
public void onEventMainThread(E event);
}
public interface EventBackgroundThreadReceiver<E> {
public void onEventBackgroundThread(E event);
}
public interface EventAsyncReceiver<E> {
public void onEventAsync(E event);
}
private static class SingletonHelper {
public static EventBus instance =new EventBus();
}
private EventBus() {
}
public static EventBus getInstance() {
return SingletonHelper.instance;
}
private HashMap<Class<?>,List<EventBus.EventReceiver<?>>>eventReceiverHashMap = newHashMap<Class<?>, List<EventBus.EventReceiver<?>>>();
private HashMap<Class<?>,List<EventBus.EventMainThreadReceiver<?>>>eventMainThreadReceiverHashMap = new HashMap<Class<?>,List<EventBus.EventMainThreadReceiver<?>>>();
private HashMap<Class<?>,List<EventBus.EventBackgroundThreadReceiver<?>>>eventBackgroundThreadReceiverHashMap = new HashMap<Class<?>,List<EventBus.EventBackgroundThreadReceiver<?>>>();
private HashMap<Class<?>,List<EventBus.EventAsyncReceiver<?>>>eventAsyncReceiverHashMap = newHashMap<Class<?>, List<EventBus.EventAsyncReceiver<?>>>();
// 订阅事件的方法,相应的有四种模式,这里编译器自动对事件的类型进行检查
// 订阅者必须实现相应类型、相应模式的接收者接口,才能订阅相对应的事件
public <E>void registerEventReceiver(EventReceiver<E>receiver,
Class<E> cls) {
if (receiver ==null) {
throw new NullPointerException(DebugUtils.STACK_TRACE()
+ "receiver ==null");
}
if (cls ==null) {
throw new NullPointerException(DebugUtils.STACK_TRACE()
+ "cls ==null");
}
List<EventBus.EventReceiver<?>> eventReceiverList = eventReceiverHashMap
.get(cls);
if (eventReceiverList ==null) {
eventReceiverList =new LinkedList<EventBus.EventReceiver<?>>();
eventReceiverHashMap.put(cls,eventReceiverList);
}
eventReceiverList.add(receiver);
}
public <E>void registerEventMainThreadReceiver(
EventMainThreadReceiver<E> receiver, Class<E> cls) {
if (receiver ==null) {
throw new NullPointerException(DebugUtils.STACK_TRACE()
+ "receiver ==null");
}
if (cls ==null) {
throw new NullPointerException(DebugUtils.STACK_TRACE()
+ "cls ==null");
}
List<EventBus.EventMainThreadReceiver<?>> eventMainThreadReceiverList = eventMainThreadReceiverHashMap
.get(cls);
if (eventMainThreadReceiverList ==null) {
eventMainThreadReceiverList =newLinkedList<EventBus.EventMainThreadReceiver<?>>();
eventMainThreadReceiverHashMap
.put(cls, eventMainThreadReceiverList);
}
eventMainThreadReceiverList.add(receiver);
}
public <E>void registerEventBackgroundThreadReceiver(
EventBackgroundThreadReceiver<E> receiver, Class<E> cls) {
if (receiver ==null) {
throw new NullPointerException(DebugUtils.STACK_TRACE()
+ "receiver ==null");
}
if (cls ==null) {
throw new NullPointerException(DebugUtils.STACK_TRACE()
+ "cls ==null");
}
List<EventBus.EventBackgroundThreadReceiver<?>> eventBackgroundThreadReceiverList = eventBackgroundThreadReceiverHashMap
.get(cls);
if (eventBackgroundThreadReceiverList ==null) {
eventBackgroundThreadReceiverList =newLinkedList<EventBus.EventBackgroundThreadReceiver<?>>();
eventBackgroundThreadReceiverHashMap.put(cls,
eventBackgroundThreadReceiverList);
}
eventBackgroundThreadReceiverList.add(receiver);
}
public <E>void registerAsyncReceiver(EventAsyncReceiver<E>receiver,
Class<E> cls) {
if (receiver ==null) {
throw new NullPointerException(DebugUtils.STACK_TRACE()
+ "receiver ==null");
}
if (cls ==null) {
throw new NullPointerException(DebugUtils.STACK_TRACE()
+ "cls ==null");
}
List<EventBus.EventAsyncReceiver<?>> eventAsyncReceiverList = eventAsyncReceiverHashMap
.get(cls);
if (eventAsyncReceiverList ==null) {
eventAsyncReceiverList =newLinkedList<EventBus.EventAsyncReceiver<?>>();
eventAsyncReceiverHashMap.put(cls,eventAsyncReceiverList);
}
eventAsyncReceiverList.add(receiver);
}
// 事件发送方法,对事件的类型和模式都进行匹配
public <E>void post(E event) {
if (event ==null) {
throw new NullPointerException(DebugUtils.STACK_TRACE()
+ "event ==null");
}
List<EventReceiver<?>> eventReceiverList =eventReceiverHashMap
.get(event.getClass());
if (eventReceiverList !=null) {
for (inti = 0, sz =eventReceiverList.size(); i < sz; i++) {
EventBus.EventReceiver<E> eventReceiver =(EventReceiver<E>) eventReceiverList
.get(i);
if (eventReceiver !=null) {
eventReceiver.onEvent(event);
}
}
}
List<EventMainThreadReceiver<?>> eventMainThreadReceiverList = eventMainThreadReceiverHashMap
.get(event.getClass());
if (eventMainThreadReceiverList !=null) {
for (inti = 0, sz =eventMainThreadReceiverList.size(); i < sz; i++) {
EventBus.EventMainThreadReceiver<E> eventMainThreadReceiver = (EventMainThreadReceiver<E>) eventMainThreadReceiverList
.get(i);
if (eventMainThreadReceiver !=null) {
eventMainThreadReceiver.onEventMainThread(event);
}
}
}
List<EventBus.EventBackgroundThreadReceiver<?>> eventBackgroundThreadReceiverList = eventBackgroundThreadReceiverHashMap
.get(event.getClass());
if (eventBackgroundThreadReceiverList !=null) {
for (inti = 0, sz =eventBackgroundThreadReceiverList.size(); i < sz; i++) {
EventBus.EventBackgroundThreadReceiver<E> eventBackgroundThreadReceiver = (EventBackgroundThreadReceiver<E>) eventBackgroundThreadReceiverList
.get(i);
if (eventBackgroundThreadReceiver !=null) {
eventBackgroundThreadReceiver
.onEventBackgroundThread(event);
}
}
}
List<EventAsyncReceiver<?>> eventAsyncReceiverList =eventAsyncReceiverHashMap
.get(event.getClass());
if (eventAsyncReceiverList !=null) {
for (inti = 0, sz =eventAsyncReceiverList.size(); i < sz; i++) {
EventBus.EventAsyncReceiver<E> eventAsyncReceiver = (EventAsyncReceiver<E>) eventAsyncReceiverList
.get(i);
if (eventAsyncReceiver !=null) {
eventAsyncReceiver.onEventAsync(event);
}
}
}
}
}