EventBus 框架解耦和核心原理(事件总线,订阅,取消订阅,解耦,根据自己看源码的理解手写一个简洁版的EventBus)
下面是EventBus类的核心代码
public void regist(Object obj) {
List<SubscribeMethod> subscribeMethods = mCacheMap.get(obj);
if (subscribeMethods == null) {
subscribeMethods = findSubscribeMethod(obj);
mCacheMap.put(obj, subscribeMethods);
}
}
private List<SubscribeMethod> findSubscribeMethod(Object obj) {
List<SubscribeMethod> list = new ArrayList<>();
Class<?> aClass = obj.getClass();
while (aClass != null) {
Method[] methods = aClass.getDeclaredMethods();
for (Method method : methods) {
Subscribe subscribe = method.getAnnotation(Subscribe.class);
if (subscribe == null) {
continue;
}
Class<?>[] parameterTypes = method.getParameterTypes();
if (parameterTypes.length != 1) {
throw new IllegalArgumentException("EventBus params only one");
}
ThreadMode threadMode = subscribe.threadMode();
SubscribeMethod subscribeMethod = new SubscribeMethod(method, threadMode, parameterTypes[0]);
list.add(subscribeMethod);
}
aClass = aClass.getSuperclass();
}
return list;
}
public void post(Object type) {
Set<Object> keySet = mCacheMap.keySet();
Iterator<Object> iterator = keySet.iterator();
while (iterator.hasNext()) {
Object obj = iterator.next();
List<SubscribeMethod> subscribeMethods = mCacheMap.get(obj);
for (SubscribeMethod subscribeMethod : subscribeMethods) {
if (subscribeMethod.getType().isAssignableFrom(type.getClass())) {
invoke(subscribeMethod, obj, type);
}
}
}
}
private void invoke(SubscribeMethod subscribeMethod, Object obj, Object type) {
Method method = subscribeMethod.getMethod();
if (isMainThread()){
if (subscribeMethod.getThreadMode() == ThreadMode.MAIN){
try {
method.invoke(obj, type);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
} else{
if (subscribeMethod.getThreadMode() == ThreadMode.BACKGOUND){
try {
method.invoke(obj, type);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}
}
public void unRegist(Object obj) {
mCacheMap.remove(obj);
}
public boolean isMainThread() {
return Looper.getMainLooper() == Looper.myLooper();
}
自定义注解类(ThraedMode就是一个枚举)
@Target(value = ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Subscribe {
ThreadMode threadMode() default ThreadMode.MAIN;
}
事件总线中的实体类(主要封装了Method,TheadMode,Type(参数类型))
private Method method;
private ThreadMode threadMode;
private Class<?> type;
public SubscribeMethod(Method method, ThreadMode threadMode, Class<?> type) {
this.method = method;
this.threadMode = threadMode;
this.type = type;
}
public Method getMethod() {
return method;
}
public void setMethod(Method method) {
this.method = method;
}
public ThreadMode getThreadMode() {
return threadMode;
}
public void setThreadMode(ThreadMode threadMode) {
this.threadMode = threadMode;
}
public Class<?> getType() {
return type;
}
public void setType(Class<?> type) {
this.type = type;
}
下面是发送和接收
@Subscribe(threadMode = ThreadMode.MAIN)
public void onMessageEvent(MessageEvent event){
Log.d("====>","主线程:"+event.toString());
}
@Subscribe(threadMode = ThreadMode.BACKGOUND)
public void onMessageEvent1(MessageEvent event){
Log.d("====>","子线程:"+event.toString());
}
-------------------------------
public void main(View view) {
EventBus.getDefault().post(new MessageEvent("罗大佑","唱歌"));
}
public void sub(View view) {
new Thread(){
@Override
public void run() {
super.run();
EventBus.getDefault().post(new MessageEvent("科比","打篮球"));
}
}.start();
}
运行结果如下
2019-10-10 10:15:00.834 6614-6614/com.example.eventbuspro D/====1>: MessageEvent{name='罗大佑', hobby='唱歌'}
2019-10-10 10:15:02.202 6614-6664/com.example.eventbuspro D/====2>: MessageEvent{name='科比', hobby='打篮球'}