控制反转
此條目需要精通或熟悉相关主题的编者参与及协助编辑。 |
控制反转(英文缩写为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