Skip to content

Commit 09fb790

Browse files
Merge branch 'master' into extension-objects
2 parents 22a7c15 + f87249e commit 09fb790

File tree

39 files changed

+1227
-32
lines changed

39 files changed

+1227
-32
lines changed

abstract-factory/README.md

+16
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,22 @@ Use the Abstract Factory pattern when
2727
* a system should be configured with one of multiple families of products
2828
* a family of related product objects is designed to be used together, and you need to enforce this constraint
2929
* you want to provide a class library of products, and you want to reveal just their interfaces, not their implementations
30+
* the lifetime of the dependency is conceptually shorter than the lifetime of the consumer.
31+
* you need a run-time value to construct a particular dependency
32+
* you want to decide which product to call from a family at runtime.
33+
* you need to supply one or more parameters only known at run-time before you can resolve a dependency.
34+
35+
## Use Cases:
36+
37+
* Selecting to call the appropriate implementation of FileSystemAcmeService or DatabaseAcmeService or NetworkAcmeService at runtime.
38+
* Unit test case writing becomes much easier
39+
40+
## Consequences:
41+
42+
* Dependency injection in java hides the service class dependencies that can lead to runtime errors that would have been caught at compile time.
43+
44+
45+
3046

3147
## Real world examples
3248

data-bus/src/main/java/com/iluwatar/databus/AbstractDataType.java

+22
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,25 @@
1+
/**
2+
* The MIT License
3+
* Copyright (c) 2014 Ilkka Seppälä
4+
*
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+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
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+
*/
123
/*
224
The MIT License (MIT)
325

data-bus/src/main/java/com/iluwatar/databus/DataType.java

+22
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,25 @@
1+
/**
2+
* The MIT License
3+
* Copyright (c) 2014 Ilkka Seppälä
4+
*
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+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
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+
*/
123
/*
224
The MIT License (MIT)
325

data-bus/src/main/java/com/iluwatar/databus/Member.java

+22
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,25 @@
1+
/**
2+
* The MIT License
3+
* Copyright (c) 2014 Ilkka Seppälä
4+
*
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+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
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+
*/
123
/*
224
The MIT License (MIT)
325

data-bus/src/test/java/com/iluwatar/databus/DataBusTest.java

+22
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,25 @@
1+
/**
2+
* The MIT License
3+
* Copyright (c) 2014 Ilkka Seppälä
4+
*
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+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
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+
*/
123
package com.iluwatar.databus;
224

325
import org.junit.Before;

data-bus/src/test/java/com/iluwatar/databus/members/MessageCollectorMemberTest.java

+22
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,25 @@
1+
/**
2+
* The MIT License
3+
* Copyright (c) 2014 Ilkka Seppälä
4+
*
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+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
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+
*/
123
package com.iluwatar.databus.members;
224

325
import com.iluwatar.databus.data.MessageData;

data-bus/src/test/java/com/iluwatar/databus/members/StatusMemberTest.java

+22
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,25 @@
1+
/**
2+
* The MIT License
3+
* Copyright (c) 2014 Ilkka Seppälä
4+
*
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+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
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+
*/
123
package com.iluwatar.databus.members;
224

325
import com.iluwatar.databus.DataBus;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/**
2+
* The MIT License
3+
* Copyright (c) 2014 Ilkka Seppälä
4+
*
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+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
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+
/**
26+
* The MIT License
27+
* Copyright (c) 2014-2017 Ilkka Seppälä
28+
* <p>
29+
* Permission is hereby granted, free of charge, to any person obtaining a copy
30+
* of this software and associated documentation files (the "Software"), to deal
31+
* in the Software without restriction, including without limitation the rights
32+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
33+
* copies of the Software, and to permit persons to whom the Software is
34+
* furnished to do so, subject to the following conditions:
35+
* <p>
36+
* The above copyright notice and this permission notice shall be included in
37+
* all copies or substantial portions of the Software.
38+
* <p>
39+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
40+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
41+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
42+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
43+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
44+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
45+
* THE SOFTWARE.
46+
*/
47+
48+
49+
/**
50+
* AdvancedSorceress implements inversion of control. It depends on abstraction that can be injected
51+
* through its setter.
52+
*/
53+
public class AdvancedSorceress implements Wizard {
54+
55+
private Tobacco tobacco;
56+
57+
public void setTobacco(Tobacco tobacco) {
58+
this.tobacco = tobacco;
59+
}
60+
61+
@Override
62+
public void smoke() {
63+
tobacco.smoke(this);
64+
}
65+
}

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

+14-11
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();
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+
}

event-driven-architecture/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ Use an Event-driven architecture when
3232

3333
## Credits
3434

35-
* [Event-driven architecture - Wikipedia](http://www.computerweekly.com/feature/Write-through-write-around-write-back-Cache-explained)
35+
* [Event-driven architecture - Wikipedia](https://en.wikipedia.org/wiki/Event-driven_architecture)
3636
* [Fundamental Components of an Event-Driven Architecture](http://giocc.com/fundamental-components-of-an-event-driven-architecture.html)
3737
* [Real World Applications/Event Driven Applications](https://wiki.haskell.org/Real_World_Applications/Event_Driven_Applications)
3838
* [Event-driven architecture definition](http://searchsoa.techtarget.com/definition/event-driven-architecture)

0 commit comments

Comments
 (0)