2018-12-05-Component跟SubComponent的區(qū)別

參考地址 如下:

https://blog.csdn.net/soslinken/article/details/70231089
/**
* Component跟SubComponent的區(qū)別
* 1.方式一:如果全部都是使用Component 然后使用dependence屬性來依賴====子組件為主角 父組件暴露方法給子組件注入到容器
* 邏輯分析:
* 第一步:那就是父組件需要暴露 容器(Activity/Fragment)所需要的實例
* daggger里面會首先執(zhí)行以下代碼:===目標是先生成父組件的實例
* 父組件寫法如下:
* @Component(modules={AppModule.class})
* public interface AppComponent {
*//將AppModule中的SomeClassA1暴露出來,以便于其他依賴于AppComponent的Component調(diào)用
* SomeClassA1 someClassA1();
*//UserDaggerPresenterComponent依賴相當于繼承AppComponent 因為子組件 需要一個IBasePresenter 而子組件所對應
* 的module并沒有提供 如果需要父組件提供 那么父組件需要暴露一個Presenter
* IBasePresenter getPresenter();
*}
* Activity生成代碼如下:
* ApplicationModule applicationModule=new ApplicationModule(this);
* DaggerApplicationComponent.builder().applicationModule(applicationModule).build(); //生成父組件實例
* 第二步:生成子類的實例
* UserDaggerPresenterSubComponent userDaggerPresenterSubComponent=UserDaggerPresenterSubComponent.builder().appComponent(appComponent).studentModule(new StudentModule()).build().inject(this)
* 2. 方式二:如果是使用Component跟SubComponent的結(jié)合方式====這種方式父組件為主角 父組件添加 子組件做注入到容器里面
* 邏輯分析:
* 父組件添加一個返回子組件的實例
* UserDaggerPresenterSubComponent addSub(UserDaggerSubPresenterModule module);
* 子組件注入方法
* void inject(UserSubDaggerActivity activity);
*/

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容