雞湯:感到迷茫是因?yàn)槟銢](méi)有給自己做好人生規(guī)劃
接上一章的內(nèi)容,如果還沒(méi)看過(guò)的朋友呜象,
請(qǐng)點(diǎn)
從零開(kāi)始系列第0章
從零開(kāi)始系列第1章
從零開(kāi)始系列第2章
從零開(kāi)始系列完結(jié)章
本章內(nèi)容
Dagger2的引入
Dagger2的引入
Dagger2是一個(gè)依賴注入框架,那么dagger2能起什么作用呢右犹?
簡(jiǎn)單點(diǎn)講就是
dagger2會(huì)幫你維護(hù)好一些對(duì)象丹擎,你需要什么對(duì)象,可以直接問(wèn)dagger2要镰踏,
當(dāng)然前提是你已經(jīng)按照dagger2的要求寫(xiě)了好這些依賴函筋。
比如:像下圖這種情況,你需要得到result奠伪,那你不得不像代碼那樣跌帐,先得到c對(duì)象,得到c對(duì)象绊率,就需要得到b對(duì)象谨敛,需要得到a對(duì)象。
public class Dagger2Test{
private classA a;
private classB b;
private classC c;
private String result;
public void onCreate() {
super.onCreate();
b=new classB(a);
c=new classC(b);
result=c.get(0);
}
}
而使用了dagger2的話即舌,可以寫(xiě)成這樣
public class Dagger2Test{
//一個(gè)直接得到c對(duì)象佣盒,然后在oncreate中一行代碼搞定
@Inject
classC c;
private TestComponent mComponent;
private String result;
public void onCreate() {
super.onCreate();
mComponent.inject(this);
result=c.get(0);
}
}
Dagger2的引入
在config.gradle(不知道config.gradle如何來(lái)的,請(qǐng)看系列文章第0章)設(shè)置依賴
//版本號(hào)
daggerVersion = '2.2'
javaxAnnotationVersion = '1.0'
//遠(yuǎn)程依賴
dependencies = [
daggerCompiler: "com.google.dagger:dagger-compiler:${daggerVersion}",
dagger: "com.google.dagger:dagger:${daggerVersion}",
javaxAnnotation: "javax.annotation:jsr250-api:${javaxAnnotationVersion}"
]
build.gradle下
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.1.0'
//加入dagger2的apt插件
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4'
}
}
app/build.gradle下
文件頂部
//引入dagger2 的apt插件
apply plugin: 'com.neenbedankt.android-apt'
//添加依賴
apt dependency['daggerCompiler']
compile dependency['dagger']
compile dependency['javaxAnnotation']
做完上述的工作顽聂,dagger2就已經(jīng)引入成功了肥惭。
在項(xiàng)目中使用
首先回顧一下在開(kāi)源項(xiàng)目2中的代碼,在事件的起點(diǎn)view紊搪,也就是TodayGankFragment中蜜葱,有段代碼是這樣的!
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
RxFlux rxFlux = RxFlux.init(MyApplication.getApplication());
Dispatcher dispatcher = rxFlux.getDispatcher();
SubscriptionManager manager = rxFlux.getSubscriptionManager();
store = new TodayGankStore(dispatcher);
dispatcher.subscribeRxStore(store);
dispatcher.subscribeRxView(this);
creator = new TodayGankActionCreator(dispatcher, manager);
//view從對(duì)應(yīng)的Creator請(qǐng)求數(shù)據(jù)
creator.getTodayGank();
}
上述代碼中最重要的耀石,不可或缺的牵囤,不能再縮減的代碼有這么幾句
//兩個(gè)監(jiān)聽(tīng)注冊(cè)
dispatcher.subscribeRxStore(store);
dispatcher.subscribeRxView(this);
//view從對(duì)應(yīng)的Creator請(qǐng)求數(shù)據(jù)
creator.getTodayGank();
兩個(gè)subscribe監(jiān)聽(tīng)注冊(cè),以及數(shù)據(jù)請(qǐng)求滞伟,數(shù)據(jù)請(qǐng)求是事件的起點(diǎn)揭鳞,subscribe是為了之后的數(shù)據(jù)流轉(zhuǎn)所必須的。
除了這三行代碼梆奈,其他的變量和對(duì)象的聲明都可以想辦法放到別處野崇,使得這段代碼變得簡(jiǎn)潔。
這個(gè)時(shí)候Daggar2就可以排上用場(chǎng)了亩钟。
首先來(lái)整理下依賴乓梨,我們的需要的依賴對(duì)象或者變量有
RxFlux
dispatcher
manager
store
creator
一個(gè)有5個(gè)依賴,再根據(jù)業(yè)務(wù)邏輯將5個(gè)依賴劃分一下清酥,
RxFlux扶镀,dispatcher,manager這三個(gè)依賴屬于全局型依賴焰轻,就是說(shuō)很多地方都需要用的到的依賴臭觉。
而store和creator只是局部型依賴,所以單獨(dú)處理。
全局依賴
定義一個(gè)AppComponent類蝠筑,具體寫(xiě)法不在累述忆肾。
@Singleton
@Component(modules = {AppModule.class})
public interface AppComponent {
//
Dispatcher getDispatcher();
SubscriptionManager getSubscriptManager();
}
AppComponent類提供Dispatcher和SubscriptionManager兩個(gè)對(duì)象,具體的實(shí)現(xiàn)在AppModule類中
@Module
public class AppModule {
private final RxFlux mRxFlux;
public AppModule(MyApplication application) {
mApplication = application;
//初始化RxFlux
mRxFlux = RxFlux.init(application);
}
@Provides
@Singleton
Dispatcher provideDispatcher() {
return mRxFlux.getDispatcher();
}
@Provides
@Singleton
SubscriptionManager provideSubscriptManager() {
return mRxFlux.getSubscriptionManager();
}
}
構(gòu)造函數(shù)里初始化RxFlux菱肖,然后借用RxFlux,初始化Dispatcher和SubscriptionManager對(duì)象旭从。
調(diào)用:
在程序入口MyApplication中使用
public class MyApplication extends Application {
private static AppComponent mAppComponent;
@Override
public void onCreate() {
super.onCreate();
initInjector();
}
private void initInjector() {
mAppComponent = DaggerAppComponent.builder()
.appModule(new AppModule(this))
.build();
}
public static AppComponent getAppComponent() {
return mAppComponent;
}
}
在MyApplication中initInjector()方法執(zhí)行之后稳强,RxFlux,Dispatcher和SubscriptionManager三個(gè)依賴對(duì)象就初始化成功了和悦。
接下來(lái)還需要store和creator這兩個(gè)依賴退疫,
定義一個(gè)TodayGankFragmentComponent類
@PerActivity
@Component(dependencies = {AppComponent.class})
public interface TodayGankFragmentComponent {
void inject(TodayGankFragment todayGankFragment);
}
PerActivity類,聲明scope鸽素,不然會(huì)報(bào)錯(cuò)
@Scope
@Retention(RUNTIME)
public @interface PerActivity {}
在TodayGankFragment中褒繁,使用TodayGankFragmentComponent,注意appComponent的時(shí)候要把你之前定義好的appComponent對(duì)象傳進(jìn)去,
private void initInjector() {
TodayGankFragmentComponent mComponent= DaggerTodayGankFragmentComponent.builder()
.appComponent(MyApplication.getAppComponent())
.build();
mComponent.inject(this);
}
這個(gè)時(shí)候你就可以很簡(jiǎn)單的聲明對(duì)象了馍忽,
比如TodayGankStore類中棒坏,先在構(gòu)造方法上加上@Inject注解,表示我能接受一個(gè)dispatcher參數(shù)遭笋,并提供一個(gè)TodayGankStore對(duì)象坝冕,而dispatcher這個(gè)對(duì)象在最開(kāi)始的appComponent中就已經(jīng)初始化完成,
@Inject
public TodayGankStore(Dispatcher dispatcher) {
super(dispatcher);
}
在需求類中瓦呼,即TodayGankFragment中喂窟,聲明TodayGankStore對(duì)象store,這個(gè)store就是TodayGankStore所提供的央串,這中間的過(guò)程Dagger2會(huì)幫你處理磨澡。
@Inject TodayGankStore store;
單獨(dú)解釋一下@Inject這個(gè)注解的作用
1.標(biāo)記在構(gòu)造方法上,表示對(duì)象提供者
2.標(biāo)記在目標(biāo)類中质和,表示實(shí)例對(duì)象稳摄。
加入了dagger2之后,代碼變化如下侦另,
@Inject TodayGankStore mStore;
@Inject TodayGankActionCreator mActionCreator;
@Inject Dispatcher mDispatcher;
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
//RxFlux rxFlux = RxFlux.init(MyApplication.getApplication());
//Dispatcher dispatcher = rxFlux.getDispatcher();
//SubscriptionManager manager = rxFlux.getSubscriptionManager();
//store = new TodayGankStore(dispatcher);
//dispatcher.subscribeRxStore(store);
//dispatcher.subscribeRxView(this);
//creator = new TodayGankActionCreator(dispatcher, manager);
//view從對(duì)應(yīng)的Creator請(qǐng)求數(shù)據(jù)
//creator.getTodayGank();
mDispatcher.subscribeRxStore(mStore);
mDispatcher.subscribeRxView(this);
mActionCreator.getTodayGank();
}
注釋的代碼是沒(méi)加Dagger2之前的秩命,可以明顯的看出依賴關(guān)系簡(jiǎn)單了很多,代碼邏輯也清晰了許多褒傅。
Dagger2還是能夠給代碼帶來(lái)挺大的變化的弃锐。
本來(lái)還想再寫(xiě)一點(diǎn)基類封裝的內(nèi)容,不過(guò)由于部分代碼和業(yè)務(wù)邏輯關(guān)系比較緊殿托,要寫(xiě)的話霹菊,內(nèi)容還有不少。
所以留到下次再講吧!
本人也只是Android開(kāi)發(fā)路上一只稍大一點(diǎn)的菜鳥(niǎo)旋廷,如果各位讀者中發(fā)現(xiàn)文章中有誤之處鸠按,請(qǐng)幫忙指出,你的批評(píng)和鼓勵(lì)都是我前進(jìn)的動(dòng)力饶碘。
寫(xiě)在文末:如果讀者朋友有什么問(wèn)題或者意見(jiàn)可以在評(píng)論里指出.
代碼地址為https://github.com/niknowzcd/FluxDemo3