2015-08-13 23:54:40 +02:00
---
layout: pattern
title: Dependency Injection
folder: dependency-injection
2015-08-15 18:03:05 +02:00
permalink: /patterns/dependency-injection/
2019-12-13 21:09:28 +02:00
categories: Creational
2021-05-19 10:49:05 -06:00
language: en
2015-12-28 15:52:44 +02:00
tags:
2019-12-13 21:09:28 +02:00
- Decoupling
2015-08-13 23:54:40 +02:00
---
2016-01-03 21:14:30 +01:00
## Intent
2020-08-29 16:56:52 +03:00
Dependency Injection is a software design pattern in which one or more dependencies (or services)
are injected, or passed by reference, into a dependent object (or client) and are made part of the
client's state. The pattern separates the creation of a client's dependencies from its own behavior,
which allows program designs to be loosely coupled and to follow the inversion of control and single
responsibility principles.
2020-07-21 20:04:06 +03:00
## Explanation
2020-08-29 16:56:52 +03:00
2020-07-21 20:04:06 +03:00
Real world example
2020-08-29 16:56:52 +03:00
> The old wizard likes to fill his pipe and smoke tobacco once in a while. However, he doesn't want
> to depend on a single tobacco brand only but likes to be able to enjoy them all interchangeably.
2020-07-21 20:04:06 +03:00
In plain words
> Dependency Injection separates creation of client's dependencies from its own behavior.
Wikipedia says
2020-08-29 16:56:52 +03:00
> In software engineering, dependency injection is a technique in which an object receives other
> objects that it depends on. These other objects are called dependencies.
2020-07-21 20:04:06 +03:00
**Programmatic Example**
2020-08-29 16:56:52 +03:00
Let's first introduce the `Tobacco` interface and the concrete brands.
2020-07-21 20:04:06 +03:00
```java
2021-03-13 13:19:21 +01:00
@Slf4j
2020-07-21 20:04:06 +03:00
public abstract class Tobacco {
public void smoke(Wizard wizard) {
LOGGER.info("{} smoking {}", wizard.getClass().getSimpleName(),
this.getClass().getSimpleName());
}
}
public class SecondBreakfastTobacco extends Tobacco {
}
public class RivendellTobacco extends Tobacco {
}
public class OldTobyTobacco extends Tobacco {
}
```
2020-08-29 16:56:52 +03:00
Next here's the `Wizard` class hierarchy.
2020-07-21 20:04:06 +03:00
```java
public interface Wizard {
void smoke();
}
public class AdvancedWizard implements Wizard {
2020-07-30 20:28:47 +03:00
private final Tobacco tobacco;
2020-07-21 20:04:06 +03:00
public AdvancedWizard(Tobacco tobacco) {
this.tobacco = tobacco;
}
@Override
public void smoke() {
tobacco.smoke(this);
}
}
```
And lastly we can show how easy it is to give the old wizard any brand of tobacco.
```java
var advancedWizard = new AdvancedWizard(new SecondBreakfastTobacco());
advancedWizard.smoke();
```
2015-08-13 23:54:40 +02:00
2019-12-07 20:01:13 +02:00
## Class diagram
2020-08-29 16:56:52 +03:00
2015-08-13 23:54:40 +02:00

2016-01-03 21:14:30 +01:00
## Applicability
2015-08-13 23:54:40 +02:00
2020-08-29 16:56:52 +03:00
Use the Dependency Injection pattern when:
* When you need to remove knowledge of concrete implementation from object.
* To enable unit testing of classes in isolation using mock objects or stubs.
2020-07-21 20:04:06 +03:00
## Credits
* [Dependency Injection Principles, Practices, and Patterns ](https://www.amazon.com/gp/product/161729473X/ref=as_li_qf_asin_il_tl?ie=UTF8&tag=javadesignpat-20&creative=9325&linkCode=as2&creativeASIN=161729473X&linkId=57079257a5c7d33755493802f3b884bd )
* [Clean Code: A Handbook of Agile Software Craftsmanship ](https://www.amazon.com/gp/product/0132350882/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0132350882&linkCode=as2&tag=javadesignpat-20&linkId=2c390d89cc9e61c01b9e7005c7842871 )
* [Java 9 Dependency Injection: Write loosely coupled code with Spring 5 and Guice ](https://www.amazon.com/gp/product/1788296257/ref=as_li_tl?ie=UTF8&tag=javadesignpat-20&camp=1789&creative=9325&linkCode=as2&creativeASIN=1788296257&linkId=4e9137a3bf722a8b5b156cce1eec0fc1 )
2021-09-28 21:43:31 +03:00
* [Google Guice: Agile Lightweight Dependency Injection Framework ](https://www.amazon.com/gp/product/1590599977/ref=as_li_qf_asin_il_tl?ie=UTF8&tag=javadesignpat-20&creative=9325&linkCode=as2&creativeASIN=1590599977&linkId=3b10c90b7ba480a1b7777ff38000f956 )