Dagger2 设计模式之构建者模式

Dagger 中的设计模式分析

Builder 模式

DaggerFatherComponent 对象的构建

Dagger 中对 DaggerFatherComponent 对象的构建可以拆分成三部分:

一 构建使用 @Inject 注解构造函数的对象

如:

this.provideCarProvider = FatherModule_ProvideCarFactory.create(builder.fatherModule);
二 构建 FatherComponent 所对应的 FatherModule.class 中提供的依赖对象
  1. 对于 FatherModule.class 中提供的所有依赖对象,Dagger 都会生成一个相应的工厂类,可以使用该该工厂类来获取依赖

如:

this.provideCarProvider = FatherModule_ProvideCarFactory.create(builder.fatherModule);
三 构建存在依赖或者继承关系的依赖对象
  1. FatherComponent 除了可以使用 FatherModule.class 中提供的依赖对象,还可以使用通过依赖或者继承其他Component的依赖。

    • 对于继承(SubComponent)
      Dagger 在构建 FatherComponent 时,同时会构建一个 sonComponentBuilderProvider 对象,该对象主要用来
      构建 sonComponent 对象。即在继承关系中,子 Component 是需要父 Component 来构建的。在构建 子 Component 过程中,父 Component 会将其
      所有依赖都传给 子 Component。

    • 对于依赖(Dependent)

    假设 SonComponent 依赖 FatherComponent,我们在构建 SonComponent 时需要传入一个 fatherComponent 对象,表明 SonComponent 中需要使用
    FatherComponent 的一些依赖。而在 FatherComponent 通过接口的形式暴露了 SonComponent 中可以使用的依赖。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值