Skip to content

Commit 4d6467e

Browse files
authored
Merge pull request iluwatar#569 from kapinuss/master
Adding Setter Dependency Injection as a second subtype of DI
2 parents 992e76a + 17039dc commit 4d6467e

File tree

3 files changed

+133
-11
lines changed

3 files changed

+133
-11
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.iluwatar.dependency.injection;
2+
3+
/**
4+
* The MIT License
5+
* Copyright (c) 2014-2017 Ilkka Seppälä
6+
* <p>
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+
* <p>
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
* <p>
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+
* AdvancedSorceress implements inversion of control. It depends on abstraction that can be injected
29+
* through its setter.
30+
*/
31+
public class AdvancedSorceress implements Wizard {
32+
33+
private Tobacco tobacco;
34+
35+
public void setTobacco(Tobacco tobacco) {
36+
this.tobacco = tobacco;
37+
}
38+
39+
@Override
40+
public void smoke() {
41+
tobacco.smoke(this);
42+
}
43+
}

dependency-injection/src/main/java/com/iluwatar/dependency/injection/App.java

Lines changed: 14 additions & 11 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,6 @@
2626
import com.google.inject.Injector;
2727

2828
/**
29-
*
3029
* Dependency Injection pattern deals with how objects handle their dependencies. The pattern
3130
* implements so called inversion of control principle. Inversion of control has two specific rules:
3231
* - High-level modules should not depend on low-level modules. Both should depend on abstractions.
@@ -36,21 +35,21 @@
3635
* naive implementation violating the inversion of control principle. It depends directly on a
3736
* concrete implementation which cannot be changed.
3837
* <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.
4343
* <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
4545
* Injection. {@link TobaccoModule} binds a concrete implementation to abstraction. Injector is then
4646
* used to create {@link GuiceWizard} object with correct dependencies.
47-
*
4847
*/
4948
public class App {
5049

5150
/**
5251
* Program entry point
53-
*
52+
*
5453
* @param args command line args
5554
*/
5655
public static void main(String[] args) {
@@ -60,6 +59,10 @@ public static void main(String[] args) {
6059
AdvancedWizard advancedWizard = new AdvancedWizard(new SecondBreakfastTobacco());
6160
advancedWizard.smoke();
6261

62+
AdvancedSorceress advancedSorceress = new AdvancedSorceress();
63+
advancedSorceress.setTobacco(new SecondBreakfastTobacco());
64+
advancedSorceress.smoke();
65+
6366
Injector injector = Guice.createInjector(new TobaccoModule());
6467
GuiceWizard guiceWizard = injector.getInstance(GuiceWizard.class);
6568
guiceWizard.smoke();
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/**
2+
* The MIT License
3+
* Copyright (c) 2014-2016 Ilkka Seppälä
4+
* <p>
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
* <p>
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
* <p>
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
package com.iluwatar.dependency.injection;
24+
25+
import com.iluwatar.dependency.injection.utils.InMemoryAppender;
26+
import org.junit.After;
27+
import org.junit.Before;
28+
import org.junit.Test;
29+
30+
import static org.junit.Assert.assertEquals;
31+
32+
/**
33+
* Date: 28/04/17 - 7:40 AM
34+
*
35+
* @author Stanislav Kapinus
36+
*/
37+
38+
public class AdvancedSorceressTest {
39+
40+
private InMemoryAppender appender;
41+
42+
@Before
43+
public void setUp() {
44+
appender = new InMemoryAppender(Tobacco.class);
45+
}
46+
47+
@After
48+
public void tearDown() {
49+
appender.stop();
50+
}
51+
52+
/**
53+
* Test if the {@link AdvancedSorceress} smokes whatever instance of {@link Tobacco} is passed to her
54+
* through the setter's parameter
55+
*/
56+
@Test
57+
public void testSmokeEveryThing() throws Exception {
58+
59+
final Tobacco[] tobaccos = {
60+
new OldTobyTobacco(), new RivendellTobacco(), new SecondBreakfastTobacco()
61+
};
62+
63+
for (final Tobacco tobacco : tobaccos) {
64+
final AdvancedSorceress advancedSorceress = new AdvancedSorceress();
65+
advancedSorceress.setTobacco(tobacco);
66+
advancedSorceress.smoke();
67+
// Verify if the sorceress is smoking the correct tobacco ...
68+
assertEquals("AdvancedSorceress smoking " + tobacco.getClass().getSimpleName(), appender.getLastMessage());
69+
70+
}
71+
72+
// ... and nothing else is happening.
73+
assertEquals(tobaccos.length, appender.getLogSize());
74+
75+
}
76+
}

0 commit comments

Comments
 (0)