Inversion of Control vs Dependency Injection
Inversion of Control (IoC) : IoC or Dependency Inversion Principle (DIP) is one of the SOLID principles of object-oriented programming. It is a design guideline that promotes the creation of flexible and maintainable software by inverting the direction of dependency relationships. The Dependency Inversion Principle consists of two key concepts: High-level modules should not depend on low-level modules. Both should depend on abstractions. Abstractions should not depend on details. Details should depend on abstractions.
Here's a breakdown of the key concepts:
High-level modules and Low-level modules:
- High-level modules are modules or components that define the main functionality or business logic of an application. Low-level modules are the implementation details or components that support the high-level modules.
Depend on Abstractions:
- Instead of high-level modules depending directly on low-level modules, both should depend on abstractions (interfaces or abstract classes). This promotes a more flexible and interchangeable system.
Abstractions should not depend on details:
- Abstractions (interfaces or abstract classes) should define the contract or API without depending on specific implementations or details. This allows for multiple implementations to coexist without affecting the high-level modules.
Example without Dependency Inversion:
public class LightBulb { public void turnOn() { // Implementation details to turn on the light bulb } } public class Switch { private LightBulb bulb; public Switch() { this.bulb = new LightBulb(); // Dependency on a specific implementation } public void operate() { bulb.turnOn(); } }
In this example, the Switch
class directly depends on the LightBulb
class, violating the Dependency Inversion Principle.
Example with Dependency Inversion:
public interface Switchable { void turnOn(); } public class LightBulb implements Switchable { @Override public void turnOn() { // Implementation details to turn on the light bulb } } public class Switch { private Switchable device; public Switch(Switchable device) { this.device = device; // Dependency on abstraction (Switchable interface) } public void operate() { device.turnOn(); } }
In this improved example, the Switch
class depends on the abstraction (Switchable
interface) instead of a specific implementation (LightBulb
). This adheres to the Dependency Inversion Principle, making the system more flexible and allowing different implementations of Switchable
to be used without modifying the Switch
class.
Dependency Injection (DI): DI is a specific pattern that implements IoC. In DI, the dependencies of a class are injected into it rather than the class creating its dependencies. This is typically achieved through constructor injection, setter injection, or field injection. Spring IoC container manages the dependencies of objects and injects them when creating the objects. This reduces the tight coupling between classes and makes the code more testable and maintainable.
Comments
Post a Comment