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
public class ExpenseTracker {
private ExpenseCalculator expenseCal
= ExpenseCalculatorFactory.getInstance();
public void add(Transaction transaction)
{
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
public class ExpenseTracker {
private ExpenseCalculator expenseCal;
public ExpenseTracker(
ExpenseCalculator expenseCalculator)
{
this .expenseCal = expenseCalculator;
}
public void add(Transaction transaction)
{
int expense
= expenseCal.getTransactionAmount(transaction);
add(expense);
}
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. |
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
01 Apr, 2022
Like Article
Save Article