How exactly does dependency injection reduce coupling?

前端 未结 4 465
醉梦人生
醉梦人生 2020-12-14 21:16

I\'ve done plenty of reading on Dependency Injection, but I have no idea, how does it actually reduce coupling?

The analogy I have of DI is that all components are r

4条回答
  •  天涯浪人
    2020-12-14 21:35

    In DI one component calls another component only via a well-defined interface and all components "glue" together via configuration and not code.
    As a result we can swap implementations used by an application by re-configuration alone.
    As an example in containers you can visualize dependency-injection as a reversed look-up (if you have used JNDI). Instead of having resource/module names hard-coded, you declare what is needed for a class (acting as a bean).The container is responsible for providing everything at run time.
    This term was coined by Martin Fowler and you can check his article on the concept DI

提交回复
热议问题