控制反转

维基百科,自由的百科全书

跳转到: 导航, 搜索

控制反转(英文缩写为IoC),也叫做依赖注入Dependency Injection)。简称DI。是一个重要的面向对象编程的法则来削减计算机程序的耦合问题。

目录

[编辑] 起源

早在2004年,Martin Fowler就提出了“哪些方面的控制被反转了?”这个问题。他总结出是依赖对象的获得被反转了。基于这个结论,他为创造了控制反转一个更好的名字:依赖注入。许多非凡的应用(比HelloWorld.java更加优美,更加复杂)都是由两个或是更多的类通过彼此的合作来实现业务逻辑,这使得每个对象都需要,与其合作的对象(也就是它所依赖的对象)的引用。如果这个获取过程要靠自身实现,那么如你所见,这将导致代码高度耦合并且难以测试。

IoC 亦稱為 「依賴倒置原理」("Dependency Inversion Principle") (Martin 2002:127)。差不多所有框架都使用了「倒置注入(Fowler 2004)技巧,這可說是IoC原理的一項應用。SmallTalkC++, Java 或各種.NET 語言等面向對象程序語言的程序員已使用了這些原理。

控制反转是Spring框架的核心。

应用控制反转,对象在被创建的时候,由一个调控系统内所有对象的外界实体,将其所依赖的对象的引用,传递给它。也可以说,依赖被注入到对象中。所以,控制反转是,关于一个对象如何获取他所依赖的对象的引用,这个责任的反转

[编辑] Technical description

[编辑] 术语

Class X 依賴于 class Y 只在如下狀況中成立:

  • X 擁有 Y 的控制並且在 X 中使用 Y
  • X 是 Y 的派生物
  • X 依賴于 Z,而 Z 又依賴于 Y (transitivity)

X 依賴于 Y 並不表示 Y 也依賴于 X。但如果 X 和 Y 同時依賴于對方,這種依賴性被稱作 循環依賴:這時,X 無法和 Y 分開單獨使用,反之亦然。如果在一個面對對象程式中擁有太多的循環依賴,這可能表示這個程式是個欠佳的設計。

[编辑] Breaking up a dependency

File:Inversion of Control.svg
breaking a dependency with Inversion of Control (using UML diagram notation)

If an object x (of class X) calls methods of an object y (of class Y), then class X depends on Y. The dependency can now be inverted by introducing a third class, namely an interface class I that must contain all methods that x might call on y. Furthermore, Y must be changed such that it implements interface I. X and Y are now both dependent on interface I and class X no longer depends on class Y, presuming that x does not instantiate Y.

This elimination of the dependency of class X on Y by introducing an interface I is said to be an inversion of control (or a dependency inversion).

It must be noted that Y might depend on other classes. Before the transformation had been applied, X depended on Y and thus X depended indirectly on all classes that Y depends on. By applying Inversion of wangzeb control, all those indirect dependencies have been completely broken up too, not only the dependency from X on Y. The newly introduced interface I depends on nothing.

[编辑] IoC 的類型

Martin Fowler 將 IoC 分成三類。

  • Type 1 : 基於interface (interface injection)。Depending object 需要實作(implement) 特定 interface 以供框架注入所需物件。
  • Type 2 : 基於setter (setter injection)。Depending object 需要實現特定 setter 方法 (但不需要依賴特定interface),
  • Type 3 : 基於constructor (constructor injection)

框架,如Plexus 提出Type 4 IoC, 以field為基礎,惟此法到目前為止尚未被廣泛接納。[1]

各種框架不一定支援以上所有IoC類型。例如SpringFramework 支援Type 1, Type 2及 Type 3 IoC, 而 Plexus支援 Type 2, Type 3 和 Type 4。

[编辑] 控制反转应用实例

[编辑] C++

[编辑] Java

Programmers using the Java programming language have applied Inversion of Control in an Inversion of Control Container (Martin 2004). The software requests an object from the container and the container builds the object and its dependencies. The ATG Dynamo application server was one of the first environments to leverage this approach, while more recent examples of these containers include HiveMind, PicoContainer, Spring Framework (note that Spring is a complete enterprise platform, not just an IOC container), Apache Excalibur, Seasar, and DPML Metro.

[编辑] .NET

[编辑] 相关信息

[编辑] 参考文档

  1. ^  Robert Cecil Martin(2002).Agile Software Development: Principles, Patterns and Practices.Pearson Education.ISBN 0-13-597444-5 
  2. ^  Robert Cecil Martin.The Dependency Inversion Principle(PDF).於2005年11月15日查閱.
  3. ^  Martin Fowler(2004年).Inversion of Control Containers and the Dependency Injection Pattern.於2005年11月15日查閱.
  4. ^  Sony Mathew(2005年).Examining the Validity of Inversion of Control.於2005年11月16日查閱.
  5. ^  Ke Jin(2007年).Domain Specific Modeling (DSM) in IoC frameworks.於2007年11月13日查閱.

[编辑] 外部连接

[编辑] 參考文獻

  1. ^ [1]
个人工具