跳转到内容

控制反转

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

这是本页的一个历史版本,由75.37.202.187留言2007年11月23日 (五) 08:21 参考文档编辑。这可能和当前版本存在着巨大的差异。

控制反转(英文缩写为IoC)是一个重要的面向对象编程的法则来削减计算机程序的耦合问题。

IoC 亦称为 “依赖倒置原理”("Dependency Inversion Principle") (Martin 2002:127)。差不多所有框架都使用了“倒置注入(Fowler 2004)技巧,这可说是IoC原理的一项应用。SmallTalkC++, Java 或各种.NET 语言等面向对象程序语言的程序员已使用了这些原理。

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 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. Agile Software Development: Principles, Patterns and Practices. Pearson Education. 2002. ISBN 0-13-597444-5. 
  2. ^ Robert Cecil Martin. The Dependency Inversion Principle (PDF). [2005-11-15]. 
  3. ^ Martin Fowler. Inversion of Control Containers and the Dependency Injection Pattern. 2004 [2005-11-15]. 
  4. ^ Sony Mathew. Examining the Validity of Inversion of Control. 2005 [2005-11-16]. 
  5. ^ Ke Jin. Domain Specific Modeling (DSM) in IoC frameworks. 2007 [2007-11-13]. 

外部连接

  1. ^ [1]