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 1
1
package com .iluwatar ;
2
2
3
+ /**
4
+ *
5
+ * AdvancedWizard implements inversion of control.
6
+ * It depends on abstraction that can be injected through
7
+ * its constructor.
8
+ *
9
+ */
3
10
public class AdvancedWizard implements Wizard {
4
11
5
12
private Tobacco tobacco ;
Original file line number Diff line number Diff line change 3
3
import com .google .inject .Guice ;
4
4
import com .google .inject .Injector ;
5
5
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
+ */
6
27
public class App {
7
28
8
29
public static void main ( String [] args ) {
Original file line number Diff line number Diff line change 2
2
3
3
import javax .inject .Inject ;
4
4
5
+ /**
6
+ *
7
+ * GuiceWizard implements inversion of control.
8
+ * Its dependencies are injected through its constructor
9
+ * by Guice framework.
10
+ *
11
+ */
5
12
public class GuiceWizard implements Wizard {
6
13
7
14
private Tobacco tobacco ;
Original file line number Diff line number Diff line change 1
1
package com .iluwatar ;
2
2
3
+ /**
4
+ *
5
+ * OldTobyTobacco concrete Tobacco implementation
6
+ *
7
+ */
3
8
public class OldTobyTobacco extends Tobacco {
4
9
}
Original file line number Diff line number Diff line change 1
1
package com .iluwatar ;
2
2
3
+ /**
4
+ *
5
+ * RivendellTobacco concrete Tobacco implementation
6
+ *
7
+ */
3
8
public class RivendellTobacco extends Tobacco {
4
9
}
Original file line number Diff line number Diff line change 1
1
package com .iluwatar ;
2
2
3
+ /**
4
+ *
5
+ * SecondBreakfastTobacco concrete Tobacco implementation
6
+ *
7
+ */
3
8
public class SecondBreakfastTobacco extends Tobacco {
4
9
}
Original file line number Diff line number Diff line change 1
1
package com .iluwatar ;
2
2
3
+ /**
4
+ *
5
+ * Naive Wizard implementation violating the inversion of control principle.
6
+ * It should depend on abstraction instead.
7
+ *
8
+ */
3
9
public class SimpleWizard implements Wizard {
4
10
5
11
private OldTobyTobacco tobacco = new OldTobyTobacco ();
Original file line number Diff line number Diff line change 1
1
package com .iluwatar ;
2
2
3
+ /**
4
+ *
5
+ * Tobacco abstraction
6
+ *
7
+ */
3
8
public abstract class Tobacco {
4
9
5
10
public void smoke (Wizard wizard ) {
Original file line number Diff line number Diff line change 2
2
3
3
import com .google .inject .AbstractModule ;
4
4
5
+ /**
6
+ *
7
+ * Guice module for binding certain concrete Tobacco implementation.
8
+ *
9
+ */
5
10
public class TobaccoModule extends AbstractModule {
6
11
7
12
@ Override
Original file line number Diff line number Diff line change 1
1
package com .iluwatar ;
2
2
3
+ /**
4
+ *
5
+ * Wizard interface
6
+ *
7
+ */
3
8
public interface Wizard {
4
9
5
10
void smoke ();
You can’t perform that action at this time.
0 commit comments