Inversion Of Control (IOC) Pattern
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
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.
- Strategy Design Pattern
- Template Method Design Pattern
- Contextualized Lookup
- Dependency Injection Pattern
- Service Locator Pattern
- Factory Pattern
Advantages Of Using Inversion Of Control Pattern
- Inversion of control is used to increase modularity of the program and make it extensible.
- There is a decoupling of the execution of a certain task from implementation.
- Every module can focus on what it was designed to do.
- Modules make no assumptions about what other systems do but rely on their contracts.
- Replacing modules has no side effect on other modules.
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.