Inversion of control
From Wikipedia, the free encyclopedia
| This article is in need of attention from an expert on the subject. WikiProject Computer science or the Computer science Portal may be able to help recruit one. (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. Although the caller will eventually get its answer, how and when is out of control of the caller. It is the callee who decides to answer how and when. The principle of Inversion of Control is therefore also known as the Hollywood Principle. 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.
- Every system does not make assumptions about what other systems do or should do.
- Replacing systems will have no side effect on other systems.
| This article may require cleanup to meet Wikipedia's quality standards. Please improve this article if you can. (May 2009) |
Contents |
[edit] Example
public class serverFacade { public Object respondToRequest(Object pRequest) { if(businessLayer.validateRequest(pRequest)) { DAO.getData(pRequest); return aspect.convertData(pRequest); } else { return null; } } }
This is a very crude example as examples can be. Important however is that in the serverFacade a lot of assumptions are made about the data returning from the DAO object. 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) { return DAO.getData(pRequest); } }
It does not make any assumption about what is happening next on both sides of the exchange. The caller and the callee should know, not the serverFacade. This is the next important effect of Inversion of Control. The serverFacade is doing solely the task for which it is designed, namely connecting systems.
[edit] Background
Inversion of Control is not a new term in computer science. According to Martin Fowler[1] the etymology of the word dates back to 1988. It is debatable if it is a new architectural principle. It is very comparable to the technique to create abstraction by layering. Inversion of Control however created hype as something new. It definitely raised the awareness for this architectural principle and gave architects a proper guideline to improve their work. Doing that it served its job well. In the world of Java, if not others, Inversion of Control has created its own namespace. Compared to 'abstraction by layering' it is a more specific term, which at the same time describes its purposes in a more concrete way. The term itself is not that abstract to say. It is to prefer when architectural terms would not be computer language specific.
It is still discussed 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 Robert C. Martin[4] 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.
[edit] Implementation techniques
Implementation techniques are influenced by the computer language used. Next is an overview of techniques how to implement IoC ordered by language. In Java there are six basic techniques to implement inversion of control. These are:
- 1. using a factory pattern,
- 2. using a service locator,
- 3. using a constructor injection,
- 4. using a setter injection,
- 5. using an interface injection, and
- 6. contextualized lookup.
In an original article of Martin Fowler[5] the first five different techniques are discussed. In a description about Inversion of Control types[6] 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. Inversion of Control is not restricted to these techniques, but serves these forementioned purposes.
[edit] Advantages and disadvantages of inversion of control
Inversion of Control has the same advantages and disadvantages as any type of abstraction. It roughly simplifies the building of specific tasks on one hand and makes the orchestration of applications more complex on the other. In the wikipedia article about abstraction layering the next quotes are used: 'A famous aphorism of David Wheeler goes: All problems in computer science can be solved by another level of indirection;[7] this is often deliberately mis-quoted with "abstraction" substituted for "indirection". Kevlin Henney's corollary to this is, "...except for the problem of too many layers of indirection."'
Exactly the same applies for Inversion of Control. Taking the code in the article of Robert C. Martin[4] as an example. There are in the end five classes needed to switch a lamp after pressing a button. In procedural programming this could have been implemented using one method. Inversion of Control has the advantage and power to uncouple implementations from each other, but the orchestration is more complex. Where the danger in procedural programming is to end with spaghetti code, the danger when using Inversion of Control is ending with macaroni code[citation needed].
[edit] References
- ^ http://martinfowler.com/bliki/InversionOfControl.html Inversion of Control on Martin Fowler's Bliki
- ^ http://www.codeproject.com/KB/aspnet/IOCDI.aspx Design pattern – Inversion of control and Dependency injection
- ^ http://www.devx.com/Java/Article/27583 Design Better Software with the Inversion of Control Pattern
- ^ a b http://www.objectmentor.com/resources/articles/dip.pdf The Dependency Inversion principle
- ^ http://www.martinfowler.com/articles/injection.html Inversion of Control Containers and the Dependency Injection Pattern
- ^ http://docs.codehaus.org/display/PICO/IoC+Types IoC Types
- ^ Tanenbaum, Andrew S: Structured Computer Organization, Prentice Hall, 1979.
[edit] See also
- Abstraction layer
- Asynchronous I/O
- Callback (computer science)
- Closure (computer science)
- Continuation
- Delegate (.NET)
- Dependency inversion principle
- 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.

