Dependency Inversion Principle (DIP)

Gani Demirel
2 min readFeb 26, 2023

The Dependency Inversion Principle (DIP) is a software design principle that states that high-level modules should not depend on low-level modules; both should depend on abstractions. Furthermore, abstractions should not depend on details; details should depend on abstractions. This principle helps us to create software that is more flexible, reusable, and maintainable.

Here’s a simple C# example to illustrate the DIP:

Suppose we have a “WeatherStation” class that is responsible for displaying the current temperature. The “WeatherStation” class depends on a “TemperatureSensor” class to get the current temperature.

Here’s an example code that violates the DIP:

public class WeatherStation
{
private TemperatureSensor _temperatureSensor;

public WeatherStation()
{
_temperatureSensor = new TemperatureSensor();
}

public void DisplayTemperature()
{
double temperature = _temperatureSensor.GetTemperature();
Console.WriteLine($"Current temperature: {temperature} degrees Celsius");
}
}

public class TemperatureSensor
{
public double GetTemperature()
{
// Get temperature from a physical sensor
return 25.0;
}
}

In this implementation, the “WeatherStation” class depends on the “TemperatureSensor” class, which is a low-level module. This violates the DIP because high-level modules should not depend on low-level modules.

Here’s an example code that follows the DIP:

public interface ITemperatureSensor
{
double GetTemperature();
}

public class PhysicalTemperatureSensor : ITemperatureSensor
{
public double GetTemperature()
{
// Get temperature from a physical sensor
return 25.0;
}
}

public class WeatherStation
{
private ITemperatureSensor _temperatureSensor;

public WeatherStation(ITemperatureSensor temperatureSensor)
{
_temperatureSensor = temperatureSensor;
}

public void DisplayTemperature()
{
double temperature = _temperatureSensor.GetTemperature();
Console.WriteLine($"Current temperature: {temperature} degrees Celsius");
}
}

In this implementation, we’ve created an interface called “ITemperatureSensor” that defines the “GetTemperature()” method. We’ve also created a concrete class called “PhysicalTemperatureSensor” that implements the “ITemperatureSensor” interface. Finally, we’ve modified the “WeatherStation” class to depend on the “ITemperatureSensor” interface instead of the “TemperatureSensor” class. This allows us to inject any object that implements the “ITemperatureSensor” interface into the “WeatherStation” class. This follows the DIP because high-level modules depend on abstractions (i.e., the “ITemperatureSensor” interface) instead of low-level modules (i.e., the “PhysicalTemperatureSensor” class).

--

--

Gani Demirel
Gani Demirel

Written by Gani Demirel

Experienced full stack developer with a focus on creating beautiful and functional web pages. Proficient in .Net Core, MVC, and React.

No responses yet