IOC-Inversion of Control, Dependency Injection

-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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值