控制反转
此条目需要精通或熟悉相关主题的编者参与及协助编辑。 |
控制反转(英文缩写为IoC)是一个重要的面向对象编程的法则来削减计算机程序的耦合问题。
IoC 亦称为 “依赖倒置原理”("Dependency Inversion Principle") (Martin 2002:127)。差不多所有框架都使用了“倒置注入(Fowler 2004)技巧,这可说是IoC原理的一项应用。SmallTalk,C++, 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
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++
- PocoCapsule IoC and DSM framework GPL开源的,支持完全非侵入C++的控制反转(IoC)及领域特定建模(DSM)容器
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
相关信息
参考文档
- ^ Robert Cecil Martin. Agile Software Development: Principles, Patterns and Practices. Pearson Education. 2002. ISBN 0-13-597444-5.
- ^ Robert Cecil Martin. The Dependency Inversion Principle (PDF). [2005-11-15].
- ^ Martin Fowler. Inversion of Control Containers and the Dependency Injection Pattern. 2004 [2005-11-15].
- ^ Sony Mathew. Examining the Validity of Inversion of Control. 2005 [2005-11-16].
- ^ Ke Jin. Domain Specific Modeling (DSM) in IoC frameworks. 2007 [2007-11-13].
外部连接
- Another description of IOC
- A list of "Open Source Inversion of Control Containers"
- A simple demo of Inversion of Control (using Spring framework)
- Inversion of Control Containers and the Dependency Injection pattern by Martin Fowler
- Needle, a dependency injection (inversion of control) container for Ruby
- Drip IoC, a dependency injection (inversion of control) container for PHP4
- PyContainer, a dependency injection (inversion of control) container for Python
- Qt Ioc Container, a dependency injection (inversion of control) container for C++
- Introducing Castle, explains the problems that Inversion of control containers try to solve (using Castle Project)
- PocoCapsule IoC and DSM framework for C++
- Domain Specific Modeling (DSM) in IoC frameworks