What is Dependency Injection??

What is Dependency Injection??

Let's start by answering this very first question with a simple example:

Suppose you want the build a Car, so what are the things required to build a Car?

Engine, Wheels, Tyres etc. Hence these components are called dependencies as they are required to build a Car.

Now considering the example and will try to use it in our daily coding problems.

We have a class Car, which needs a reference of Engine class to work.

There are two major ways to get an object of the Engine class:

1. Class Car should create the object and initialize its own instance of Engine Class. For Example

class Car {

   
private Engine engine = new Engine();

   
public void startCar() {
        engine
.start();
   
}
}


class MyApp {
   
public static void main(String[] args) {
       
Car car = new Car();
        car
.startCar();
   
}
}

This approach has a few problems:

a. It will make the Car class difficult to do unit testing. Suppose you want to use Electric Engine or Diesel Engine then you have to create two Car classes which is not a good practice.

b. You won't be able to see the required components to create Car class from outside.


2. Supply the Engine Class object as a parameter from outside, when a Car class is constructed. For example.

class Car {

   
private final Engine engine;

   
public Car(Engine engine) {
       
this.engine = engine;
   
}

   
public void startCar() {
        engine
.start();
   
}
}


class MyApp {
   
public static void main(String[] args) {
       
Engine engine = new Engine();
       
Car car = new Car(engine);
        car
.startCar();
   
}
}
This approach is Dependency Injection. I know you must have used this practice already in your code but didn't know the fancy name.

Benefits of using this approach:
a. Reusability of Car class, which will make it available for all different kinds of engines and make the unit testing easier.

b. Now you can see what components are required to build Car class without actually hopping into class code.


I know you must be wondering If this is called Dependency Injection and you are already doing it then why do we need to learn the Dagger2?

Let me try to answer this question with a simple example

class Car {

   
private final Engine engine;
    private final Wheels wheels:
    private final Steer steer

   
public Car(Engine engine, Wheels wheels, Steer steer) {
       
this.engine = engine;
        this.wheels = wheels;
        this.steer = steer;
    }

   
public void startCar() {
        wheels.installed();
        steer.assembled();
        engine.start();
   
}
}


class MyApp {
   
public static void main(String[] args) {
       
Engine engine = new Engine();
        Wheels wheels = new Wheels();
        Steer steer = new Steer();
        Car car = new Car(engine, wheels, steer);
        car
.startCar();
   
}
}

As you can see multiple dependencies are added to create a Car class object.  
If you want to create a new object of a Car class then you have to initialize the dependencies again, which will cause duplicate code and will make it difficult to maintain as the project size increases.

Dagger here actually makes our life easier by automatically creating and providing the dependencies in the required class to function.

Let's wrap it up here!!

In future blogs, I will help you understand how we can optimize this solution and we will keep on optimizing until it reaches a point that is very similar to what dagger would automatically generate for you.


Thanks for reading!!

Happy Coding :)

Comments