Dependency Injection (DI)
What is Dependency Injection?
ependency Injectionis a design pattern used to implementInversion of Control (IoC)where
D
an objectdoes not create its dependencies itself,but insteadreceives them from an
external source.
In simple terms:
Instead of a class creating the objects it needs, those objects areinjected from
outside.
This helps reducetight couplingbetween classes.
Problem Without Dependency Injection
Java
class
EmailService {
void
sendEmail(
String
message) {
[Link](
"Email sent: "
+ message);
}
}
class
NotificationService {
private
EmailService emailService =
new
EmailService();
void
notifyUser() {
[Link](
"Hello User"
);
}
}
Issues
T
● ight coupling
● Hard totest
● Hard toreplace dependency
NotificationService
Example: If we want to switch toSMSService, we must modify .
Solution: Dependency Injection
Java
class
NotificationService {
private
EmailService emailService;
NotificationService(EmailService emailService) {
this
.emailService = emailService;
}
void
notifyUser() {
[Link](
"Hello User"
);
}
}
Usage:
Java
EmailService emailService =
new
EmailService();
NotificationService service =
new
NotificationService(emailService);
Now:
N
● otificationServicedoes not create EmailService
● Itreceives it from outside
Types of Dependency Injection
1. Constructor Injection (Most Recommended)
Dependency is provided throughconstructor.
Java
class
OrderService {
private
PaymentService paymentService;
OrderService(PaymentService paymentService) {
this
.paymentService = paymentService;
}
}
Advantages
D
● ependency becomesmandatory
● Makes objectimmutable
● Easiertesting
2. Setter Injection
Dependency is provided through asetter method.
Java
class
OrderService {
private
PaymentService paymentService;
void
setPaymentService(PaymentService paymentService)
{
this
.paymentService = paymentService;
}
}
Advantages
D
● ependency can beoptional
● Can bechanged later
3. Field Injection (Common in frameworks)
Java
class
OrderService {
@Inject
private
PaymentService paymentService;
}
Common in frameworks like:
S
● pring Framework
● Google Guice
● Dagger
Drawbacks
H
● arder to test
● Breaks encapsulation
● Dependency hidden
ependency Injection vs Inversion of
D
Control
These are often confused.
Concept Meaning
Inversion of Control (IoC) Design principle where control is inverted
Dependency Injection (DI) Implementation of IoC
Think of it like:
None
IoC = Concept
DI = Implementation
Real World Analogy
Imagine acar.
Without DI:
None
Car creates its own Engine
With DI:
None
Engine is provided to the Car from outside
This allows:
P
● etrol engine
● Electric engine
● Hybrid engine
Carclass.
Without changing the
Dependency Injection Improves
1. Loose Coupling
Classes depend onabstractions, not concrete implementations.
Example:
Java
class
OrderService {
private
PaymentGateway gateway;
}
Instead of:
None
StripePaymentGateway
RazorpayPaymentGateway
2. Testability
Easy tomock dependencies.
Example:
Java
MockPaymentService paymentService =
new
MockPaymentService();
OrderService service =
new
OrderService(paymentService);
3. Maintainability
Changing dependencydoes not affect other classes.
4. Scalability
Allows plug-and-play architecture.
Dependency Injection in LLD Interviews
Interviewers expect you to:
Use interfaces
Java
interface
PaymentService {
void
pay();
}
Implementations:
None
CreditCardPayment
UPIPayment
NetBankingPayment
Injection:
Java
class
OrderService {
private
PaymentService paymentService;
OrderService(PaymentService paymentService) {
this
.paymentService = paymentService;
}
}
Dependency Injection with Factory
Often combined withFactory Pattern.
None
Controller
↓
Service
↓
Repository
Each layer receives dependencies throughDI.
Dependency Injection Frameworks
Popular frameworks that implement DI:
● pring Framework
S
● Dagger
● Google Guice
● Micronaut
These frameworks:
C
● reate objects
● Manage lifecycle
● Inject dependencies automatically