Wednesday, May 11, 2016

Inversion of control, Dependency Inversion, Dependency Injection

Inversion of control: It is a principle in which custom written portion of computer program receives the flow of control from a generic framework.
Custom written portion of computer program:    Whatever the code we write using Spring framework is an example of custom written portion of computer program.
Receives the flow of control from a generic framework: Our custom written computer program is governed by Spring framework.  Example:   object creation, scope, defining their dependencies and some other main activities are defined by spring framework.
It means that reusable code and problem specific code developed independently even though they operate together in application.
IOC is based on one principle ‘Do not call us, we will call you back’.  Example: Spring framework defines when our custom classes will be created and injected.  There are many such examples available with Spring framework.
Purpose of IOC: Increasing the modularity, reducing coupling and increasing the extensibility of the application program.

Service locator pattern, Dependency Injection, template method design pattern are the examples of Inversion of Control principle.
Dependency Inversion and Dependency Injection:
Dependency Inversion is a principal while dependency Injection is a design pattern that follows the dependency inversion principle.
Dependency Inversion principle states that:
            a: High level modules should not depend on low level modules. Both should depend on abstractions.
            b: Abstraction should not depend on details. Details should depend on abstraction.

Dependency injection is a software design pattern that implements inversion of control for resolving dependencies. A dependency is an object that can be used (a service). An injection is the passing of a dependency to a dependent object (a client) that would use it. The service is made part of the client's state. Passing the service to the client, rather than allowing a client to build or find the service, is the fundamental requirement of the pattern.

References: Wikipedia

No comments:

Post a Comment