2019獨角獸企業重金招聘Python工程師標準>>>
本文由云+社區發表
事件總線核心邏輯的實現。
<!--more-->
EventBus的作用
Android中存在各種通信場景,如Activity
之間的跳轉,Activity
與Fragment
以及其他組件之間的交互,以及在某個耗時操作(如請求網絡)之后的callback回調等,互相之之間往往需要持有對方的引用,每個場景的寫法也有差異,導致耦合性較高且不便維護。以Activity
和Fragment
的通信為例,官方做法是實現一個接口,然后持有對方的引用,再強行轉成接口類型,導致耦合度偏高。再以Activity
的返回為例,一方需要設置setResult
,而另一方需要在onActivityResult
做對應處理,如果有多個返回路徑,代碼就會十分臃腫。而SimpleEventBus
(本文最終實現的簡化版事件總線)的寫法如下:
public class MainActivity extends AppCompatActivity {TextView mTextView;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mTextView = findViewById(R.id.tv_demo);mTextView.setText("MainActivity");mTextView.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {Intent intent = new Intent(MainActivity.this, SecondActivity.class);startActivity(intent);}});EventBus.getDefault().register(this);}@Subscribe(threadMode = ThreadMode.MAIN)public void onReturn(Message message) {mTextView.setText(message.mContent);}@Overrideprotected void onDestroy() {super.onDestroy();EventBus.getDefault().unregister(this);}}
來源Activity
:
public class SecondActivity extends AppCompatActivity {TextView mTextView;@Overrideprotected void onCreate(@Nullable Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mTextView = findViewById(R.id.tv_demo);mTextView.setText("SecondActivity,點擊返回");mTextView.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {Message message = new Message();message.mContent = "從SecondActivity返回";EventBus.getDefault().post(message);finish();}});}}
效果如下:
似乎只是換了一種寫法,但在場景愈加復雜后,EventBus
能夠體現出更好的解耦能力。
背景知識
主要涉及三方面的知識:
-
觀察者模式(or 發布-訂閱模式)
-
Android消息機制
-
Java并發編程
本文可以認為是greenrobot/EventBus這個開源庫的源碼閱讀指南,筆者在看設計模式相關書籍的時候了解到這個庫,覺得有必要實現一下核心功能以加深理解。
實現過程
EventBus
的使用分三個步驟:注冊監聽、發送事件和取消監聽,相應本文也將分這三步來實現。
注冊監聽
定義一個注解:
@Retention(RetentionPolicy.RUNTIME)@Target(ElementType.METHOD)public @interface Subscribe {ThreadMode threadMode() default ThreadMode.POST;}
greenrobot/EventBus
還支持優先級和粘性事件,這里只支持最基本的能力:區分線程,因為如更新UI的操作必須放在主線程。ThreadMode
如下:
public enum ThreadMode {MAIN, // 主線程POST, // 發送消息的線程ASYNC // 新開一個線程發送}
在對象初始化的時候,使用register
方法注冊,該方法會解析被注冊對象的所有方法,并解析聲明了注解的方法(即觀察者),核心代碼如下:
public class EventBus {...public void register(Object subscriber) {if (subscriber == null) {return;}synchronized (this) {subscribe(subscriber);}}...private void subscribe(Object subscriber) {if (subscriber == null) {return;}// TODO 巨踏馬難看的縮進Class<?> clazz = subscriber.getClass();while (clazz != null && !isSystemClass(clazz.getName())) {final Method[] methods = clazz.getDeclaredMethods();for (Method method : methods) {Subscribe annotation = method.getAnnotation(Subscribe.class);if (annotation != null) {Class<?>[] paramClassArray = method.getParameterTypes();if (paramClassArray != null && paramClassArray.length == 1) {Class<?> paramType = convertType(paramClassArray[0]);EventType eventType = new EventType(paramType);SubscriberMethod subscriberMethod = new SubscriberMethod(method, annotation.threadMode(), paramType);realSubscribe(subscriber, subscriberMethod, eventType);}}}clazz = clazz.getSuperclass();}}...private void realSubscribe(Object subscriber, SubscriberMethod method, EventType eventType) {CopyOnWriteArrayList<Subscription> subscriptions = mSubscriptionsByEventtype.get(subscriber);if (subscriptions == null) {subscriptions = new CopyOnWriteArrayList<>();}Subscription subscription = new Subscription(subscriber, method);if (subscriptions.contains(subscription)) {return;}subscriptions.add(subscription);mSubscriptionsByEventtype.put(eventType, subscriptions);}...}
執行過這些邏輯后,該對象所有的觀察者方法都會被存在一個Map中,其Key是EventType
,即觀察事件的類型,Value是訂閱了該類型事件的所有方法(即觀察者)的一個列表,每個方法和對象一起封裝成了一個Subscription
類:
public class Subscription {public final Reference<Object> subscriber;public final SubscriberMethod subscriberMethod;public Subscription(Object subscriber, SubscriberMethod subscriberMethod) {this.subscriber = new WeakReference<>(subscriber);// EventBus3 沒用弱引用?this.subscriberMethod = subscriberMethod;}@Overridepublic int hashCode() {return subscriber.hashCode() + subscriberMethod.methodString.hashCode();}@Overridepublic boolean equals(Object obj) {if (obj instanceof Subscription) {Subscription other = (Subscription) obj;return subscriber == other.subscribe&& subscriberMethod.equals(other.subscriberMethod);} else {return false;}}}
如此,便是注冊監聽方法的核心邏輯了。
消息發送
消息的發送代碼很簡單:
public class EventBus {...private EventDispatcher mEventDispatcher = new EventDispatcher();private ThreadLocal<Queue<EventType>> mThreadLocalEvents = new ThreadLocal<Queue<EventType>>() {@Overrideprotected Queue<EventType> initialValue() {return new ConcurrentLinkedQueue<>();}};...public void post(Object message) {if (message == null) {return;}mThreadLocalEvents.get().offer(new EventType(message.getClass()));mEventDispatcher.dispatchEvents(message);}...}
比較復雜一點的是需要根據注解聲明的線程模式在對應的線程進行發布:
public class EventBus {...private class EventDispatcher {private IEventHandler mMainEventHandler = new MainEventHandler();private IEventHandler mPostEventHandler = new DefaultEventHandler();private IEventHandler mAsyncEventHandler = new AsyncEventHandler();void dispatchEvents(Object message) {Queue<EventType> eventQueue = mThreadLocalEvents.get();while (eventQueue.size() > 0) {handleEvent(eventQueue.poll(), message);}}private void handleEvent(EventType eventType, Object message) {List<Subscription> subscriptions = mSubscriptionsByEventtype.get(eventType);if (subscriptions == null) {return;}for (Subscription subscription : subscriptions) {IEventHandler eventHandler = getEventHandler(subscription.subscriberMethod.threadMode);eventHandler.handleEvent(subscription, message);}}private IEventHandler getEventHandler(ThreadMode mode) {if (mode == ThreadMode.ASYNC) {return mAsyncEventHandler;}if (mode == ThreadMode.POST) {return mPostEventHandler;}return mMainEventHandler;}}// end of the class...}
三種線程模式分別如下,DefaultEventHandler
(在發布線程執行觀察者放方法):
public class DefaultEventHandler implements IEventHandler {@Overridepublic void handleEvent(Subscription subscription, Object message) {if (subscription == null || subscription.subscriber.get() == null) {return;}try {subscription.subscriberMethod.method.invoke(subscription.subscriber.get(), message);} catch (IllegalAccessException | InvocationTargetException e) {e.printStackTrace();}}}
MainEventHandler
(在主線程執行):
public class MainEventHandler implements IEventHandler {private Handler mMainHandler = new Handler(Looper.getMainLooper());DefaultEventHandler hanlder = new DefaultEventHandler();@Overridepublic void handleEvent(final Subscription subscription, final Object message) {mMainHandler.post(new Runnable() {@Overridepublic void run() {hanlder.handleEvent(subscription, message);}});}}
AsyncEventHandler
(新開一個線程執行):
public class AsyncEventHandler implements IEventHandler {private DispatcherThread mDispatcherThread;private IEventHandler mEventHandler = new DefaultEventHandler();public AsyncEventHandler() {mDispatcherThread = new DispatcherThread(AsyncEventHandler.class.getSimpleName());mDispatcherThread.start();}@Overridepublic void handleEvent(final Subscription subscription, final Object message) {mDispatcherThread.post(new Runnable() {@Overridepublic void run() {mEventHandler.handleEvent(subscription, message);}});}private class DispatcherThread extends HandlerThread {// 關聯了AsyncExecutor消息隊列的HandleHandler mAsyncHandler;DispatcherThread(String name) {super(name);}public void post(Runnable runnable) {if (mAsyncHandler == null) {throw new NullPointerException("mAsyncHandler == null, please call start() first.");}mAsyncHandler.post(runnable);}@Overridepublic synchronized void start() {super.start();mAsyncHandler = new Handler(this.getLooper());}}}
以上便是發布消息的代碼。
注銷監聽
最后一個對象被銷毀還要注銷監聽,否則容易導致內存泄露,目前SimpleEventBus
用的是WeakReference
,能夠通過GC自動回收,但不知道greenrobot/EventBus
為什么沒這樣實現,待研究。注銷監聽其實就是遍歷Map,拿掉該對象的訂閱即可:
public class EventBus {...public void unregister(Object subscriber) {if (subscriber == null) {return;}Iterator<CopyOnWriteArrayList<Subscription>> iterator = mSubscriptionsByEventtype.values().iterator();while (iterator.hasNext()) {CopyOnWriteArrayList<Subscription> subscriptions = iterator.next();if (subscriptions != null) {List<Subscription> foundSubscriptions = new LinkedList<>();for (Subscription subscription : subscriptions) {Object cacheObject = subscription.subscriber.get();if (cacheObject == null || cacheObject.equals(subscriber)) {foundSubscriptions.add(subscription);}}subscriptions.removeAll(foundSubscriptions);}// 如果針對某個Event的訂閱者數量為空了,那么需要從map中清除if (subscriptions == null || subscriptions.size() == 0) {iterator.remove();}}}...}
以上便是事件總線最核心部分的代碼實現,完整代碼見vimerzhao/SimpleEventBus,后面發現問題更新或者進行升級也只會改動倉庫的代碼。
局限性
由于時間關系,目前只研究了EventBus
的核心部分,還有幾個值得深入研究的點,在此記錄一下,也歡迎路過的大牛指點一二。
性能問題
實際使用時,注解和反射會導致性能問題,但EventBus3
已經通過Subscriber Index
基本解決了這一問題,實現也非常有意思,是通過注解處理器(Annotation Processor
)把耗時的邏輯從運行期提前到了編譯期,通過編譯期生成的索引來給運行期提速,這也是這個名字的由來。
可用性問題
如果訂閱者很多會不會影響體驗,畢竟原始的方法是點對點的消息傳遞,不會有這種問題,如果部分訂閱者尚未初始化怎么辦。等等。目前EventBus3
提供了優先級和粘性事件的屬性來進一步滿足開發需求。但是否徹底解決問題了還有待驗證。
跨進程問題
EventBus
是進程內的消息管理機制,并且從開源社區的反饋來看這個項目是非常成功的,但稍微有點體量的APP都做了進程拆分,所以是否有必要支持多進程,能否在保證性能的情況下提供同等的代碼解耦能力,也值得繼續挖掘。目前有lsxiao/Apollo和Xiaofei-it/HermesEventBus等可供參考。
參考
-
greenrobot/EventBus
-
hehonghui/AndroidEventBus
-
《Android開發藝術探索》第十章
此文已由作者授權騰訊云+社區發布