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='打籃球'}