inversion of control design pattern Edit

Murugan Andezuthu Dharmaratnam | 04 September 2020 | 127


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 it's clear that something is different in comparison to procedural programming. So I would like to explain Inversion of Control with an example, Lets start with a regular object-oriented 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 Another Data Access Class MYSQL. Let's add another class to access data from MYSQL & let's implement the same methods as in MSSQL.

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

Modifying The Code To Use Interface

Let us 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 on abstraction. By depending upon 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 it's tightly coupled. This means in this case if you have another class called SQLite which implements the interface IDBAccess you would need to make a modification to the DataAccessLayer class.

Now Lets Solve This Problem

Looking at the new code we have inverted the creation of DataAccess Object, Instead of creating it in the 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 the modularity of the program and make it extensible.
    2. There is a decoupling of the execution of a certain task from implementation.
    3. Modules make no assumptions about what other systems do but rely on their contracts.
    4. Every module can focus on what it was designed to do.
    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.