Tags

, , , ,

When an object depends on other objects for getting some utilities, it’s called dependency.

A mobile is dependent on WiFi object to get connected to web. Not only on WiFi alone, it might have dependency on Cable and Hotspot objects, for the same reason.

The question is how we can resolve this dependency?

We can always hard code it. It is allowed. However, in that case, the two objects are tightly coupled and each time you have to change the source code to change the dependency.

The solution is common interface. We can have a common connection interface so that we can change the interface if needed without disturbing the main Mobile class. If WiFi does not work, we can try Cable, if the Cable does not work, we can have another option left: Hotspot.

Let us see how we can do that in Java.

The Mobile class:

package fun.sanjibsinha.dependency;

public class Mobile {

private ConnectionInterface connect;

public Mobile(ConnectionInterface connect){
this.connect = connect;
}

public void getConneceted(){
System.out.println("The mobile is connected through " + connect.getName());
}

}

The ConnectionInterface:

package fun.sanjibsinha.dependency;

public interface ConnectionInterface {
String getName();
void setName(String name);
}

Now we have three connection classes, WiFi, Cable and Hotspot:

package fun.sanjibsinha.dependency;

public class Wifi implements ConnectionInterface {

    private String name = "Wifi";

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
package fun.sanjibsinha.dependency;

public class Cable implements ConnectionInterface {

private String name = "Cable";

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}
package fun.sanjibsinha.dependency;

public class Hotspot implements ConnectionInterface {

private String name = "Hotspot";

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}

Now think about a situation where the Mobile is using the WiFi. Someone in the room having a laptop, wants to use the same connection. It can be done through the same common interface.

The Laptop class:

package fun.sanjibsinha.dependency;

public class Laptop {

private ConnectionInterface connect;

public Laptop(ConnectionInterface connect){
this.connect = connect;
}

public void getConneceted(){
System.out.println("The Laptop is connected through " + connect.getName());
}
}

Finally, we have the Main application class, where we can watch how the objects resolve the dependency.

The Main class:

package fun.sanjibsinha.dependency;

public class Main {

public static void main(String[] args) {

ConnectionInterface connect = new Wifi();
Mobile sam = new Mobile(connect);
sam.getConneceted();
Laptop leno = new Laptop(connect);
leno.getConneceted();

}
}

If we run the code, we get this output:

The mobile is connected through Wifi
The Laptop is connected through Wifi

Now your WiFi is not working. The laptop connects the cable and it gets the wired connection. Furthermore, the mobile uses its hotspot to get connected.

We don’t have to hard code them into our classes. We can just change the common interface.

The Main class:

package fun.sanjibsinha.dependency;

public class Main {

public static void main(String[] args) {

ConnectionInterface cable = new Cable();
Laptop leno = new Laptop(cable);
leno.getConneceted();
ConnectionInterface hotspot = new Hotspot();
Mobile sam = new Mobile(hotspot);
sam.getConneceted();
}
}

And here is the output:

The Laptop is connected through Cable
The mobile is connected through Hotspot

I hope this explanation helps you grasp the basic knowledge of dependency injection.

100 problems in Java
100 problems in Java

https://leanpub.com/100problemsinjava