Skip to content
Related Articles

Related Articles

Spring – Difference Between Dependency Injection and Factory Pattern

View Discussion
Improve Article
Save Article
  • Last Updated : 01 Apr, 2022
View Discussion
Improve Article
Save Article

Dependency Injection and Factory Pattern are almost similar in the sense that they both follow the interface-driven programming approach and create the instance of classes.

A. Factory Pattern

In Factory Pattern, the client class is still responsible for getting the instance of products by class getInstance() method of factory class, which means the client class is directly coupled with factory class and it can not be unit tested without the factory class.

Implementation: In this example, we will the difference between Factory Pattern and DI with the help of the ExpenseTracker application. In this application, we have a dependent class ExpenseTracker which depends on ExpenseCalculator class. Here we will be using both Factory Pattern and Dependency Injection to understand the difference between them.

Example:

Java




// Java Program to Illustrate Factory Pattern
 
// Dependent class
public class ExpenseTracker {
 
    // Getting instance of ExpenseCalculator class
    // using Factory class method
    private ExpenseCalculator expenseCal
        = ExpenseCalculatorFactory.getInstance();
 
    // Method
    public void add(Transaction transaction)
    {
 
        // Returns the expense amount
        // of ExpenseCalculator class
        int expense
            = expenseCal.getTransactionAmount(transaction);
        add(expense);
    }
}


Code Explanation: In the above example, we can see that our dependent class ExpenseTracker is directly coupled with ExpenseCalculatorFactory as it is calling the static getInstance() method of ExpenseCalculatorFactory class in order to satisfy its dependency. If we want to test ExpenseTracker class, we must have ExpenseCalculatorFactory class which is not suitable for the Unit Testing of ExpenseTracker class and makes the unit testing harder.

On other hand, If we will use DI, then the dependencies are added by Spring framework or DI container as we reverse the responsibilities of dependencies acquiring. This will make the IOC container to be responsible for injecting the dependencies rather than the dependent class and it will turn any client or dependent class looks into a POJO class.

B. Dependency Injection

In the case of Dependency Injection(DI), the client is not aware of how the dependencies are created and managed. The client class is only aware of the dependencies and most of the dependencies are injected by the IOC container. For Example, Bean class exists without any hardcoded dependency and they are injected by IOC containers like Spring Framework.

Example:

Java




// Java Program to Illustrate Dependency Injection
 
// Dependent class ExpenseTracker
public class ExpenseTracker {
 
    // Class data member
    private ExpenseCalculator expenseCal;
 
    // Constructor
    // Dependencies are injected
    // using Constructor Dependency Injection
    public ExpenseTracker(
        ExpenseCalculator expenseCalculator)
    {
 
        // This keyword refers to current instance itself
        this.expenseCal = expenseCalculator;
    }
 
    // Method
    public void add(Transaction transaction)
    {
        int expense
            = expenseCal.getTransactionAmount(transaction);
        add(expense);
    }
 
    // Dependencies are injected
    // using Setter Dependency Injection
    public void setExpenseCalculator(
        ExpenseCalculator expenseCalculator)
    {
        this.expenseCal = expenseCalculator;
    }
}


Code Explanation: We can see that the dependency of ExpenseCalculator is injected in the ExpenseTracker class using the constructor of the class, this is called Constructor Dependency Injection. Also, we can inject the dependency using Setter methods which also we can see in the above code, it’s called Setter Dependency Injection.

Difference between Dependency Injection and Factory Pattern

Dependency Injection

Factory Pattern

DI is only aware of the dependencies. It does not know anything about the container or factory. It adds coupling between objects, factory, and dependency. It requires both a dependent object and a factory object to work properly.
DI makes the unit tests easier. It does not require boilerplate code. The factory pattern requires the object you want to test, the factory object, and the dependent object.
DI is more flexible than factory patterns. It also gives the facility to switch different DI frameworks such as Spring IOC and Google Guice. It is not much flexible as Dependency Injection.
DI requires a container and configuration in order to inject dependencies. The factory pattern does not require these configuration steps.
Due to less coupling, the result of DI is much cleaner. The client class looks like the POJO class. In the case of the Factory pattern, the client class is not clean as in DI.

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!