本文章已授權(quán)玉剛說(shuō)微信公眾號(hào)轉(zhuǎn)載
上一篇:深扒 EventBus:解鎖新姿勢(shì)
看源碼的目的
避開(kāi)使用的誤區(qū)
了解框架的實(shí)現(xiàn)原理
借鑒里面使用的設(shè)計(jì)模式和思想
EventBus.getDefault
首先從最常用的 getDefault 方法開(kāi)始分析仲锄,我們使用 EventBus 必須先調(diào)用這個(gè)方法
首先讓我們先看一下方法上面的注釋
Convenience singleton for apps using a process-wide EventBus instance
方便的單例應(yīng)用程序使用進(jìn)程范圍的EventBus實(shí)例
一看注釋就知道這個(gè)方法是什么儒喊,這個(gè)方法采用了單例設(shè)計(jì)模式中的懶漢式(沒(méi)用到的時(shí)候不初始化,用的時(shí)候才初始化)怀愧,這個(gè)方法體里面還使用了 synchronized 同步代碼塊余赢,就連單例對(duì)象也被volatile關(guān)鍵字修飾妻柒,可想而知 EventBus 的懶漢單例已經(jīng)處理了線程安全的問(wèn)題
EventBusBuilder
EventBus.getDefault 最終還是會(huì)調(diào)用 EventBus 的無(wú)參構(gòu)造函數(shù)
在這里我們發(fā)現(xiàn)了一個(gè)類 EventBusBuilder蛤奢,光看類名早已看透了一切
看透歸看透陶贼,源碼還是要看一看,不然怎么繼續(xù)寫(xiě)下去痹屹,先簡(jiǎn)單擼一遍源碼
public class EventBusBuilder {
private final static ExecutorService DEFAULT_EXECUTOR_SERVICE = Executors.newCachedThreadPool();
boolean logSubscriberExceptions = true;
boolean logNoSubscriberMessages = true;
boolean sendSubscriberExceptionEvent = true;
boolean sendNoSubscriberEvent = true;
boolean throwSubscriberException;
boolean eventInheritance = true;
boolean ignoreGeneratedIndex;
boolean strictMethodVerification;
ExecutorService executorService = DEFAULT_EXECUTOR_SERVICE;
List<Class<?>> skipMethodVerificationForClasses;
List<SubscriberInfoIndex> subscriberInfoIndexes;
Logger logger;
MainThreadSupport mainThreadSupport;
EventBusBuilder() {}
public EventBusBuilder logSubscriberExceptions(boolean logSubscriberExceptions) {
this.logSubscriberExceptions = logSubscriberExceptions;
return this;
}
public EventBusBuilder logNoSubscriberMessages(boolean logNoSubscriberMessages) {
this.logNoSubscriberMessages = logNoSubscriberMessages;
return this;
}
public EventBusBuilder sendSubscriberExceptionEvent(boolean sendSubscriberExceptionEvent) {
this.sendSubscriberExceptionEvent = sendSubscriberExceptionEvent;
return this;
}
public EventBusBuilder sendNoSubscriberEvent(boolean sendNoSubscriberEvent) {
this.sendNoSubscriberEvent = sendNoSubscriberEvent;
return this;
}
public EventBusBuilder throwSubscriberException(boolean throwSubscriberException) {
this.throwSubscriberException = throwSubscriberException;
return this;
}
public EventBusBuilder eventInheritance(boolean eventInheritance) {
this.eventInheritance = eventInheritance;
return this;
}
public EventBusBuilder executorService(ExecutorService executorService) {
this.executorService = executorService;
return this;
}
public EventBusBuilder skipMethodVerificationFor(Class<?> clazz) {
if (skipMethodVerificationForClasses == null) {
skipMethodVerificationForClasses = new ArrayList<>();
}
skipMethodVerificationForClasses.add(clazz);
return this;
}
public EventBusBuilder ignoreGeneratedIndex(boolean ignoreGeneratedIndex) {
this.ignoreGeneratedIndex = ignoreGeneratedIndex;
return this;
}
public EventBusBuilder strictMethodVerification(boolean strictMethodVerification) {
this.strictMethodVerification = strictMethodVerification;
return this;
}
public EventBusBuilder addIndex(SubscriberInfoIndex index) {
if (subscriberInfoIndexes == null) {
subscriberInfoIndexes = new ArrayList<>();
}
subscriberInfoIndexes.add(index);
return this;
}
public EventBusBuilder logger(Logger logger) {
this.logger = logger;
return this;
}
Logger getLogger() {
if (logger != null) {
return logger;
} else {
// also check main looper to see if we have "good" Android classes (not Stubs etc.)
return Logger.AndroidLogger.isAndroidLogAvailable() && getAndroidMainLooperOrNull() != null
? new Logger.AndroidLogger("EventBus") :
new Logger.SystemOutLogger();
}
}
MainThreadSupport getMainThreadSupport() {
if (mainThreadSupport != null) {
return mainThreadSupport;
} else if (Logger.AndroidLogger.isAndroidLogAvailable()) {
Object looperOrNull = getAndroidMainLooperOrNull();
return looperOrNull == null ? null :
new MainThreadSupport.AndroidHandlerMainThreadSupport((Looper) looperOrNull);
} else {
return null;
}
}
Object getAndroidMainLooperOrNull() {
try {
return Looper.getMainLooper();
} catch (RuntimeException e) {
// Not really a functional Android (e.g. "Stub!" maven dependencies)
return null;
}
}
public EventBus installDefaultEventBus() {
synchronized (EventBus.class) {
if (EventBus.defaultInstance != null) {
throw new EventBusException("Default instance already exists." +
" It may be only set once before it's used the first time to ensure consistent behavior.");
}
EventBus.defaultInstance = build();
return EventBus.defaultInstance;
}
}
public EventBus build() {
return new EventBus(this);
}
}
這個(gè)類采用的是 Builder 建造者設(shè)計(jì)模式(簡(jiǎn)單來(lái)說(shuō)就是將本體對(duì)象和參數(shù)設(shè)置進(jìn)行分離)聊替,沒(méi)錯(cuò)這個(gè)類就是對(duì) EventBus 的參數(shù)配置
比如它的第一個(gè)字段就是用到了線程池:
上面這種寫(xiě)法很巧妙,默認(rèn)的線程池對(duì)象是靜態(tài)的春叫,而對(duì)象中的線程池則引用了默認(rèn)的泣港,這樣就能避免線程池的不必要的創(chuàng)建,如果對(duì)這種線程池的配置不滿意還可以使用下面這個(gè)方法進(jìn)行重新設(shè)置
最后在 build 方法創(chuàng)建 EventBus 對(duì)象呛每,將自己 EventBusBuilder.this 作為參數(shù)
看到這里你是否明白了坡氯,EventBus 不止是可以通過(guò) getDefault 方法獲取一個(gè) EventBus,也可以通過(guò) EventBusBuilder 來(lái)創(chuàng)建一個(gè) EventBus
但是如果我要把這個(gè) EventBus 作為全局 EventBus 呢颓遏?該怎么辦呢滞时?讓我們看看 installDefaultEventBus 方法源碼
你會(huì)發(fā)現(xiàn) installDefaultEventBus 最終還是會(huì)調(diào)用 build 方法滤灯,但是有一點(diǎn)需要注意的是曼玩,它把這個(gè)創(chuàng)建的 EventBus 對(duì)象設(shè)置為靜態(tài)的黍判,這樣我們就可以通過(guò) EventBus.getDefault() 獲取到這個(gè)單例的 EventBus 對(duì)象