File tree Expand file tree Collapse file tree 10 files changed +71
-0
lines changed
dependency-injection/src/main/java/com/iluwatar Expand file tree Collapse file tree 10 files changed +71
-0
lines changed Original file line number Diff line number Diff line change 11package com .iluwatar ;
22
3+ /**
4+ *
5+ * AdvancedWizard implements inversion of control.
6+ * It depends on abstraction that can be injected through
7+ * its constructor.
8+ *
9+ */
310public class AdvancedWizard implements Wizard {
411
512 private Tobacco tobacco ;
Original file line number Diff line number Diff line change 33import com .google .inject .Guice ;
44import com .google .inject .Injector ;
55
6+ /**
7+ *
8+ * Dependency Injection pattern deals with how objects handle their dependencies. The pattern
9+ * implements so called inversion of control principle. Inversion of control has two specific rules:
10+ * - High-level modules should not depend on low-level modules. Both should depend on abstractions.
11+ * - Abstractions should not depend on details. Details should depend on abstractions.
12+ *
13+ * In this example we show you three different wizards. The first one (SimpleWizard) is a naive
14+ * implementation violating the inversion of control principle. It depends directly on a concrete
15+ * implementation which cannot be changed.
16+ *
17+ * The second wizard (AdvancedWizard) is more flexible. It does not depend on any concrete implementation
18+ * but abstraction. It utilizes Dependency Injection pattern allowing its Tobacco dependency to be
19+ * injected through its constructor. This way, handling the dependency is no longer the wizard's
20+ * responsibility. It is resolved outside the wizard class.
21+ *
22+ * The third example takes the pattern a step further. It uses Guice framework for Dependency Injection.
23+ * TobaccoModule binds a concrete implementation to abstraction. Injector is then used to create
24+ * GuiceWizard object with correct dependencies.
25+ *
26+ */
627public class App {
728
829 public static void main ( String [] args ) {
Original file line number Diff line number Diff line change 22
33import javax .inject .Inject ;
44
5+ /**
6+ *
7+ * GuiceWizard implements inversion of control.
8+ * Its dependencies are injected through its constructor
9+ * by Guice framework.
10+ *
11+ */
512public class GuiceWizard implements Wizard {
613
714 private Tobacco tobacco ;
Original file line number Diff line number Diff line change 11package com .iluwatar ;
22
3+ /**
4+ *
5+ * OldTobyTobacco concrete Tobacco implementation
6+ *
7+ */
38public class OldTobyTobacco extends Tobacco {
49}
Original file line number Diff line number Diff line change 11package com .iluwatar ;
22
3+ /**
4+ *
5+ * RivendellTobacco concrete Tobacco implementation
6+ *
7+ */
38public class RivendellTobacco extends Tobacco {
49}
Original file line number Diff line number Diff line change 11package com .iluwatar ;
22
3+ /**
4+ *
5+ * SecondBreakfastTobacco concrete Tobacco implementation
6+ *
7+ */
38public class SecondBreakfastTobacco extends Tobacco {
49}
Original file line number Diff line number Diff line change 11package com .iluwatar ;
22
3+ /**
4+ *
5+ * Naive Wizard implementation violating the inversion of control principle.
6+ * It should depend on abstraction instead.
7+ *
8+ */
39public class SimpleWizard implements Wizard {
410
511 private OldTobyTobacco tobacco = new OldTobyTobacco ();
Original file line number Diff line number Diff line change 11package com .iluwatar ;
22
3+ /**
4+ *
5+ * Tobacco abstraction
6+ *
7+ */
38public abstract class Tobacco {
49
510 public void smoke (Wizard wizard ) {
Original file line number Diff line number Diff line change 22
33import com .google .inject .AbstractModule ;
44
5+ /**
6+ *
7+ * Guice module for binding certain concrete Tobacco implementation.
8+ *
9+ */
510public class TobaccoModule extends AbstractModule {
611
712 @ Override
Original file line number Diff line number Diff line change 11package com .iluwatar ;
22
3+ /**
4+ *
5+ * Wizard interface
6+ *
7+ */
38public interface Wizard {
49
510 void smoke ();
You can’t perform that action at this time.
0 commit comments