Dependency Injection for beginners in a simple language

Mr. Das

Mr Das hates travel. And more so the business ones. But as a Senior Integration Engineer, Mr. Das has to travel to the client’s location. He has booked a nice hotel for his stay.

Hotel has given Mr Das a phone number. He can dial the number from anywhere for any kind of assist he needs.

One day in life of Mr. Das

Let’s see, life of Mr. Das in a cartoon.

Joe, the receptionist has a tough day ahead.
Joe hates phone. Guests call from literally anywhere. Mr. Das borrows phone from another guest.
Receptionist has been changed. Now, Samantha is in charge.

Magic! Who is in for a magic trick?

Who noticed that Mr. Das calls from his hotel room using his cell phone to reception. And tada! He gets a sandwich. Joe, our humble receptionist is best at his business.

So cool.

Also, Mr. Das calls from pool side while taking a sun bath using cell phone of some other guest. Joe, our receptionist makes sure Mr. Das gets his mojito.

Mr. Das got hurt while playing and he messaged to the reception number using SMS asking for painkiller.

Samantha, the other receptionist, hurried to provide Mr. Das with necessary medication.

Enough! Where was the magic?

Magic, my friend, was with the phone.

Basically, Mr. Das just have to call or message to the number provided by the hotel. He does not have to care whose phone it is. He does not have to care if the phone is a landline or a cell phone or a satellite phone. He does not care about the model of cell phone, if it is a cell phone.

Basically, Mr. Das just needs any communication device which can connect to the given number by hotel management.

Mr. Das also does not care who is receiving at reception. He does not need to know weather receptionist is Joe or Samantha or anyone else. The receptionist could have been male, female or a robot. It does not matter.

In other words, Mr. Das has dependency on communication device.

He does not have dependency on cell phone.

Mr. Das can use any communication device given by anyone as long as it dials/messages the given number.

Got the magic?

Let’s see some code?

Let’s see two different snippet where Mr. Das asks for sandwich.

public class DasInHotel {
    private Samsung phone = new Samsung();
    
    public void getSandwich(){
        phone.call();
    }
}

Here, if someone does not have Samsung phone, he can not order his sandwich. There is a hard dependancy on the Samsung phone.

public class DasInHotel {
    private CommuncationDevice phone;

    public DasInHotel(CommuncationDevice phone) {
        this.phone = phone;
    }

    public void getSandwich(){
        phone.call();
    }
}

Here, the guest can provide his or her phone in the constructor and order sandwich.

In the second snippet, we are injecting dependancy using the constructor.




Follow blog to know about upcoming post on guice.

References:

Guice

https://martinfowler.com/articles/injection.html#InversionOfControl

A special thanks to https://www.toonytool.com/ for providing me tools to make cartoons.

Advertisements

One thought on “Dependency Injection for beginners in a simple language

Add yours

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Blog at WordPress.com.

Up ↑

%d bloggers like this: