Understanding the DIP, DI, IoC theory

转载 2012年03月30日 13:42:30
  1. Dependency Inversion Principle. Means you should always only rely on interfaces and not on their implementations. If your class depends on any other class, that's bad, because it depends on that second class' details. If your class depends on interface, that's absolutely OK since this kind of dependence only means that your class needs something abstract that can do something specific and you don't really care the way it does it.

Since P in "DIP" stands for"Principle", I should probably define it this way: DependencyInversion Principle is a principle that requires all your code's entities todepend only on details they really need.

By "details they really need" I mean interfacesfor the simplest case. I also used the word "entities" to emphasizethat DIP is also applicable to procedures and whatever else, not only toclasses.

  1. Dependency Injection. It's only applicable to DI-enabled entities. DI-enabled entity is an entity which is "open" for configuring its behavior without changing its internals. There are 2 basic kinds of injection (when talking about classes):
    • Constructor Injection - is when you pass all the required "abstract details" to the object just by the moment it's about to be constructed.
    • Setter Injection - is when you "clarify" the required aspects after the object has already been created.

So, the definition is probably like following: DependencyInjection is a process of passing the "abstract details" to theentity that really needs these details.

By "really needs these details" I mean interfacesfor the simplest case. The word "entities" is, as always, used toemphasize that DI is also applicable to procedures and whatever else.

  1. Inversion of Control. It's often defined as "difference between libraries and frameworks", as "writing programs the either way you did in procedural programming" and so forth. That the most confusing thing for me. I believe that main idea here is just about initiating any actions. Either you do something "whenever you want" (Procedural way), or you "wait" until someone asks you (IoC way).

My Definition is: IoC is a property of your program'sexecution flow, when you don't do anything until they ask you to do it.

It sounds exactly as "Hollywood Principle", butI believe that "Hollywood Principle" and IoC are both absolutely thesame idea.


写这篇文章的原因是这两天在编写关于 Dagger2 主题的博文时,花了大量的精力来解释依赖注入这个概念。后来想一下,这些在面向对象开发过程中与依赖相关的诸多术语和概念实际情况下非常的抽象,因此独立成文...
  • briblue
  • briblue
  • 2017年07月13日 22:04
  • 6286


---- 概念 ---- ◆1.依赖倒置原则(DIP,Dependency Inverse Principle)     强调系统的高层组件不应当依赖于底层组件;并且不论是高层组件还是底层组件...
  • qqlinke
  • qqlinke
  • 2011年04月05日 23:45
  • 1072

Atitit。如何实现dip, di ,ioc  ,Service Locator的区别于联系

Atitit。如何实现dip, di ,ioc  ,Service Locator的区别于联系   1. Dip原则又来自于松耦合思想方向1 2. 要实现dip原则...
  • attilax
  • attilax
  • 2016年09月16日 16:55
  • 315




写这篇文章的原因是这两天在编写关于 Dagger2 主题的博文时,花了大量的精力来解释依赖注入这个概念。后来想一下,这些在面向对象开发过程中与依赖相关的诸多术语和概念实际情况下非常的抽象,因此独立成文...


DIP,IoC与DI概念解析 依赖倒置 DIP(Dependency Inversion Principle) DIP的两大原则: 1、高层模块不应该依赖于低层模块,二者都应该依赖于抽...
  • judyge
  • judyge
  • 2015年05月07日 08:00
  • 353






http://www.cnblogs.com/liuhaorain/p/3747470.html 深入理解DIP、IoC、DI以及IoC容器 摘要 面向对象设计(OOD)有助于我们开发出...


摘要 面向对象设计(OOD)有助于我们开发出高性能、易扩展以及易复用的程序。其中,OOD有一个重要的思想那就是依赖倒置原则(DIP),并由此引申出IoC、DI以及Ioc容器等概念。通过本文我们将一起...
  • aqsswe
  • aqsswe
  • 2016年03月21日 18:46
  • 306
您举报文章:Understanding the DIP, DI, IoC theory