Inversion of control

From Wikipedia, the free encyclopedia
Jump to: navigation, search

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:

Contents

[edit] 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 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, IDAO da)
  {
    return da.getData(pRequest);
  }
}

Use an interface instead of a concrete implementation (an object)

[edit] 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 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.

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
  6. using a contextualized lookdown

In an original article by 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.

[edit] References

  1. ^ Inversion of Control on Martin Fowler's Bliki
  2. ^ Design pattern – Inversion of Control and Dependency injection by Shivprasad Koirala
  3. ^ Design Better Software with the Inversion of Control Pattern by Mani Malarvannan
  4. ^ The Dependency Inversion principle by Robert C. Martin
  5. ^ Inversion of Control Containers and the Dependency Injection Pattern by Martin Fowler
  6. ^ IoC Types

[edit] See also

Personal tools
Namespaces
Variants
Actions
Navigation
Interaction
Toolbox
Print/export
Languages