Example of Dependency Injection Design Patterns in Java

Problem Statement

Consider any vehicle. Let us say, this vehicle is a car. For this car to function smoothly we have various components like the car’s engine, the car’s headlights & many such components that are assembled to build the car.

These components can be considered as your dependencies. Without any of these dependencies, our car can never be considered as a complete entity.

In the above image,

  • We have a set of dependencies, these dependencies can be the Engine of a vehicle. There maybe more such dependencies present. These dependencies are handed over to an external agent, in this case, the external agent is the Spring framework.
  • This external agent, that is the Spring framework will take over the responsibility of injecting these dependencies into the respective Car, Bike and Scooter classess, as depicted in the above image.
  • These dependencies are the instance variables of your Java classes that get injected into our targeted classes through the external agent that is taking control of this process.

Why do we need to inject dependencies through some external agent?

When you inject dependencies through an external agent, your code becomes less tightly coupled to the specific implementations of those dependencies.

  • We can easily swap out different implementations of the same dependency without having to modify your main code. This makes your code more modular and easier to maintain.
  • Ultimately, the best way to decide whether or not to use dependency injection is to consider the specific needs of your application.
  • If you are writing code that is likely to change or that needs to be easily tested and reused, then dependency injection can be a valuable tool.

Step wise Step Implementation for the above Problem

In the below solution we will understand above problem using code:

Now let us assume, our car is fitted with the most branded engine model of a certain company. Consider this car as your Java class containing its respective engine model as a dependency.

Java




public class Car
{
    /** Car containing engine as its dependency **/
    public Engine engine = new LegacyEngine();
}


The class Car has an instance variable Engine . Engine is an interface that is implemented by the respective class LegacyEngine.This instance variable Engine is initialized to a new object as seen above.

Java




public interface Engine
{
    /** The Engine interface having methods for the engine to work**/
}


As we see in the above code snippet, this Engine interface is a specification used here. This interface will be implemented by the respective classes that will override the abstract methods of this interface providing the appropriate functionalities.

Java




public class LegacyEngine implements Engine
{
 
}


Now in the above code snippet, the Engine interface gets implemented by the LegacyEngine class. Assume the interface has some abstract methods that are being overridden by this implementing LegacyEngine class.

Java Dependency Injection (DI) Design Pattern

The dependency Injection Design Pattern in Java, is part of Design Patterns, let suppose that you have a company, that companies dependent on the employees, and the employees is dependent on the company. So here company will be the dependency of the employee, and employee dependency is the company. So this is Dependency Injection.

Important Topics for the Java Dependency Injection (DI) Design Pattern

  • What is the Dependency Injection Design Pattern in Java?
  • How Dependency Injection Design Patterns in Java work?
  • Example of Dependency Injection Design Patterns in Java
  • Real-world problem that occurs with Java Dependency Injection Design Pattern
  • The solution to solve this Problem by using Spring Boot:
  • Advantages of using Java Dependency Injection Design Pattern
  • Disadvantages of using Java Dependency Injection Design Pattern

Similar Reads

What is the Dependency Injection Design Pattern in Java?

...

How Dependency Injection Design Patterns in Java work?

...

Example of Dependency Injection Design Patterns in Java

Problem Statement...

Real-world problem that occurs with Java Dependency Injection Design Pattern

...

The solution to solve this Problem by using Spring Boot:

...

Advantages of using Java Dependency Injection Design Pattern

...

Disadvantages of using Java Dependency Injection Design Pattern

...

Contact Us