控制反转
维基百科,自由的百科全书
控制反转(英文缩写为IOC)是一个重要的面向对象编程的法则来削减计算机程序的耦合问题。
IOC is also known as the Dependency Inversion Principle (Martin 2002:127). The Dependency injection (Fowler 2004) techinque is used in almost every framework and it's a simple example of the IoC principle applied. It has been applied by programmers using object-oriented programming languages such as SmallTalk, C++, Java or any .NET language.
目录 |
[编辑] 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.
[编辑] 控制反转应用实例
[编辑] 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
[编辑] 相关信息
- Software package metrics
- Circular dependency
- Dependency injection
- Implicit invocation
[编辑] 参考文档
- ^ Robert Cecil Martin (2002). Agile Software Development: Principles, Patterns and Practices,Pearson Education. ISBN 0-13-597444-5.
- ^ Robert Cecil Martin - The Dependency Inversion Principle (PDF) - 於2005-11-15zh-tw:造;zh-cn:采訪。
- ^ Martin Fowler (2004) - Inversion of Control Containers and the Dependency Injection Pattern - 於2005-11-15zh-tw:造;zh-cn:采訪。
- ^ Sony Mathew (2005) - Examining the Validity of Inversion of Control - 於2005-11-16zh-tw:造;zh-cn:采訪。
[编辑] 外部连接
- 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)
页面分类: 需要专业人士关注的页面 | 面向对象编程 | 软件设计范例