Inversion of control
![]() | It has been suggested that this article be merged with Dependency inversion principle. (Discuss) Proposed since August 2010. |
![]() | This article needs attention from an expert in Computer science. Please add a reason or a talk parameter to this template to explain the issue with the article.(November 2008) |
![]() |
Inversion of Control, or IoC, is an abstract principle describing an aspect of some software architecture designs in which the flow of control of a system is inverted in comparison to procedural programming.
In traditional programming the flow is controlled by a central piece of code. Using Inversion of Control this central control as a design principle is left behind.
Inversion of Control as a design guideline serves the following purposes:
- There is a decoupling of the execution of a certain task from implementation.
- Every system can focus on what it is designed for.
- The systems make no assumptions about what other systems do or should do.
- Replacing systems will have no side effect on other systems.
Example
public class ServerFacade
{
public Object respondToRequest(Object pRequest)
{
if(businessLayer.validateRequest(pRequest))
{
DAO.getData(pRequest);
return Aspect.convertData(pRequest);
}
return null;
}
}
This basic outline gives an example of code following the IoC methodology. Important however is that in the serverFacade a lot of assumptions are made about the data returned by the data access object (DAO). Although all these assumptions might be valid at some time, they couple the implementation of the serverFacade to the DAO implementation. Designing the application in the thought of Inversion of Control would hand over the control completely to the DAO object. The code would then become
public class ServerFacade
{
public Object respondToRequest(Object pRequest, IDAO da)
{
return da.getData(pRequest);
}
}
Background
Inversion of Control is not a new term in computer science. According to Martin Fowler[1] the etymology of the phrase dates back to 1988.
It is still undecided if Inversion of Control is a design pattern, an architectural principle, or both. In an article by Shivprasad Koirala[2] Inversion of Control is presented together with Dependency Injection as a design pattern. It is a practical example of the first five techniques mentioned. He also shows that Dependency Injection might be a design pattern, whereas Inversion of Control is implemented using Dependency Injection. In an article by Mani Malarvannan[3] Inversion of Control is presented as a design pattern using contextualized lookup. The use of a service locator is considered using the same design pattern. In an article by Loek Bergman[4] it is presented as an architectural principle. Dependency injection being the design pattern as its most natural implementation.
In an article by Robert C. Martin[5] the dependency inversion principle and abstraction by layering come together. His reason to use the term inversion is in comparison with traditional software development methods. He describes the uncoupling of services by the abstraction of layers, when he is talking about dependency inversion. The principle is used to find out where system borders are in the design of the abstraction layers.
Inversion of Control is highly associated with dependency injection and the dependency inversion principle. Dependency injection is the main method to implement Inversion of Control.
Implementation techniques
Implementation techniques are influenced by the computer language used.
In Java there are six basic techniques to implement Inversion of Control. These are:
- using a factory pattern
- using a service locator pattern
- using a constructor injection
- using a setter injection
- using an interface injection
- using a contextualized lookup
Constructor, setter, and interface injection are all aspects of Dependency injection.
In an original article by Martin Fowler,[6] the first five different techniques are discussed. In a description about Inversion of Control types,[7] the last one is mentioned. Often the contextualized lookup will be accomplished using a service locator. More important than the applied technique however is the optimization of the purposes.
References
- ^ Inversion of Control on Martin Fowler's Bliki
- ^ Design pattern – Inversion of Control and Dependency injection by Shivprasad Koirala
- ^ Design Better Software with the Inversion of Control Pattern by Mani Malarvannan
- ^ [1] by Loek Bergman
- ^ The Dependency Inversion principle by Robert C. Martin
- ^ Inversion of Control Containers and the Dependency Injection Pattern by Martin Fowler
- ^ IoC Types
See also
- Abstraction layer
- Asynchronous I/O
- Callback (computer science)
- Closure (computer science)
- Continuation
- Delegate (.NET)
- Dependency inversion principle
- Flow-based programming
- Hollywood Principle ("don't call us, we'll call you")
- Implicit invocation
- Interrupt handler
- Monad (functional programming)
- Observer pattern
- Publish/subscribe
- Service locator pattern
- Signal (computing)
- Software framework
- Strategy pattern
- User exit
- Visitor pattern
- XSLT is a data-driven scripting language, meaning that the input data controls which templates (methods) are executed and in what order. Templates automatically return control to the input data upon completion or by using the <xsl:apply-templates /> command.