Posted By : Murugan Andezuthu Dharmaratnam
Posted On : 6 December 2014
Keywords : Inversion Of Control Pattern, IOC, Inversion Of Control (IOC) , Inversion Of Control IOC , IOC Design Pattern

Inversion Of Control (IOC) Pattern

Definition


Inversion of Control ( 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.

From this definition its clear that something is different in comparision to procedural programing. So I would like to explain Inversion of Control with an example, Lets start with a regular object oriented program.

Output From The Sample Program

Problem Statment

When you write code you would have classes that depends on other classes for it to function, Lets look at an example DataAccessLayer Class that depends on MSSQL class which perform CRUD opertions on the Database.

The General Procedural Program Code

Below is the code generally you would write.

If we are to read data from Antoher Data Access Class MYSQL. Lets add another class to access data from MYSQL & lets implement the same methods as in MSSQL.

We have changed the code a bit to read the value from MSSQL or MYSQL Depending on DBType value you pass to the GetData method.

Modifying The Code To Use Interface

Lets modify the code by adding an interface IDBAccess ( Contract ) so that all Data Access classes MSSQL, MYSQL Inherit from the interface , One of the key principles of IoC is that the code should depend of abstraction. By depending up on Interfaces ( Abstraction ) We are decoupling the implementation. We can substitute different dependencies as long as they all satisfy the required interface.

Main Problems Associated WithThe Code

The main problem associated with the code is its tightly coupled. Which means in this case if you have another class called SQLite which implements the interface IDBAccess you would need to make modification to the DataAccessLayer class.

Now Lets Solve This Problem

Looking at the new code we have inverted the creation of DataAccess Object, Insted of creating it in DataAccess Layer we are creating it in the main program and passing the object using the controller.

Techniques For Implementing IOC ( Inversion Of Control)

In object-oriented programming, there are several techniques for implementation of inversion of control listed below.

  1. Strategy Design Pattern
  2. Template Method Design Pattern
  3. Contextualized Lookup
  4. Dependency Injection Pattern
  5. Service Locator Pattern
  6. Factory Pattern

Advantages Of Using Inversion Of Control Pattern

  1. Inversion of control is used to increase modularity of the program and make it extensible.
  2. There is a decoupling of the execution of a certain task from implementation.
  3. Every module can focus on what it was designed to do.
  4. Modules make no assumptions about what other systems do but rely on their contracts.
  5. Replacing modules has no side effect on other modules.

To Summarize

In traditional procedural programming, the flow of the business logic is determined by objects that are statically bound to one another. Where as in inversion of control, the flow depends on the object graph that is built up during program execution.

Murugan Andezuthu Dharmaratnam

Murugan is a technology leader with 14 years of hands-on experience in full life cycle systems design, development, implementation, and management. In addition to systems design and development, his core competencies include cloud design and architecture, interactive web applications and websites development, mobile computing, and Healthcare Information Technology. His experience is very diverse with a broad range of technologies within multiple industry settings including private, public, and non-profit. Coupled with his technology expertise, Murugan is a well-organized, result-oriented, and a highly analytical leader with proven track record in developing and implementing very complex systems.