In this article, we will explore some of the benefits of dependency injection and how this can be used in all types of programming, including small and simple programs. Some of the example shown below make use of Guice (Homepage), a dependency injection framework by Google. The same concept applies for any other dependency injection framework such as Spring (Homepage).

Please note that this article does not provide detailed description of Guice or any other dependency injection framework, but it only focuses on the benefits of dependency injection and design methods that improve modularity, extendibility and testing. The main idea behind this article is to understand why dependency injection should be used rather than how to implement it.

All code listed below is available at: https://github.com/javacreed/why-should-we-use-dependency-injection. Most of the examples will not contain the whole code and may omit fragments which are not relevant to the example being discussed. The readers can download or view all code from the above link.

What is dependency injection?

Let say we want to make some tea. How would we do it and what do we need? We need some boiled water, a clean mug, teabags, sugar and milk (at least that's what I put into mine). In order to make tea we need to provide all these ingredients and we need to manage all these ourselves. We have to fill the kettle with water and heat it up, get the milk from the refrigerator and get the rest.

Now assume that we can make tea, by simply asking someone else to do it for us. Someone that knows how to do the tea just the way we want it. Would that be better than making the tea ourselves? If yes, then welcome to dependency injection.

Dependency injection is a framework that takes care of creating objects for us without having to worry about providing the right ingredients so to say.

A Simple Example

Let say we have a class, Person, and this class needs to send a message. The Person class requires the aid of some other class, Email, in order to send a message. Following is a simple way of doing this.

package com.javacreed.examples.di.part1;

public class Email {
 public void sendEmail(String subject, String message){
   // Send the email
 }
}
package com.javacreed.examples.di.part1;

public class Person {
 private Email email = new Email();

 public void greetFriend(){
   email.sendEmail("Hello", "Hello my friend :)");
 }
}

We all agree that this is a very simple and straightforward example that involves two simple Java classes. Nevertheless, the above has some limitations as described below.

  • The Person class is dependent (has a strong/tight dependency) on the Email class. There is a hard connection between these two classes. Let say we have a new and better version of email class, FastEmail, in order for us to use it, we need to go in each and every class that depends on the Email class, such as the Person class, and replace it with the new version.
  • Let say we parameterise the Email's constructor. Again we have to go in each and every class that is initialising the Email class, such as the Person class, and change it.
  • A design decision is taken to make the Email class Singleton (Wiki). Similar to above we need to modify all instances where it is used.
  • In order to improve the notifications/messages system, we decide to add different message delivery systems such as SMS or tweets. The Person class and others like it, need to all be modified in order for it to use the new implementations.
  • Another developer needs to use the Person class, but would like to use a different notification/message system. This cannot be achieved with the current version of the Person class as it is hardwired to the Email class. What generally happens is that the other developer duplicates the Person class and modifies it as he/she needs. The projects ends up with two versions of the Person class.
  • In the above points we mentioned many scenarios where code has to be changed. All changes made, need to and should be tested. How can we test the Person class without including the message delivery class such as the Email? Testing, in many cases, is left as an afterthought. The way we have the Person class constructed makes it hard to test it without involving the Email class. Furthermore, how would we automate such test? How can we use JUnit (Homepage), or the like, to automate out tests?
  • Moving forward in the project, the Person class starts to depend on another class that allow this object to write a letter using the Pen class for example. The Person class can use other ways to write a letter, such as Pencil class or Typewriter class, but this approach does not allow that.

These limitations can be improved by changing the way we think and refactor our code in a modular way. This is independent from dependency injection and the dependency injection framework as we will see in the following section.

Change the way we think

The Email class provides a service, that is, sending of messages over the Internet using the mail protocol. Instead of having the Person class initialising an instance of the Email class, we first create an interface, MessageService, and make the Person class using this interface instead. This removes the dependency that the Person class has on the Email and replaces it with an abstract message delivery interface.

The following three steps: define, implement and use, show how we can develop modular and extendable code. This approach also improves testing as we will see at the end of this article.

  1. Define Interfaces

    Many developers do not use interfaces as they see them as additional, non-required, code. This may be true (I said maybe as the System class makes use of interfaces) for the famous hello world (example) program, definitely not true for the rest. Like with everything else, we have to see things in context and there will be cases when we can do without interfaces. Nevertheless, developing code using interfaces produce far more modular and extendable code as illustrated in the following examples. The example discussed in the previous section was quite simple, but it included several pitfalls which can be easily avoided by using interfaces instead of concrete classes to define services. Changing code at a later stage involves more work than having it right in the first place.

    We start by defining the MessageService interface that includes one method, sendMessage(String subject, String message). For simplicity we assume that no exceptions are thrown.

    package com.javacreed.examples.di.part2;
    
    public interface MessageService {
     void sendMessage(String subject, String message);
    }
    
  2. Implement Interfaces

    In the list of limitations we mentioned four possible methods of sending a message: email, fast email, SMS and tweet. Let's create four classes that handle each message delivery method and have all these classes implement the interface created above.

    package com.javacreed.examples.di.part2;
    
    public class EmailService implements MessageService {
     @Override
     public void sendMessage(String subject, String message){
       System.out.printf("Email: %s, %s%n", subject, message);
     }
    }
    
    package com.javacreed.examples.di.part2;
    
    public class FastEmailService implements MessageService {
     @Override
     public void sendMessage(String subject, String message){
       System.out.printf("Fast Email: %s, %s%n", subject, message);
     }
    }
    
    package com.javacreed.examples.di.part2;
    
    public class SmsService implements MessageService {
     @Override
     public void sendMessage(String subject, String message){
       System.out.printf("SMS: %s, %s%n", subject, message);
     }
    }
    
    package com.javacreed.examples.di.part2;
    
    public class TweetService implements MessageService {
     @Override
     public void sendMessage(String subject, String message){
       System.out.printf("Tweet: %s, %s%n", subject, message);
     }
    }
    
  3. Use Interfaces

    Finally, instead of using classes, we use interfaces. In the Person class, we replace the Email field with the MessageService service interface as highlighted below.

    public class Person {
     private MessageService messageService;
    
     public Person(MessageService messageService){
       this.messageService = messageService;
     }
    
     public void greetFriend(){
       messageService.sendMessage("Hello", "Hello my friend :)");
     }
    }
    

    Note that the Person class is not initialising the message service but it is expecting it as a parameter of its constructor. This is a key element in the design. It improves modularity, extendibility and testing. The Person class is not dependent on any implementation, but on a service defined by an interface. This means that we can use the Person class without having to worry about the underlying implementation of the message service. Furthermore, different Person instances can be instantiated using different message services.

One can argue that the new version of Person class became more complex to instantiate as it requires parameters. This is a fair statement and here is when dependency injection comes into play.

Using Dependency Injection

As mentioned in the introduction, dependency injection can help us initialising objects and provide these objects all the necessary resources (ingredients). For example, the Person class requires an instance of MessageService. The dependency injection framework will provide that for us. So to create an instance of Person class, all we need to do is call something like:

dependecyFramework.getInstance(Person.class)

Magically, (not really), the dependency injection framework will create an instance of the Person class and provide an instance of the MessageService to the Person object.

The next natural question will be, how does the dependency injection framework knows how to initialise the MessageService? We need to tell the dependency injection framework how to create an instance of MessageService. With Guice we do that by creating a module (a class that extends AbstractModule class) as illustrated below.

package com.javacreed.examples.di.part2;

import com.google.inject.AbstractModule;

public class ProjectModule extends AbstractModule {

 @Override
 protected void configure() {
   bind(MessageService.class).to(EmailService.class);
 }
}

Here we are telling the dependency injection framework (Guice) how to create an instance of the MessageService class. We also need to add an annotation to the Person class in order to allow the dependency injection framework to inject the necessary parameters.

package com.javacreed.examples.di.part2;

import com.google.inject.Inject;

public class Person {
 private MessageService messageService;

 @Inject
 public Person(MessageService messageService){
   this.messageService = messageService;
 }

 public void greetFriend(){
   messageService.sendMessage("Hello", "Hello my friend :)");
 }
}

Note that the same concept can be applies to Spring or other dependency injection frameworks. Each dependency injection framework has its way of configuration and the above only applies to Guice. Spring and the others dependencies injection frameworks make use of different configuration methods.

With everything set, we create an instance of the Person class using the dependency injection framework.

package com.javacreed.examples.di.part2;

import com.google.inject.Guice;
import com.google.inject.Injector;

public class Main {
 public static void main(String[] args) {
   Injector injector = Guice.createInjector(new ProjectModule());
   Person person = injector.getInstance(Person.class);
   person.greetFriend();
 }
}

We replaced a couple of lines of code with many others. What's the buzz about this? In the next section we will see some of the benefits of dependency injection and how this can be used to simplify our coding life.

Benefits of Dependency Injection

In this section we will see some key benefits of dependency injection

  • Changing the message service

    Let's change the delivery method from email to SMS. How would we do that?

    We only need to change the ProjectModule class to map the MessageService class to the SmsService class as highlighted in the following example.

    package com.javacreed.examples.di.part3;
    
    import com.google.inject.AbstractModule;
    
    public class ProjectModule extends AbstractModule {
    
     @Override
     protected void configure() {
       bind(MessageService.class).to(SmsService.class);
     }
    }
    

    This one change will affect all classes initialised with the dependency injection framework without having to change any of these classes. This leads us to another advantage: testing.

  • Testing and Mocking the message service

    We can create a MockMessageService class which can be using in JUnit test case as shown next.

    package com.javacreed.examples.di.part3;
    
    public class MockMessageService implements MessageService {
    
     public String subject;
     public String message;
    
     @Override
     public void sendMessage(String subject, String message) {
       this.subject = subject;
       this.message = message;
     }
    }
    

    The above mock message service simply stores the parameters into two public fields. These fields can be used to retrieve the values of the parameters and used for testing ass illustrated next.

    package com.javacreed.examples.di.part3;
    
    import org.junit.Assert;
    import org.junit.Before;
    import org.junit.Test;
    
    import com.google.inject.AbstractModule;
    import com.google.inject.Guice;
    import com.google.inject.Injector;
    import com.google.inject.Singleton;
    
    public class TestPerson {
    
     private Injector injector;
    
     @Before
     public void init() {
       injector = Guice.createInjector(new AbstractModule() {
         @Override
         protected void configure() {
           bind(MessageService.class).to(MockService.class);
         }
       });
     }
    
     @Test
     public void testGreetFriend() {
       Person person = injector.getInstance(Person.class);
       person.greetFriend();
    
       MockService mockService = injector.getInstance(MockService.class);
       Assert.assertEquals("Greet", mockService.subject);
       Assert.assertEquals("Hello my friend", mockService.message);
     }
    }
    

    This may require some explanation. So here we go. In the init() method we created a dependency injection context just for this testing and provided a custom module (as an inner anonymous class). The custom module wires the MessageService with the MockMessageService instead. After the greetFriend() method is invoked, we ascertain that the correct parameters are being passed to the message service instance.

    This design setup allows us to test the Person class independent from the other classes that it depends on in an automated manner.

  • Changing the signature of the Person constructor

    As we mentioned in the list of limitations, the Person class may evolve and include more functionality. Changing the signature of the Person constructor will not affect us as long as the injector knows how to provide the required parameters.

    package com.javacreed.examples.di.part3;
    
    import com.google.inject.Inject;
    
    public class Person {
    
     private final MessageService messageService;
     private final WritingService writingService;
    
     @Inject
     private Person(final MessageService messageService, WritingService writeService) {
       this.messageService = messageService;
       this.writingService = writeService;
     }
    
     public void greetFriend() {
       messageService.sendMessage("Hello", "Hello my friend :)");
     }
     
     public void writeToFriend() {
       writingService.write("Hello my friend :)");
     }
    }
    

    The Person class will still be initialised in the same way as it is now. Thus changing the Person constructor signature will not affect the other classes that make us of it.

    Person person = injector.getInstance(Person.class);
    

    The same applies for anything that is initialised and handled through the dependency injection framework.

  • Passing the Injector as parameter

    In a project we can have one instance shared throughout the project. This can be achieved by passing the Injector as a parameter to other objects that need it. We setup the Injector at the beginning (in a main() method for example), and then have it set as a constructor parameter in all the classes that require an instance of the injector. Like that, one injector will server all classes in the project.

Conclusion

This ended up to be quite a long article. Dependency injection is quite simple to use and it has quite a "shallow" learning curve. This article did not explain how to use the actual dependency injection framework (such as Guice or Spring). Articles about these are easily found. This article described key benefits of using a dependency injection framework, even in small projects.