Skip to content

Commit a1c5131

Browse files
authored
Add files via upload
1 parent 073d06c commit a1c5131

File tree

2 files changed

+73
-23
lines changed

2 files changed

+73
-23
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.iluwatar.dependency.injection;
2+
3+
/**
4+
* The MIT License
5+
* Copyright (c) 2014-2017 Ilkka Seppälä
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
* THE SOFTWARE.
24+
*/
25+
26+
27+
/**
28+
*
29+
* AdvancedSorceress implements inversion of control. It depends on abstraction that can be injected
30+
* through its setter.
31+
*
32+
*/
33+
public class AdvancedSorceress implements Wizard {
34+
35+
private Tobacco tobacco;
36+
37+
public void setTobacco(Tobacco tobacco) {
38+
this.tobacco = tobacco;
39+
}
40+
41+
@Override
42+
public void smoke() {
43+
tobacco.smoke(this);
44+
}
45+
}
Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
/**
22
* The MIT License
33
* Copyright (c) 2014-2016 Ilkka Seppälä
4-
*
4+
* <p>
55
* Permission is hereby granted, free of charge, to any person obtaining a copy
66
* of this software and associated documentation files (the "Software"), to deal
77
* in the Software without restriction, including without limitation the rights
88
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
99
* copies of the Software, and to permit persons to whom the Software is
1010
* furnished to do so, subject to the following conditions:
11-
*
11+
* <p>
1212
* The above copyright notice and this permission notice shall be included in
1313
* all copies or substantial portions of the Software.
14-
*
14+
* <p>
1515
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1616
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1717
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
@@ -26,7 +26,7 @@
2626
import com.google.inject.Injector;
2727

2828
/**
29-
*
29+
*
3030
* Dependency Injection pattern deals with how objects handle their dependencies. The pattern
3131
* implements so called inversion of control principle. Inversion of control has two specific rules:
3232
* - High-level modules should not depend on low-level modules. Both should depend on abstractions.
@@ -36,32 +36,37 @@
3636
* naive implementation violating the inversion of control principle. It depends directly on a
3737
* concrete implementation which cannot be changed.
3838
* <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.
39+
* The second and third wizards({@link AdvancedWizard} and {@link AdvancedSorceress}) are more flexible.
40+
* They do not depend on any concrete implementation but abstraction. They utilizes Dependency Injection
41+
* pattern allowing their {@link Tobacco} dependency to be injected through constructor ({@link AdvancedWizard})
42+
* or setter ({@link AdvancedSorceress}). This way, handling the dependency is no longer the wizard's
43+
* responsibility. It is resolved outside the wizard class.
4344
* <p>
44-
* The third example takes the pattern a step further. It uses Guice framework for Dependency
45+
* The fourth example takes the pattern a step further. It uses Guice framework for Dependency
4546
* Injection. {@link TobaccoModule} binds a concrete implementation to abstraction. Injector is then
4647
* used to create {@link GuiceWizard} object with correct dependencies.
4748
*
4849
*/
4950
public class App {
5051

51-
/**
52-
* Program entry point
53-
*
54-
* @param args command line args
55-
*/
56-
public static void main(String[] args) {
57-
SimpleWizard simpleWizard = new SimpleWizard();
58-
simpleWizard.smoke();
52+
/**
53+
* Program entry point
54+
*
55+
* @param args command line args
56+
*/
57+
public static void main(String[] args) {
58+
SimpleWizard simpleWizard = new SimpleWizard();
59+
simpleWizard.smoke();
60+
61+
AdvancedWizard advancedWizard = new AdvancedWizard(new SecondBreakfastTobacco());
62+
advancedWizard.smoke();
5963

60-
AdvancedWizard advancedWizard = new AdvancedWizard(new SecondBreakfastTobacco());
61-
advancedWizard.smoke();
64+
AdvancedSorceress advancedSorceress = new AdvancedSorceress();
65+
advancedSorceress.setTobacco(new SecondBreakfastTobacco());
66+
advancedSorceress.smoke();
6267

63-
Injector injector = Guice.createInjector(new TobaccoModule());
64-
GuiceWizard guiceWizard = injector.getInstance(GuiceWizard.class);
65-
guiceWizard.smoke();
66-
}
68+
Injector injector = Guice.createInjector(new TobaccoModule());
69+
GuiceWizard guiceWizard = injector.getInstance(GuiceWizard.class);
70+
guiceWizard.smoke();
71+
}
6772
}

0 commit comments

Comments
 (0)