Community Post

Dependency Injection in Java

John Potter

In this post, you will learn the basics of dependency injection and how your hire java developers can use it for writing code. For further information, read this article thoroughly.

Introduction

DI makes your application

  • Low coupling
  • Improves the reusability and adaptability
  • Easy to maintain
  • Easy to Test

So what actually dependency injection is?

Let us go with a problem

Let us assume that AirtelService is the one available in the market to provide broad-brand service and the dealers are consuming the same.

Consider the below class AirtelService and it is providing the broadband service,

class AirtelService {

  public  void accessBroadbandService() {
     . . . .
     //airtel service
  }
}

Dealer consumes the Airtel broadband service but hardly depends on the AirtelService

class Dealer {

    void provideService() {
        AirtelService airtelService = new AirtelService();
        airtelService.accessBroadbandService();
    }
}

Days later, there are many services are coming to the market

And now the dealer wants to provide VodafoneService due to speed in bandwidth or good signal or any benefits that are better than AirtelService

How will you make it?

class Dealer {

    void provideService() {
        VodafoneService vodafoneService = new VodafoneService();
        vodafoneService.accessBroadbandService();
    }
}

In future, again the dealer wants to sale/provide all service to his customers, will you again go and change the implementation. No!!

So how can we make it?

GoF says - "Define an interface for creating an object, but let subclasses decide which class to instantiate. The Factory method lets a class defer instantiation it uses to subclasses."

class DealerImpl implements Dealer {

    private Service service;

    public void setService(Service service) {
       this.service = service;    
    }

    void provideService() {
        service.accessBroadbandService();
    }
}

Now the code is completely loosely coupled with Service and dealer can easily provide any service without changing existing implementation

e.g.

Here the factory pattern comes to the plate to make this job easier,

interface Dealer {

    void provideService();

    public static class Factory() {

        public Dealer createDealer(String service) {

           Dealer dealer = new Dealer();

            if(service == ‘airtel’) 
                dealer.setService(new AirtelService());
            else if(service == ‘vodafone’)
                dealer.setService(new VodafoneService());
            else
                dealer.setService(new IdeaService());

            return dealer;
        }
    }
}

And there are lot of DI frameworks available in the market like Spring DI, Google Guice etc. that take care of the factory pattern job. It is actually dependency injection container that will take care of creating objects thru xml configuration or annotations.

John Potter

1 post

My Self John Potter working with Java Professional as a Technical Writer from last 5 years. I'm very much experienced person to write technical articles in various technologies like Java, Hadoop, Python, Microsoft Dynamics CRM & ASP.net