|
9 | 9 | --- |
10 | 10 |
|
11 | 11 | ## Intent |
12 | | -Dependency Injection is a software design pattern in which one or |
13 | | -more dependencies (or services) are injected, or passed by reference, into a |
14 | | -dependent object (or client) and are made part of the client's state. The |
15 | | -pattern separates the creation of a client's dependencies from its own |
16 | | -behavior, which allows program designs to be loosely coupled and to follow the |
17 | | -inversion of control and single responsibility principles. |
| 12 | +Dependency Injection is a software design pattern in which one or more dependencies (or services) are injected, or |
| 13 | +passed by reference, into a dependent object (or client) and are made part of the client's state. The pattern separates |
| 14 | +the creation of a client's dependencies from its own behavior, which allows program designs to be loosely coupled and |
| 15 | +to follow the inversion of control and single responsibility principles. |
| 16 | + |
| 17 | +## Explanation |
| 18 | +Real world example |
| 19 | + |
| 20 | +> 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. |
| 21 | +
|
| 22 | +In plain words |
| 23 | + |
| 24 | +> Dependency Injection separates creation of client's dependencies from its own behavior. |
| 25 | +
|
| 26 | +Wikipedia says |
| 27 | + |
| 28 | +> 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. |
| 29 | +
|
| 30 | +**Programmatic Example** |
| 31 | + |
| 32 | +Let's first introduce the tobacco brands. |
| 33 | + |
| 34 | +```java |
| 35 | +public abstract class Tobacco { |
| 36 | + |
| 37 | + private static final Logger LOGGER = LoggerFactory.getLogger(Tobacco.class); |
| 38 | + |
| 39 | + public void smoke(Wizard wizard) { |
| 40 | + LOGGER.info("{} smoking {}", wizard.getClass().getSimpleName(), |
| 41 | + this.getClass().getSimpleName()); |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +public class SecondBreakfastTobacco extends Tobacco { |
| 46 | +} |
| 47 | + |
| 48 | +public class RivendellTobacco extends Tobacco { |
| 49 | +} |
| 50 | + |
| 51 | +public class OldTobyTobacco extends Tobacco { |
| 52 | +} |
| 53 | +``` |
| 54 | + |
| 55 | +Next here's the wizard class hierarchy. |
| 56 | + |
| 57 | +```java |
| 58 | +public interface Wizard { |
| 59 | + |
| 60 | + void smoke(); |
| 61 | +} |
| 62 | + |
| 63 | +public class AdvancedWizard implements Wizard { |
| 64 | + |
| 65 | + private Tobacco tobacco; |
| 66 | + |
| 67 | + public AdvancedWizard(Tobacco tobacco) { |
| 68 | + this.tobacco = tobacco; |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public void smoke() { |
| 73 | + tobacco.smoke(this); |
| 74 | + } |
| 75 | +} |
| 76 | +``` |
| 77 | + |
| 78 | +And lastly we can show how easy it is to give the old wizard any brand of tobacco. |
| 79 | + |
| 80 | +```java |
| 81 | + var advancedWizard = new AdvancedWizard(new SecondBreakfastTobacco()); |
| 82 | + advancedWizard.smoke(); |
| 83 | +``` |
18 | 84 |
|
19 | 85 | ## Class diagram |
20 | 86 |  |
21 | 87 |
|
22 | 88 | ## Applicability |
23 | 89 | Use the Dependency Injection pattern when |
24 | 90 |
|
25 | | -* when you need to remove knowledge of concrete implementation from object |
26 | | -* to enable unit testing of classes in isolation using mock objects or stubs |
| 91 | +* When you need to remove knowledge of concrete implementation from object |
| 92 | +* To enable unit testing of classes in isolation using mock objects or stubs |
| 93 | + |
| 94 | +## Credits |
| 95 | + |
| 96 | +* [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) |
| 97 | +* [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) |
| 98 | +* [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) |
| 99 | +* [Google Guice Tutorial: Open source Java based dependency injection framework](https://www.amazon.com/gp/product/B083P7DZ8M/ref=as_li_tl?ie=UTF8&tag=javadesignpat-20&camp=1789&creative=9325&linkCode=as2&creativeASIN=B083P7DZ8M&linkId=04f0f902c877921e45215b624a124bfe) |
0 commit comments