Dagger2 设计模式之构建者模式

本文深入探讨Dagger框架中依赖注入的设计模式,包括Builder模式在DaggerFatherComponent构建中的应用,详细讲解了依赖对象的创建过程,以及如何通过依赖和继承实现组件间的协作。
部署运行你感兴趣的模型镜像

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 中可以使用的依赖。

您可能感兴趣的与本文相关的镜像

Llama Factory

Llama Factory

模型微调
LLama-Factory

LLaMA Factory 是一个简单易用且高效的大型语言模型(Large Language Model)训练与微调平台。通过 LLaMA Factory,可以在无需编写任何代码的前提下,在本地完成上百种预训练模型的微调

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值