1
1
/**
2
2
* The MIT License
3
3
* Copyright (c) 2014-2016 Ilkka Seppälä
4
- *
4
+ * <p>
5
5
* Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
* of this software and associated documentation files (the "Software"), to deal
7
7
* in the Software without restriction, including without limitation the rights
8
8
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
9
* copies of the Software, and to permit persons to whom the Software is
10
10
* furnished to do so, subject to the following conditions:
11
- *
11
+ * <p>
12
12
* The above copyright notice and this permission notice shall be included in
13
13
* all copies or substantial portions of the Software.
14
- *
14
+ * <p>
15
15
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
16
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
17
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26
26
import com .google .inject .Injector ;
27
27
28
28
/**
29
- *
30
29
* Dependency Injection pattern deals with how objects handle their dependencies. The pattern
31
30
* implements so called inversion of control principle. Inversion of control has two specific rules:
32
31
* - High-level modules should not depend on low-level modules. Both should depend on abstractions.
36
35
* naive implementation violating the inversion of control principle. It depends directly on a
37
36
* concrete implementation which cannot be changed.
38
37
* <p>
39
- * The second wizard ({@link AdvancedWizard}) is more flexible. It does not depend on any concrete
40
- * implementation but abstraction. It utilizes Dependency Injection pattern allowing its
41
- * {@link Tobacco} dependency to be injected through its constructor. This way, handling the
42
- * dependency is no longer the wizard's responsibility. It is resolved outside the wizard class.
38
+ * The second and third wizards({@link AdvancedWizard} and {@link AdvancedSorceress}) are more flexible.
39
+ * They do not depend on any concrete implementation but abstraction. They utilizes Dependency Injection
40
+ * pattern allowing their {@link Tobacco} dependency to be injected through constructor ({@link AdvancedWizard})
41
+ * or setter ({@link AdvancedSorceress}). This way, handling the dependency is no longer the wizard's
42
+ * responsibility. It is resolved outside the wizard class.
43
43
* <p>
44
- * The third example takes the pattern a step further. It uses Guice framework for Dependency
44
+ * The fourth example takes the pattern a step further. It uses Guice framework for Dependency
45
45
* Injection. {@link TobaccoModule} binds a concrete implementation to abstraction. Injector is then
46
46
* used to create {@link GuiceWizard} object with correct dependencies.
47
- *
48
47
*/
49
48
public class App {
50
49
51
50
/**
52
51
* Program entry point
53
- *
52
+ *
54
53
* @param args command line args
55
54
*/
56
55
public static void main (String [] args ) {
@@ -60,6 +59,10 @@ public static void main(String[] args) {
60
59
AdvancedWizard advancedWizard = new AdvancedWizard (new SecondBreakfastTobacco ());
61
60
advancedWizard .smoke ();
62
61
62
+ AdvancedSorceress advancedSorceress = new AdvancedSorceress ();
63
+ advancedSorceress .setTobacco (new SecondBreakfastTobacco ());
64
+ advancedSorceress .smoke ();
65
+
63
66
Injector injector = Guice .createInjector (new TobaccoModule ());
64
67
GuiceWizard guiceWizard = injector .getInstance (GuiceWizard .class );
65
68
guiceWizard .smoke ();
0 commit comments