IOC-Inversion of Control, Dependency Injection

原创 2012年03月28日 16:33:23

-What is?

Inversion of Control: 直观的讲,就是反转控制. 这是什么意思呢?什么是控制?反转了谁的控制? 为什么要反转呢?下面来一一说一下.

   1) Control: 这里指的是程序控制的流程。比如:

      classA::do(int a)

{

    //do something

   if (a==1)

 {

  B.do()

}

else

{

C.do()

}

}

  

   这是传统的程序的控制流程。

 2)   那么如何反转这个流程呢?

    答案是用面向对象的方法,对接口编程。上面的控制流程变为:

BandC obj = new B

   class A::do(interface BandC obj)

{

BandC.do()

}

把控制流程从A::do内部拿到了外面,根据初始的参数BandC的类型来控制A.do()的行为。

在这里你看见了什么?面向接口编程, 没什么特殊的。靠,非得弄个这么个难懂的概念。

当然,实现这种控制方式由很多种方式:

In Java there are six basic techniques to implementInversion of Control. These are:

  1. using a factory pattern
  2. using a service locator pattern
  3. using a constructor injection
  4. using a setter injection
  5. using an interface injection
  6. using a contextualized lookup

 

Dependency Injection: 

  从上面我们可以看出,这个概念是IoC的一个特例,是在实现IoC的一种方法。这个就是一个object A在依赖多个其它object的时候, 不用object A来管理和创建这些其它的objects. 而是向其它的Container来要这些依赖的objects.这里object A所依赖的其它的具体的object的时候,我们要插入一个interface, 使得object A依赖于接口,而不是具体的object.

    用DI真正的原因: 在我们写code的过程中, 各个object都有可能会用到其它的object,所以要在内部创建object. 这样就会很多的new操作。会造成一个object要改变行为不容易,并且也不遵守OO的原则:一个class尽量要完成最简单的功能,并且能够做的更 好。所以要把创建object的工作在class的外部来完成, 而在内部调用这些创建的reference. 这是Dpendency injection.

   DI有以下几种方式:

  1. using a constructor injection
  2. using a setter injection
  3. Interface injection

看一看Wiki上如何定义的:

  Dependencyinjection (DI) is an approach to the testingof computer programs, the purpose of which is both to improve the testabilityof a large software system and to simplify the deployment of softwarecomponents within that system. DI is an example of a design pattern in object-oriented computer programming.

Dependency Injection involves at least three elements:

  • a dependent consumer,
  • a declaration of a component's dependencies, defined as interface contracts,
  • an injector (sometimes referred to as a provider or container) that creates instances of classes that implement a given dependency interface on request.

- IoC container:

  如果依赖关系简单的时候,我们可以自己写一写代码来维护。比如用factory pattern。当时当关系非常夫在的时候,并且有多重依赖的时候,就非常困难了。这个时候我们就可以借助于container来实现这个功能了。这个就是产生instances 的地方。每次一个对象要依赖于其它的对象是,向他要就可以了。所以产生了许多的framework,类似Spring, Castle 的Windsor,ObejctBuilder等等。他们可以支持多种配置方式,比如我们可以通过XML来配置依赖关系等。

- 好处和坏处:

Pros:

  • Your code gets decoupled so you can easily exchange implementations of an interface with alternative implementations
  • It is a strong motivator for coding against interfaces instead of implementations
  • It's very easy to write unit tests for your code because it depends on nothing else than the objects it accepts in its constructor/setters and you can easily initialize them with the right objects in isolation.

Cons:

  • IoC not only inverts the control flow in your program, it also clouds it considerably. This means you can no longer just read your code and jump from one place to another because the connection between your code is not in the code anymore. Instead it is in XML configuration files or annotations and the in the code of your IoC container that interprets these metadata.
  • There arises a new class of bugs where you get your XML config or your annotations wrong and you can spend a lot of time finding out why your IoC container injects a null reference into one of your objects under certain conditions.

Personally I see the strong points of IoC and I really like them but Itend to avoid IoC whenever possible because it turns your software into acollection of classes that no longer constitute a "real" program butjust something that needs to be put together by XML configuration or annotationmetadata and would fall (and falls) apart without it.

 

 

reference:

http://en.wikipedia.org/wiki/Dependency_injection

http://martinfowler.com/articles/injection.html

http://en.wikipedia.org/wiki/Inversion_of_control

http://stackoverflow.com/tags/inversion-of-control/info

http://dotnetslackers.com/articles/designpatterns/InversionOfControlAndDependencyInjectionWithCastleWindsorContainerPart1.aspx

IOC(DI)- Inversion of Control(Dependency Injection)

IOC-Inversion of Control DI-Dependency Injection 这两个其实是一个概念 可能叫做依赖注入更容易理解。它有什么好处呢? 通过写配置文件,容器会帮助...

A curry of Dependency Inversion Principle (DIP), Inversion of Control (IoC), Dependency Injection (D

A curry of Dependency Inversion Principle (DIP), Inversion of Control (IoC), Dependency Injection (D...

Inversion of Control and Dependency Injection with Castle Windsor Container

http://stackoverflow.com/questions/3058/what-is-inversion-of-control The Inversion of Control (...

Inversion of Control and Dependency Injection with Castle Windsor Container - Part I

Inversion of Control and Dependency Injection with Castle Windsor Container - Part I Published: 24 ...

依赖注入 控制反转 服务定位器 模式 Dependency Injection Inversion of Control Service Locator Patterns | 超级经典

依赖注入 控制反转 服务定位器 模式  Dependency Injection Inversion of Control IoC Service Locator Patterns 撰文/M...

控制反转(Inversion of Control)与依赖注入(Dependency Injection)

控制反转与依赖注入、依赖查找的关系

控制反转(Inversion of Control,IoC)简介

控制反转(Inversion of Control,IoC)简介

Spring的IOC(Inversion of Control)(控制反转)

Spring的IOC(Inversion of Control)(控制反转)

Ioc模式(Inversion of Control,即反转模式)的理解

分离关注( Separation of Concerns : SOC)是Ioc模式和AOP产生最原始动力,通过功能分解可得到关注点,这些关注可以是 组件Components, 方面Aspects或服务...

spring ioc(Inversion of Control) 和 Aop 总结

Sping IOC(DI) AOP
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:IOC-Inversion of Control, Dependency Injection
举报原因:
原因补充:

(最多只允许输入30个字)