Inversion of control
From Wikipedia, the free encyclopedia
Inversion of Control, also known as IOC, is an object-oriented programming principle that can be used to reduce coupling inherent in computer programmes.
IOC is also known as the Dependency Injection (Martin 2002:127). The Dependency injection (Fowler 2004) technique is used in almost every framework and it is 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.
Contents |
[edit] Technical description
[edit] Terms and definitions
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.
Inversion of control relates to the way in which an object obtains references to its dependencies. This is often done by a lookup method. This approach has the disadvantage that an explicit dependence is introduced on a particular lookup mechanism, so the caller is dependent on a particular environment as well as on the object it obtains. With inversion of control the object is passed its dependencies through constructor arguments or after construction through setter methods. The approach is also called 'dependency injection' since the dependences of an object are 'injected' into it.
The advantage of inversion of control is that it decouples objects from specific lookup mechanisms and implementations of the objects it depends on. As a result, more flexibility is obtained for production applications as well as for testing. In particular, dependencies on a particular deployment environment can be removed from the code making it much easier to test functionality in a simple standalone environment. A consequence of this is that it becomes faster and easier to test so that in the end quality of the software is improved.
[edit] Inversion of Control in Practice
[edit] C++
[edit] ColdFusion
ColdSpring uses an Inversion-of-Control pattern and an Aspect-Oriented-Programming (AOP) framework for ColdFusion components (CFC).
[edit] Groovy on Grails
Groovy on Grails uses Spring Framework to initialize the application.
[edit] 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.
[edit] .NET
[edit] PHP4
[edit] PHP5
[edit] Python
[edit] Ruby
[edit] See also
[edit] References
- ^ 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). Retrieved on November 15, 2005.
- ^ Martin Fowler (2004). Inversion of Control Containers and the Dependency Injection Pattern. Retrieved on November 15, 2005.
- ^ Sony Mathew (2005). Examining the Validity of Inversion of Control. Retrieved on November 16, 2005.
[edit] External links
- Another description of IOC
- A list of "Open Source Inversion of Control Containers"
- 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)
- Explains inversion of control