控制反转
维基百科,自由的百科全书
控制反转(英文缩写为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 depends on class Y if any of the following applies:
- X has a Y and calls it
- X is a Y
- X depends on some class Z that depends on Y (transitivity)
X depends on Y does not imply Y depends on X. If both happen to be true this is called a cyclic dependency: X can't then be used without Y, and vice versa. The existence of a large number of cyclic dependencies in an object oriented program might be an indicator for suboptimal program design.
[编辑] 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-15造訪。
- ^ Martin Fowler (2004) - Inversion of Control Containers and the Dependency Injection Pattern - 於2005-11-15造訪。
- ^ Sony Mathew (2005) - Examining the Validity of Inversion of Control - 於2005-11-16造訪。
[编辑] 外部连接
- 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)zh:控制反转
页面分类: 需要专业人士关注的页面 | 面向对象编程 | 软件设计范例