Dependency Injection

本文阐述了依赖倒置原则(Dependency Inversion Principle),强调高级模块不应依赖于低级模块,而两者都应依赖于抽象。文章解释了该原则的优势:支持自顶向下的设计与开发流程、组件间的松耦合及易于单元测试。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

     Dependency inversion principle : High level modules should not depend upon low level modules. Both should depend upon abstractions. Abstractions should not depend upon details. Details should depend upon abstractions.

 

     Advantage:

 

     a. You do not have to develop the lower level modules before developing the higher level modules. Thus a top-down approach is possible in both design and development.

 

     b. Objects in the lower layers are injected into the higher level objects. The result is that components are less tightly coupled, and there is a high degree of separation of concerns.

 

     c. The individual components are more easily testable and the higher level objects can be tested with mock objects in place of lower level services.

 

     Separation of Conserns (SOC) is the motive of creation of DI(IOC) and AOP.

 

     Using DI, we decouple two modules. But actually, we haven't eliminated the connections between two modules, instead we just move the connection to another place.

 

     The transfer of this connection is a type of SOC, and it provide the possibility of AOP.

 

      DI: The dependencies for a Dependency Injector

 

      The basic idea of the Dependency Injection is to have a separate object, an assembler, that populates a field in the lister class with an appropriate implementation for the finder interface.

 

       There are three main styles of dependency injection, they are Constructor Injection, Setter Injection, and Interface Injection.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值