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.


前言 对于大部分小菜来说,当听到大牛们高谈DIP、IoC、DI以及IoC容器等名词时,有没有瞬间石化的感觉?其实,这些“高大上”的名词,理解起来也并不是那么的难,关键在于入门。只要我们入门了,然后循序...
  • erpenggg
  • erpenggg
  • 2017年05月16日 14:29
  • 244


  • dd864140130
  • dd864140130
  • 2016年05月04日 11:10
  • 3130

谈谈我对Spring IOC与DI的理解

IOC是一种叫做“控制反转”的设计思想。1、较浅的层次——从名字上解析 “控制”就是指对 对象的创建、维护、销毁等生命周期的控制,这个过程一般是由我们的程序去主动控制的,如使用new关键字去创建一个...
  • zhangliangzi
  • zhangliangzi
  • 2016年06月01日 00:23
  • 9998


DIP(dependency inversion principle)依赖倒转 IOC(inversion of control)反转控制 DI(dependency injection)依赖注入...
  • qq_27803495
  • qq_27803495
  • 2015年11月03日 16:42
  • 32


背景 这个文章主要介绍DIP、Ioc、Ioc容器和DI以及如何更好的理解这些概念,对这些概念有了理解之后我会更好的涉及组织我们的程序,更好的使用Spring Ioc容器。 依赖倒置原则(DIP):...
  • fly_zxy
  • fly_zxy
  • 2018年01月13日 19:02
  • 43


IoC(Inversion of Control): 控制反转(不是技术,而是思想)其思想是反转资源获取的方向。...
  • suzunshou
  • suzunshou
  • 2015年07月30日 14:49
  • 2355

ioc 与 DI | 区别简介

看到一个对这个概念很好诠释的帖子,特转发过来供大家一起学习 转载地址http://www.iteye.com/topic/692793    在android butterknife...
  • java_goodstudy
  • java_goodstudy
  • 2016年10月02日 00:26
  • 1149

浅谈对Spring IOC以及DI的理解

浅谈对Spring IOC以及DI的理解
  • luoyepiaoxue2014
  • luoyepiaoxue2014
  • 2017年05月17日 19:40
  • 882


IOC、DI、AOP是什么,为什么使用,怎么用 Spring中加载配置文件的两种方法,可以加载多个配置文件吗?单例模式、和工厂模式定义 依赖注入的三种方法 1.IOC IOC为控制反转...
  • maidaogo
  • maidaogo
  • 2014年11月25日 09:58
  • 890


IoC   IoC: Inversion of Control,控制反转, 控制权从应用程序转移到框架(如IoC容器),是框架共有特性   1、为什么需要IoC容器 1.1、应用...
  • cws1214
  • cws1214
  • 2016年08月04日 13:29
  • 2773
您举报文章:Understanding the DIP, DI, IoC theory