Compare commits

..

177 Commits

Author SHA1 Message Date
Ilkka Seppälä
d793160bce Reach milestone 1.16.0 2017-06-30 20:31:31 +03:00
Ilkka Seppälä
b65b89baaf Merge pull request #593 from Harshrajsinh/hotfix/readme-update
Refactored Function<Integer,T> to IntFunction<T>
2017-06-14 22:48:33 +03:00
Harshrajsinh Thakor
edea7d2220 Refactored Function<Integer,T> to IntFunction<T>. 2017-06-14 11:58:05 -07:00
Ilkka Seppälä
bf8bc4df4b Merge pull request #589 from Harshrajsinh/hotfix/readme-update
Refactored the Functional Interface to the Specialized Primitive Functional Interface
2017-06-14 20:19:44 +03:00
Ilkka Seppälä
d13635533a Merge pull request #588 from prafful1/master
Few additions in readme
2017-06-14 20:14:09 +03:00
Ilkka Seppälä
37cd4cbb3b Merge pull request #585 from fluency03/master
add RxJava to Real world examples of observer
2017-06-14 20:12:32 +03:00
Ilkka Seppälä
3d29755842 Add license headers 2017-06-11 19:37:52 +03:00
Ilkka Seppälä
5b47d48fc2 Merge pull request #573 from SrdjanPaunovic/extension-objects
#541 Extension objects pattern
2017-06-11 19:26:47 +03:00
Harshrajsinh Thakor
73934e25e5 Refactored the Functional Interface to the Specialized Functional
Interface.
2017-06-09 17:17:56 -07:00
prafful1
41d487d32a Few additions in readme
Adding Consequences and General usage of Adapter Pattern section to the Readme doc.
2017-06-07 20:22:15 +05:30
Srđan Paunović
a0916aab6c Update README.md 2017-06-04 21:12:30 +02:00
SrdjanPaunovic
863589ed29 Create meaningful JavaDocs instead of author names 2017-06-04 21:09:17 +02:00
SrdjanPaunovic
85acb1cf6c Replase system.println with logger 2017-06-04 20:49:15 +02:00
Srđan Paunović
09fb79099f Merge branch 'master' into extension-objects 2017-06-02 21:50:00 +02:00
SrdjanPaunovic
22a7c15acc Reset execute-around/pom.xml from master 2017-06-02 21:42:59 +02:00
Chang Liu
0182b840af add RxJava to Real world examples of observer 2017-05-30 13:22:52 +02:00
Ilkka Seppälä
f87249e03b Add missing license headers 2017-05-29 21:53:05 +03:00
Ilkka Seppälä
dd0ca2d16e Merge pull request #572 from 4lexis/master
#567 Marker Interface pull request
2017-05-29 21:41:27 +03:00
Aleksandar Dudukovic
f2e35ec03d #567 fix version and javadoc 2017-05-23 01:38:02 +02:00
Ilkka Seppälä
ae1d9cf7a8 Add missing license headers 2017-05-21 09:19:29 +03:00
Ilkka Seppälä
1bbc597671 Merge pull request #575 from prafful1/master
Few additions in README
2017-05-21 09:11:00 +03:00
Ilkka Seppälä
4d6467e435 Merge pull request #569 from kapinuss/master
Adding Setter Dependency Injection as a second subtype of DI
2017-05-21 09:02:18 +03:00
kapinuss
17039dc5e9 Update AdvancedSorceressTest.java 2017-05-18 20:27:16 +03:00
kapinuss
30dcbee2cc Update AdvancedSorceress.java 2017-05-18 20:26:47 +03:00
kapinuss
08901f3c26 Update AdvancedSorceress.java 2017-05-18 20:23:05 +03:00
kapinuss
c746004f73 Update AdvancedSorceress.java 2017-05-18 19:13:45 +03:00
kapinuss
09aa44ddcb Update App.java 2017-05-18 19:13:09 +03:00
Ilkka Seppälä
992e76ac61 Merge pull request #566 from qpi/master
Event Queue pattern
2017-05-16 21:59:50 +03:00
Mihály Kuprivecz
167a43f72e updated model.png 2017-05-15 11:04:41 +02:00
Mihály Kuprivecz
fe1e45bd69 some fixes 2017-05-15 10:40:12 +02:00
Ilkka Seppälä
4b32fb65da Merge pull request #571 from ytian90/master
Fix Block 3 Case in Feature Toggle Design Pattern
2017-05-15 08:13:28 +03:00
Mihály Kuprivecz
0546223bba quick fix 2017-05-11 21:44:07 +02:00
Mihály Kuprivecz
857902ab95 compatibility fix
When the system is not capable to play the sound, do not throw
exception, just log it. For example on Linux there are several issues to
play sound and there are no workarounds for that :(
2017-05-11 21:41:25 +02:00
Ilkka Seppälä
0b57edd211 Merge pull request #568 from brandon-d-mckay/patch-1
Fix incorrect URL
2017-05-09 23:10:31 +03:00
prafful1
fb26d42b51 Few additions in README
Added few more points in applicability and also some use cases and consequences.
2017-05-07 13:28:54 +05:30
Srđan Paunović
6857486f27 #541 Create README.md file 2017-05-04 12:33:25 +02:00
Aleksandar Dudukovic
1abd96a9c8 #567 checkstyle fix 2017-05-03 22:09:47 +02:00
SrdjanPaunovic
08c4202852 #541 fix checkstyle errors 2017-05-03 22:08:04 +02:00
Aleksandar Dudukovic
6ecf994258 #567 pom.xml align fix 2017-05-03 21:42:15 +02:00
SrdjanPaunovic
0687a3f9f8 #541 pom.xml fix align 2017-05-03 21:40:54 +02:00
Aleksandar Dudukovic
64337dff06 merge fix 2017-05-03 21:07:49 +02:00
4lexis
34b09c75ec #567 added .gitignore 2017-05-03 20:51:51 +02:00
4lexis
66c6f30c1c #567 Updated pom.xml 2017-05-03 20:34:27 +02:00
Aleksandar Dudukovic
e413b116ac Merge branch 'master' of https://github.com/4lexis/java-design-patterns 2017-05-03 19:57:57 +02:00
Aleksandar Dudukovic
6e0b3e37ea updated pom.xml 2017-05-03 19:57:14 +02:00
4lexis
be3f4dce50 Update Picture 2017-05-03 19:49:25 +02:00
4lexis
6d87f63ed5 Added README.md 2017-05-03 19:48:29 +02:00
SrdjanPaunovic
c92a8daeda Class diagram & fix relation between units 2017-05-03 17:59:54 +02:00
Aleksandar Dudukovic
20062faae6 All without Readme and pumlid 2017-05-03 17:50:35 +02:00
SrdjanPaunovic
678524704c Test done 2017-05-03 13:21:13 +02:00
kapinuss
2b229d8ea1 Update AdvancedSorceressTest.java 2017-05-02 20:38:08 +03:00
kapinuss
03aa99c55f Update AdvancedSorceressTest.java 2017-05-02 20:18:05 +03:00
ytian90
20a4c054a7 Merge branch 'master' of github.com:ytian90/java-design-patterns 2017-04-30 00:20:48 -07:00
ytian90
3ed6cc19d2 fix Block 3 case 2017-04-30 00:19:50 -07:00
Yu Tian
87a9387e62 Merge pull request #1 from iluwatar/master
Rebase
2017-04-29 16:03:26 -07:00
Aleksandar Dudukovic
8530d01e10 code implemented 2017-04-29 16:35:57 +02:00
Aleksandar Dudukovic
a34e7be9c2 check commit 2017-04-28 19:35:19 +02:00
Mihály Kuprivecz
ee3744cb0a added travis supported sound files 2017-04-28 13:47:56 +02:00
Mihály Kuprivecz
2830a407ba some fixes for testing in event queue 2017-04-28 12:19:57 +02:00
kapinuss
ccfb6709c7 Add files via upload 2017-04-28 13:00:01 +03:00
kapinuss
a1c5131304 Add files via upload 2017-04-28 12:59:09 +03:00
Mihály Kuprivecz
44401988d1 pom.xml fixes 2017-04-28 11:32:35 +02:00
Mihály Kuprivecz
6e8eaf7593 adding some test cases for the event queue 2017-04-28 10:38:40 +02:00
SrdjanPaunovic
645e91ed23 first version of pattern without Tests 2017-04-27 13:58:58 +02:00
Brandon D. McKay
9e7e8a64f6 Fix incorrect URL 2017-04-26 04:22:44 -04:00
Mihály Kuprivecz
152b2762c3 read.me and the diagram is added 2017-04-22 17:16:38 +02:00
Ilkka Seppälä
82b9f4fea7 Merge pull request #544 from kemitix/data-bus
[WIP] Data Bus
2017-04-22 15:17:31 +03:00
Paul Campbell
ff8d854a8d #467 data-bus: README.md: clean up 2017-04-16 06:51:10 +01:00
Ilkka Seppälä
31f4b15e86 Merge pull request #565 from kapinuss/patch-1
Update ObjectPool.java
2017-04-16 08:17:56 +03:00
Ilkka Seppälä
cd54cf5512 Merge pull request #563 from sunilmogadati/SonarQubeBlockerBugs
#507 SonarQube blocker severity bugs
2017-04-16 08:13:26 +03:00
kapinuss
798aee47b3 Update ObjectPool.java 2017-04-12 18:59:43 +03:00
Mihály Kuprivecz
dce767c1c5 first commit 2017-04-06 23:48:15 +02:00
Sunil Mogadati
60ebcc56f8 #507 SonarQube blocker severity bugs 2017-04-01 20:55:47 -06:00
Paul Campbell
2643dfa0b8 #467 data-bus: App: add notes about this implementation of the patter 2017-04-01 19:33:54 +01:00
Paul Campbell
c96ebcb197 #467 data-bus: App: add description of the pattern 2017-04-01 19:26:41 +01:00
Paul Campbell
b72d545349 #467 data-bus: members: StatusMemberTest: added 2017-04-01 19:17:09 +01:00
Paul Campbell
311bb79870 #467 data-bus: members: StatusMember: records start and stop times 2017-04-01 19:16:55 +01:00
Paul Campbell
f495a88e91 #467 data-bus: members: MessageCollectorMemberTest: added 2017-04-01 19:05:19 +01:00
Paul Campbell
8b0c14cae0 Counter doesn't count anything. Added ability to collect the messages
from the MessageData that it receives.
2017-04-01 19:05:09 +01:00
Paul Campbell
b7a6a018e0 #467 data-bus: DataBusTest: added 2017-04-01 18:36:04 +01:00
Paul Campbell
46e0fa4825 #467 data-bus: pom.xml: add mockito dependency 2017-04-01 18:35:40 +01:00
Paul Campbell
86009f2261 #467 data-bus: add missing javadoc 2017-04-01 18:12:56 +01:00
Paul Campbell
146f367188 #467 data-bus: remove lombok 2017-04-01 18:12:32 +01:00
Paul Campbell
bc4d029a87 #467 data-bus: pom.xml: remove surefire plugin 2017-04-01 18:04:44 +01:00
Paul Campbell
960eee3d43 #467 update version 2017-04-01 18:04:16 +01:00
Paul Campbell
30315e788f Merge remote-tracking branch 'upstream/master' into data-bus
* upstream/master: (67 commits)
  Set version for next development iteration
  Reached milestone 1.15.0
  #539 Checkstyle fix
  #539 More Checkstyle fixes
  #539 Checkstyle fixes
  #497 Add missing puml and license headers
  #77 Add missing license header
  Work on #190: urm/puml updates * added pumlid where it was missing and possible * removed pumlid where it generated a bad image * regenerated some incorrect puml's * added 'left to right direction' puml prefix to some diagrams to improve the automatic layouting
  Removed AvoidStarImport Rule Added JavaDocType Rule
  Update App.java
  Update App.java
  Update after changes from review. Additional improvements.
  Pom.xml files fixed.
  #66 Balking Pattern
  fixed pmd violation
  fixed checkstyle violations
  diagrams added
  comments, tests and description
  changed parent POM version #69
  apply changes from review #69
  ...

# Conflicts:
#	pom.xml
2017-04-01 18:03:02 +01:00
Ilkka Seppälä
073d06c0ae Set version for next development iteration 2017-04-01 17:15:59 +03:00
Ilkka Seppälä
8fea969912 Reached milestone 1.15.0 2017-04-01 17:14:02 +03:00
Ilkka Seppälä
139876f96a #539 Checkstyle fix 2017-04-01 15:51:01 +03:00
Ilkka Seppälä
f3c4640d12 #539 More Checkstyle fixes 2017-04-01 15:43:36 +03:00
Ilkka Seppälä
c1c4411957 #539 Checkstyle fixes 2017-04-01 15:34:13 +03:00
Ilkka Seppälä
3f272bf291 Merge pull request #548 from muditporwal/master
Checkstyle improvements #539 : Added JavaDocType Rule
2017-04-01 14:45:52 +03:00
Ilkka Seppälä
2921448f8b #497 Add missing puml and license headers 2017-04-01 14:25:13 +03:00
Ilkka Seppälä
952c207a62 Merge pull request #546 from Crossy147/converter
Converter
2017-04-01 14:17:37 +03:00
Ilkka Seppälä
6ba4d7be98 #77 Add missing license header 2017-04-01 12:39:06 +03:00
Ilkka Seppälä
854101bb49 Merge pull request #529 from thomasoss/master
Thread Local Storage issue #77
2017-04-01 12:29:03 +03:00
Kamil Pietruszka
a5ab5c7d8e Merge branch 'master' into converter 2017-03-26 23:50:08 +02:00
Ilkka Seppälä
286d6c3a4c Merge pull request #536 from Rzeposlaw/master
#66 Balking Pattern
2017-03-26 15:00:21 +03:00
NooBxGockeL
c2a7b902a9 Work on #190: urm/puml updates
* added pumlid where it was missing and possible
* removed pumlid where it generated a bad image
* regenerated some incorrect puml's
* added 'left to right direction' puml prefix to some diagrams to improve the automatic layouting
2017-03-25 22:07:10 +01:00
Mudit Porwal
09585c3874 Removed AvoidStarImport Rule
Added JavaDocType Rule
2017-03-22 01:16:02 +08:00
Thomas
f84c4c1611 Update App.java
Correction of correction ;-)
2017-03-18 10:02:37 +01:00
Thomas
2c2d874ac8 Update App.java
Correction of error detected by maven-pmd-plugin.
2017-03-18 10:00:13 +01:00
Kamil Pietruszka
72a0765ec6 Merge branch 'master' into converter 2017-03-16 12:27:05 +01:00
Katarzyna Rzepecka
cca4d5a670 Update after changes from review. Additional improvements. 2017-03-13 01:33:14 +01:00
Katarzyna Rzepecka
bb4a1bdc05 Pom.xml files fixed. 2017-03-13 01:16:26 +01:00
Katarzyna Rzepecka
1aed5905d7 #66 Balking Pattern 2017-03-13 01:15:39 +01:00
Ilkka Seppälä
175e9f58c1 Merge pull request #537 from robertt240/master
guarded suspension pattern #69
2017-03-12 10:36:27 +02:00
Kamil Pietruszka
77a534385c fixed pmd violation 2017-03-11 13:35:55 +01:00
Kamil Pietruszka
2ed7acbc31 fixed checkstyle violations 2017-03-11 13:02:29 +01:00
Kamil Pietruszka
3a243eee6e diagrams added 2017-03-11 12:47:58 +01:00
Kamil Pietruszka
8632bafcd7 comments, tests and description 2017-03-11 12:24:48 +01:00
Robert Kasperczyk
449aed1a53 changed parent POM version #69 2017-03-11 12:02:04 +01:00
Robert Kasperczyk
a20abae21c apply changes from review #69 2017-03-11 11:50:19 +01:00
Robert Kasperczyk
7423f47daa further POM rearragments #69 2017-03-11 11:50:19 +01:00
Robert Kasperczyk
f09578c091 further POM rearragments #69 2017-03-11 11:50:19 +01:00
Robert Kasperczyk
29715028d1 fix in POM formatting #69 2017-03-11 11:50:19 +01:00
Robert Kasperczyk
e5034c6ae9 guarded suspension pattern #69 2017-03-11 11:50:19 +01:00
Robert Kasperczyk
a09866d35b another readme fix 2017-03-11 11:50:19 +01:00
Robert Kasperczyk
1824b4138b readme fix 2017-03-11 11:50:19 +01:00
Robert Kasperczyk
764ff4bf53 Initial commit of guarded suspension design pattern 2017-03-11 11:50:19 +01:00
Kamil Pietruszka
e8b634c33e java docs added 2017-03-10 20:59:24 +01:00
Kamil Pietruszka
8871f788d2 #497 Converter pattern implementation 2017-03-10 20:08:58 +01:00
Kamil Pietruszka
e9c54011e9 Merge remote-tracking branch 'refs/remotes/iluwatar/master' 2017-03-08 16:50:04 +01:00
Paul Campbell
6b795e52c3 #467 data-bus: README.md: updated for data-bus 2017-03-05 19:43:57 +00:00
Paul Campbell
b5bdf2d7d7 #467 data-bus: etc: add urm diagrams 2017-03-05 19:43:26 +00:00
Paul Campbell
3fd6887975 #467 data-bus: implement pattern 2017-03-05 18:55:34 +00:00
Paul Campbell
eecffb0ea5 #467 data-bus: add stub 2017-03-03 19:58:03 +00:00
Thomas
fd7107694a Update pom.xml
Changed
    <version>1.14.0-SNAPSHOT</version>
to
    <version>1.15.0-SNAPSHOT</version>
2017-02-18 15:16:15 +01:00
Ilkka Seppälä
cca4760f69 #190 Rename package for Queue-Based Load Leveling pattern 2017-02-12 00:33:30 +02:00
Ilkka Seppälä
a1ff55b462 #190 Regenerate puml files 2017-02-11 21:46:56 +02:00
Thomas
ddac9dc6cb Add files via upload
Changed the classname part "runnable" to "callable"
2017-01-29 12:34:13 +01:00
Thomas
2bbf84233e Delete DateFormatRunnableTestMultiThread.java 2017-01-29 12:32:44 +01:00
Thomas
82f8460243 Delete DateFormatRunnableTestIncorrectDateFormat.java 2017-01-29 12:32:32 +01:00
Thomas
59ea20745f Delete DateFormatRunnableTest.java 2017-01-29 12:32:21 +01:00
Thomas
3d3dd58501 Update pom.xml
removed errors caused by copy code from master
2017-01-29 12:08:11 +01:00
Thomas
3324e1bc43 Update pom.xml
added  <module>tls</module>
2017-01-29 12:01:32 +01:00
Thomas
6202f3ab44 Add files via upload 2017-01-29 11:42:16 +01:00
Thomas
453862cfc2 Delete AppTest.java 2017-01-29 11:41:46 +01:00
Thomas
a8e2c157de Add files via upload
Test reworked completely. AppTest seperated.
2017-01-29 11:41:13 +01:00
Thomas
080965fb17 Delete DateFormatUglyRunnable.java 2017-01-29 11:38:49 +01:00
Thomas
7200329a6b Add files via upload
Rework replaces previous version completely. Using ExecutorService. Use of result object instead of static variables. Ugly example is left out.
2017-01-29 11:38:20 +01:00
Thomas
f170aaa42b Delete DateFormatRunnable.java 2017-01-29 11:34:12 +01:00
Thomas
c598748549 Delete AppUgly.java 2017-01-29 11:33:59 +01:00
Thomas
ed11c4c4f9 Delete App.java 2017-01-29 11:33:40 +01:00
Thomas
283f198ba8 Delete empty 2017-01-29 11:32:26 +01:00
Thomas
15913d6382 Add files via upload
new uml diagramm after changes to the java classes
2017-01-29 11:31:06 +01:00
Thomas
65e047974c Create empty 2017-01-29 11:29:19 +01:00
Thomas
c529e35791 Delete tls.urm.puml 2017-01-29 11:28:27 +01:00
Thomas
3342851005 Delete tls.ucls 2017-01-29 11:28:17 +01:00
Thomas
c167f87ce5 Delete tls.png 2017-01-29 11:27:46 +01:00
Thomas
e8fc3427c6 Update README.md
no change of content. Improved text
2017-01-29 11:23:33 +01:00
Thomas
26b79a5382 Update README.md
Some additonal description, deleted wrong pumlid
2017-01-29 11:16:45 +01:00
Ilkka Seppälä
9ec0935a1c Merge pull request #532 from leogtzr/master
Adding initialization-on-demand idiom and noninstantiable class instead of interface constant idiom
2017-01-23 08:21:07 +02:00
leogtzr
c6d0d28557 Reverting initialization on demand holder idiom. 2017-01-22 11:06:57 -07:00
leogtzr
e26215578c Changing code to use interfaces instead of implementations. 2017-01-21 15:49:29 -07:00
leogtzr
d6fc28e120 Changing code to use interfaces instead of implementations. 2017-01-21 15:47:54 -07:00
Ilkka Seppälä
115a85301c #525 Add link to proxy blog 2017-01-21 13:37:16 +02:00
Ilkka Seppälä
29c5b80f19 #525 Add link to Queue-Based Load Leveling blog 2017-01-21 12:56:42 +02:00
Ilkka Seppälä
0c8bb1c22e Set version for next development iteration 2016-12-31 10:24:26 +02:00
leogtzr
6292690250 Changing constant interface pattern with a Noninstantiable class 2016-12-24 14:49:41 -07:00
leogtzr
20b1c2bd49 Adding initialization on demand holder idiom. 2016-12-24 00:49:46 -07:00
Thomas
e210e4ed62 Add files via upload 2016-12-16 11:17:27 +01:00
Thomas
fb98da86c4 Add files via upload 2016-12-16 11:12:43 +01:00
Thomas
c8b3c773c7 Add files via upload 2016-12-16 09:43:16 +01:00
Thomas
61b1356976 Delete emptyfile 2016-12-16 09:42:46 +01:00
Thomas
487f9ddc80 Add files via upload 2016-12-16 09:42:08 +01:00
Thomas
9b7ce556e3 Create emptyfile 2016-12-16 09:35:19 +01:00
Thomas
e17cf27e5a upload pom.xml 2016-12-15 18:33:52 +01:00
Thomas
937a1e269d delete empty file 2016-12-15 18:32:13 +01:00
Thomas
803a97237c delete empty file 2016-12-15 18:31:53 +01:00
Thomas
e64f7faa3f upload junit test 2016-12-15 18:31:20 +01:00
Thomas
15d660b117 empty file to create folder 2016-12-15 18:30:22 +01:00
Thomas
544f7fb6d1 delete empty file 2016-12-15 18:28:02 +01:00
Thomas
c4eb198a8d Upload code files 2016-12-15 18:27:28 +01:00
Thomas
7067d1ae56 Create test 2016-12-15 18:24:03 +01:00
Thomas
d80edd1ed3 Create test 2016-12-15 18:08:12 +01:00
Thomas
aeefc11f25 Merge pull request #1 from iluwatar/master
Update my local repository
2016-12-14 08:52:22 +01:00
384 changed files with 6688 additions and 592 deletions

View File

@@ -29,7 +29,7 @@
<parent> <parent>
<artifactId>java-design-patterns</artifactId> <artifactId>java-design-patterns</artifactId>
<groupId>com.iluwatar</groupId> <groupId>com.iluwatar</groupId>
<version>1.14.0</version> <version>1.16.0</version>
</parent> </parent>
<artifactId>abstract-document</artifactId> <artifactId>abstract-document</artifactId>
<dependencies> <dependencies>

View File

@@ -27,6 +27,22 @@ Use the Abstract Factory pattern when
* a system should be configured with one of multiple families of products * a system should be configured with one of multiple families of products
* a family of related product objects is designed to be used together, and you need to enforce this constraint * a family of related product objects is designed to be used together, and you need to enforce this constraint
* you want to provide a class library of products, and you want to reveal just their interfaces, not their implementations * you want to provide a class library of products, and you want to reveal just their interfaces, not their implementations
* the lifetime of the dependency is conceptually shorter than the lifetime of the consumer.
* you need a run-time value to construct a particular dependency
* you want to decide which product to call from a family at runtime.
* you need to supply one or more parameters only known at run-time before you can resolve a dependency.
## Use Cases:
* Selecting to call the appropriate implementation of FileSystemAcmeService or DatabaseAcmeService or NetworkAcmeService at runtime.
* Unit test case writing becomes much easier
## Consequences:
* Dependency injection in java hides the service class dependencies that can lead to runtime errors that would have been caught at compile time.
## Real world examples ## Real world examples

View File

@@ -29,7 +29,7 @@
<parent> <parent>
<groupId>com.iluwatar</groupId> <groupId>com.iluwatar</groupId>
<artifactId>java-design-patterns</artifactId> <artifactId>java-design-patterns</artifactId>
<version>1.14.0</version> <version>1.16.0</version>
</parent> </parent>
<artifactId>abstract-factory</artifactId> <artifactId>abstract-factory</artifactId>
<dependencies> <dependencies>

View File

@@ -28,6 +28,9 @@ import static org.junit.Assert.assertTrue;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
/**
* Test for abstract factory
*/
public class AbstractFactoryTest { public class AbstractFactoryTest {
private App app = new App(); private App app = new App();

View File

@@ -21,6 +21,9 @@ incompatible interfaces.
![alt text](./etc/adapter.png "Adapter") ![alt text](./etc/adapter.png "Adapter")
## General usage of Adapter Pattern:
+ Wrappers used to adopt 3rd parties libraries and frameworks - most of the applications using third party libraries use adapters as a middle layer between the application and the 3rd party library to decouple the application from the library. If another library has to be used only an adapter for the new library is required without having to change the application code.
## Applicability ## Applicability
Use the Adapter pattern when Use the Adapter pattern when
@@ -28,6 +31,19 @@ Use the Adapter pattern when
* you want to create a reusable class that cooperates with unrelated or unforeseen classes, that is, classes that don't necessarily have compatible interfaces * you want to create a reusable class that cooperates with unrelated or unforeseen classes, that is, classes that don't necessarily have compatible interfaces
* you need to use several existing subclasses, but it's impractical to adapt their interface by subclassing every one. An object adapter can adapt the interface of its parent class. * you need to use several existing subclasses, but it's impractical to adapt their interface by subclassing every one. An object adapter can adapt the interface of its parent class.
## Consequences:
Class and object adapters have different trade-offs. A class adapter
* adapts Adaptee to Target by committing to a concrete Adaptee class. As a consequence, a class adapter wont work when we want to adapt a class and all its subclasses.
* lets Adapter override some of Adaptees behavior, since Adapter is a subclass of Adaptee.
* introduces only one object, and no additional pointer indirection is needed to get to the adaptee.
An object adapter
* lets a single Adapter work with many Adaptees—that is, the Adaptee itself and all of its subclasses (if any). The Adapter can also add functionality to all Adaptees at once.
* makes it harder to override Adaptee behavior. It will require subclassing Adaptee and making Adapter refer to the subclass rather than the Adaptee itself.
## Real world examples ## Real world examples
* [java.util.Arrays#asList()](http://docs.oracle.com/javase/8/docs/api/java/util/Arrays.html#asList%28T...%29) * [java.util.Arrays#asList()](http://docs.oracle.com/javase/8/docs/api/java/util/Arrays.html#asList%28T...%29)

View File

@@ -29,7 +29,7 @@
<parent> <parent>
<groupId>com.iluwatar</groupId> <groupId>com.iluwatar</groupId>
<artifactId>java-design-patterns</artifactId> <artifactId>java-design-patterns</artifactId>
<version>1.14.0</version> <version>1.16.0</version>
</parent> </parent>
<artifactId>adapter</artifactId> <artifactId>adapter</artifactId>
<dependencies> <dependencies>

View File

@@ -29,7 +29,7 @@
<parent> <parent>
<artifactId>aggregator-microservices</artifactId> <artifactId>aggregator-microservices</artifactId>
<groupId>com.iluwatar</groupId> <groupId>com.iluwatar</groupId>
<version>1.14.0</version> <version>1.16.0</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>

View File

@@ -25,6 +25,9 @@ package com.iluwatar.aggregator.microservices;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* Spring Boot EntryPoint Class
*/
@SpringBootApplication @SpringBootApplication
public class App { public class App {

View File

@@ -22,15 +22,18 @@
*/ */
package com.iluwatar.aggregator.microservices; package com.iluwatar.aggregator.microservices;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.when;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.mockito.InjectMocks; import org.mockito.InjectMocks;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.MockitoAnnotations; import org.mockito.MockitoAnnotations;
import static org.junit.Assert.assertEquals; /**
import static org.mockito.Mockito.when; * Test Aggregation of domain objects
*/
public class AggregatorTest { public class AggregatorTest {
@InjectMocks @InjectMocks

View File

@@ -29,7 +29,7 @@
<parent> <parent>
<artifactId>aggregator-microservices</artifactId> <artifactId>aggregator-microservices</artifactId>
<groupId>com.iluwatar</groupId> <groupId>com.iluwatar</groupId>
<version>1.14.0</version> <version>1.16.0</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>

View File

@@ -26,6 +26,9 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
/**
* Controller providing endpoints to retrieve information about products
*/
@RestController @RestController
public class InformationController { public class InformationController {

View File

@@ -25,6 +25,9 @@ package com.iluwatar.information.microservice;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
/**
* Test for Information Rest Controller
*/
public class InformationControllerTest { public class InformationControllerTest {
@Test @Test

View File

@@ -29,7 +29,7 @@
<parent> <parent>
<artifactId>aggregator-microservices</artifactId> <artifactId>aggregator-microservices</artifactId>
<groupId>com.iluwatar</groupId> <groupId>com.iluwatar</groupId>
<version>1.14.0</version> <version>1.16.0</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>

View File

@@ -26,6 +26,9 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
/**
* Controller providing endpoints to retrieve product inventories
*/
@RestController @RestController
public class InventoryController { public class InventoryController {

View File

@@ -25,8 +25,10 @@ package com.iluwatar.inventory.microservice;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
/**
* Test Inventory Rest Controller
*/
public class InventoryControllerTest { public class InventoryControllerTest {
@Test @Test
public void testGetProductInventories() throws Exception { public void testGetProductInventories() throws Exception {
InventoryController inventoryController = new InventoryController(); InventoryController inventoryController = new InventoryController();

View File

@@ -29,7 +29,7 @@
<parent> <parent>
<artifactId>java-design-patterns</artifactId> <artifactId>java-design-patterns</artifactId>
<groupId>com.iluwatar</groupId> <groupId>com.iluwatar</groupId>
<version>1.14.0</version> <version>1.16.0</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<artifactId>aggregator-microservices</artifactId> <artifactId>aggregator-microservices</artifactId>

View File

@@ -29,7 +29,7 @@
<parent> <parent>
<artifactId>api-gateway</artifactId> <artifactId>api-gateway</artifactId>
<groupId>com.iluwatar</groupId> <groupId>com.iluwatar</groupId>
<version>1.14.0</version> <version>1.16.0</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<artifactId>api-gateway-service</artifactId> <artifactId>api-gateway-service</artifactId>

View File

@@ -35,7 +35,7 @@ import java.io.IOException;
* An adapter to communicate with the Image microservice * An adapter to communicate with the Image microservice
*/ */
@Component @Component
public class ImageClientImpl implements ImageClient{ public class ImageClientImpl implements ImageClient {
/** /**
* Makes a simple HTTP Get request to the Image microservice * Makes a simple HTTP Get request to the Image microservice
* @return The path to the image * @return The path to the image

View File

@@ -35,7 +35,7 @@ import java.io.IOException;
* An adapter to communicate with the Price microservice * An adapter to communicate with the Price microservice
*/ */
@Component @Component
public class PriceClientImpl implements PriceClient{ public class PriceClientImpl implements PriceClient {
/** /**
* Makes a simple HTTP Get request to the Price microservice * Makes a simple HTTP Get request to the Price microservice
* @return The price of the product * @return The price of the product

View File

@@ -22,15 +22,18 @@
*/ */
package com.iluwatar.api.gateway; package com.iluwatar.api.gateway;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.when;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.mockito.InjectMocks; import org.mockito.InjectMocks;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.MockitoAnnotations; import org.mockito.MockitoAnnotations;
import static org.junit.Assert.assertEquals; /**
import static org.mockito.Mockito.when; * Test API Gateway Pattern
*/
public class ApiGatewayTest { public class ApiGatewayTest {
@InjectMocks @InjectMocks

View File

@@ -29,7 +29,7 @@
<parent> <parent>
<artifactId>api-gateway</artifactId> <artifactId>api-gateway</artifactId>
<groupId>com.iluwatar</groupId> <groupId>com.iluwatar</groupId>
<version>1.14.0</version> <version>1.16.0</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>

View File

@@ -25,6 +25,9 @@ package com.iluwatar.image.microservice;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
/**
* Test for Image Rest Controller
*/
public class ImageControllerTest { public class ImageControllerTest {
@Test @Test
public void testGetImagePath() { public void testGetImagePath() {

View File

@@ -29,7 +29,7 @@
<parent> <parent>
<artifactId>java-design-patterns</artifactId> <artifactId>java-design-patterns</artifactId>
<groupId>com.iluwatar</groupId> <groupId>com.iluwatar</groupId>
<version>1.14.0</version> <version>1.16.0</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<artifactId>api-gateway</artifactId> <artifactId>api-gateway</artifactId>

View File

@@ -29,7 +29,7 @@
<parent> <parent>
<artifactId>api-gateway</artifactId> <artifactId>api-gateway</artifactId>
<groupId>com.iluwatar</groupId> <groupId>com.iluwatar</groupId>
<version>1.14.0</version> <version>1.16.0</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>

View File

@@ -25,6 +25,10 @@ package com.iluwatar.price.microservice;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
/**
* Test for Price Rest Controller
*/
public class PriceControllerTest { public class PriceControllerTest {
@Test @Test
public void testgetPrice() { public void testgetPrice() {

View File

@@ -29,7 +29,7 @@
<parent> <parent>
<groupId>com.iluwatar</groupId> <groupId>com.iluwatar</groupId>
<artifactId>java-design-patterns</artifactId> <artifactId>java-design-patterns</artifactId>
<version>1.14.0</version> <version>1.16.0</version>
</parent> </parent>
<artifactId>async-method-invocation</artifactId> <artifactId>async-method-invocation</artifactId>
<dependencies> <dependencies>

View File

@@ -25,8 +25,8 @@ package com.iluwatar.async.method.invocation;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
/** /**
*
* AsyncResult interface * AsyncResult interface
* @param <T> parameter returned when getValue is invoked
*/ */
public interface AsyncResult<T> { public interface AsyncResult<T> {

27
balking/README.md Normal file
View File

@@ -0,0 +1,27 @@
---
layout: pattern
title: Balking
folder: balking
permalink: /patterns/balking/
categories: Concurrency
tags:
- Java
- Difficulty-Beginner
---
## Intent
Balking Pattern is used to prevent an object from executing certain code if it is an
incomplete or inappropriate state
![alt text](./etc/balking.png "Balking")
## Applicability
Use the Balking pattern when
*you want to invoke an action on an object only when it is in a particular state
*objects are generally only in a state that is prone to balking temporarily
but for an unknown amount of time
## Related patterns
* Guarded Suspendion Pattern
* Double Checked Locking Pattern

BIN
balking/etc/balking.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 21 KiB

46
balking/etc/balking.ucls Normal file
View File

@@ -0,0 +1,46 @@
<?xml version="1.0" encoding="UTF-8"?>
<class-diagram version="1.1.13" icons="true" automaticImage="PNG" always-add-relationships="false"
generalizations="true" realizations="true" associations="true" dependencies="false" nesting-relationships="true"
router="FAN">
<class id="1" language="java" name="com.iluwatar.balking.App" project="balking"
file="/balking/src/main/java/com/iluwatar/balking/App.java" binary="false" corner="BOTTOM_RIGHT">
<position height="113" width="114" x="135" y="103"/>
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
<operations public="true" package="true" protected="true" private="true" static="true"/>
</display>
</class>
<class id="2" language="java" name="com.iluwatar.balking.WashingMachine" project="balking"
file="/balking/src/main/java/com/iluwatar/balking/WashingMachine.java" binary="false" corner="BOTTOM_RIGHT">
<position height="149" width="268" x="289" y="103"/>
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
<operations public="true" package="true" protected="true" private="true" static="true"/>
</display>
</class>
<enumeration id="3" language="java" name="com.iluwatar.balking.WashingMachineState" project="balking"
file="/balking/src/main/java/com/iluwatar/balking/WashingMachineState.java" binary="false" corner="BOTTOM_RIGHT">
<position height="113" width="192" x="289" y="292"/>
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
<operations public="true" package="true" protected="true" private="true" static="true"/>
</display>
</enumeration>
<association id="4">
<end type="SOURCE" refId="2" navigable="false">
<attribute id="5" name="washingMachineState"/>
<multiplicity id="6" minimum="0" maximum="1"/>
</end>
<end type="TARGET" refId="3" navigable="true"/>
<display labels="true" multiplicity="true"/>
</association>
<classifier-display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
<operations public="true" package="true" protected="true" private="true" static="true"/>
</classifier-display>
<association-display labels="true" multiplicity="true"/>
</class-diagram>

View File

@@ -0,0 +1,24 @@
@startuml
package com.iluwatar.balking {
class App {
- LOGGER : Logger {static}
+ App()
+ main(args : String[]) {static}
}
class WashingMachine {
- LOGGER : Logger {static}
- washingMachineState : WashingMachineState
+ WashingMachine()
+ endOfWashing()
+ getWashingMachineState() : WashingMachineState
+ wash()
}
enum WashingMachineState {
+ ENABLED {static}
+ WASHING {static}
+ valueOf(name : String) : WashingMachineState {static}
+ values() : WashingMachineState[] {static}
}
}
WashingMachine --> "-washingMachineState" WashingMachineState
@enduml

46
balking/pom.xml Normal file
View File

@@ -0,0 +1,46 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
The MIT License
Copyright (c) 2014 Ilkka Seppälä
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>java-design-patterns</artifactId>
<groupId>com.iluwatar</groupId>
<version>1.16.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>balking</artifactId>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,65 @@
/**
* The MIT License
* Copyright (c) 2014 Ilkka Seppälä
* <p>
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* <p>
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* <p>
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.balking;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
/**
* In Balking Design Pattern if an objects method is invoked when it is in an inappropriate state,
* then the method will return without doing anything. Objects that use this pattern are generally only in a
* state that is prone to balking temporarily but for an unknown amount of time
*
* In this example implementation WashingMachine is an object that has two states
* in which it can be: ENABLED and WASHING. If the machine is ENABLED
* the state is changed into WASHING that any other thread can't invoke this action on this and then do the job.
* On the other hand if it have been already washing and any other thread execute wash()
* it can't do that once again and returns doing nothing.
*/
public class App {
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
/**
* @param args the command line arguments - not used
*/
public static void main(String... args) {
final WashingMachine washingMachine = new WashingMachine();
ExecutorService executorService = Executors.newFixedThreadPool(3);
for (int i = 0; i < 3; i++) {
executorService.execute(washingMachine::wash);
}
executorService.shutdown();
try {
executorService.awaitTermination(10, TimeUnit.SECONDS);
} catch (InterruptedException ie) {
LOGGER.error("ERROR: Waiting on executor service shutdown!");
}
}
}

View File

@@ -0,0 +1,76 @@
/**
* The MIT License
* Copyright (c) 2014 Ilkka Seppälä
* <p>
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* <p>
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* <p>
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.balking;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Washing machine class
*/
public class WashingMachine {
private static final Logger LOGGER = LoggerFactory.getLogger(WashingMachine.class);
private WashingMachineState washingMachineState;
public WashingMachine() {
washingMachineState = WashingMachineState.ENABLED;
}
public WashingMachineState getWashingMachineState() {
return washingMachineState;
}
/**
* Method responsible for washing
* if the object is in appropriate state
*/
public void wash() {
synchronized (this) {
LOGGER.info("{}: Actual machine state: {}", Thread.currentThread().getName(), getWashingMachineState());
if (washingMachineState == WashingMachineState.WASHING) {
LOGGER.error("ERROR: Cannot wash if the machine has been already washing!");
return;
}
washingMachineState = WashingMachineState.WASHING;
}
LOGGER.info("{}: Doing the washing", Thread.currentThread().getName());
try {
Thread.sleep(50);
} catch (InterruptedException ie) {
ie.printStackTrace();
}
endOfWashing();
}
/**
* Method responsible of ending the washing
* by changing machine state
*/
public synchronized void endOfWashing() {
washingMachineState = WashingMachineState.ENABLED;
LOGGER.info("{}: Washing completed.", Thread.currentThread().getId());
}
}

View File

@@ -0,0 +1,32 @@
/**
* The MIT License
* Copyright (c) 2014 Ilkka Seppälä
* <p>
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* <p>
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* <p>
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.balking;
/**
* WashingMachineState enum describes in which state machine is,
* it can be enabled and ready to work as well as during washing
*/
public enum WashingMachineState {
ENABLED, WASHING
}

View File

@@ -0,0 +1,39 @@
/**
* The MIT License
* Copyright (c) 2014 Ilkka Seppälä
* <p>
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* <p>
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* <p>
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.balking;
import org.junit.Test;
/**
* Application test
*/
public class AppTest {
@Test
public void main() throws Exception {
String[] args = {};
App.main(args);
}
}

View File

@@ -0,0 +1,65 @@
/**
* The MIT License
* Copyright (c) 2014 Ilkka Seppälä
* <p>
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* <p>
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* <p>
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.balking;
import org.junit.Test;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import static org.junit.Assert.*;
/**
* Tests for {@link WashingMachine}
*/
public class WashingMachineTest {
private volatile WashingMachineState machineStateGlobal;
@Test
public void wash() throws Exception {
WashingMachine washingMachine = new WashingMachine();
ExecutorService executorService = Executors.newFixedThreadPool(2);
executorService.execute(washingMachine::wash);
executorService.execute(() -> {
washingMachine.wash();
machineStateGlobal = washingMachine.getWashingMachineState();
});
executorService.shutdown();
try {
executorService.awaitTermination(10, TimeUnit.SECONDS);
} catch (InterruptedException ie) {
ie.printStackTrace();
}
assertEquals(WashingMachineState.WASHING, machineStateGlobal);
}
@Test
public void endOfWashing() throws Exception {
WashingMachine washingMachine = new WashingMachine();
washingMachine.wash();
assertEquals(WashingMachineState.ENABLED, washingMachine.getWashingMachineState());
}
}

View File

@@ -29,7 +29,7 @@
<parent> <parent>
<groupId>com.iluwatar</groupId> <groupId>com.iluwatar</groupId>
<artifactId>java-design-patterns</artifactId> <artifactId>java-design-patterns</artifactId>
<version>1.14.0</version> <version>1.16.0</version>
</parent> </parent>
<artifactId>bridge</artifactId> <artifactId>bridge</artifactId>
<dependencies> <dependencies>

View File

@@ -86,15 +86,15 @@ package com.iluwatar.builder {
+ values() : Weapon[] {static} + values() : Weapon[] {static}
} }
} }
Hero --> "-profession" Profession
Builder ..+ Hero Builder ..+ Hero
Hero --> "-profession" Profession
Hero --> "-armor" Armor Hero --> "-armor" Armor
Builder --> "-hairColor" HairColor
Builder --> "-weapon" Weapon Builder --> "-weapon" Weapon
Builder --> "-hairType" HairType Builder --> "-hairType" HairType
Builder --> "-hairColor" HairColor
Hero --> "-hairColor" HairColor Hero --> "-hairColor" HairColor
Builder --> "-profession" Profession Builder --> "-profession" Profession
Hero --> "-weapon" Weapon
Hero --> "-hairType" HairType Hero --> "-hairType" HairType
Hero --> "-weapon" Weapon
Builder --> "-armor" Armor Builder --> "-armor" Armor
@enduml @enduml

View File

@@ -29,7 +29,7 @@
<parent> <parent>
<groupId>com.iluwatar</groupId> <groupId>com.iluwatar</groupId>
<artifactId>java-design-patterns</artifactId> <artifactId>java-design-patterns</artifactId>
<version>1.14.0</version> <version>1.16.0</version>
</parent> </parent>
<artifactId>builder</artifactId> <artifactId>builder</artifactId>
<dependencies> <dependencies>

View File

@@ -46,8 +46,8 @@ package com.iluwatar.business.delegate {
+ values() : ServiceType[] {static} + values() : ServiceType[] {static}
} }
} }
BusinessDelegate --> "-serviceType" ServiceType
BusinessLookup --> "-ejbService" EjbService BusinessLookup --> "-ejbService" EjbService
BusinessDelegate --> "-serviceType" ServiceType
Client --> "-businessDelegate" BusinessDelegate Client --> "-businessDelegate" BusinessDelegate
BusinessDelegate --> "-businessService" BusinessService BusinessDelegate --> "-businessService" BusinessService
BusinessDelegate --> "-lookupService" BusinessLookup BusinessDelegate --> "-lookupService" BusinessLookup

View File

@@ -30,7 +30,7 @@
<parent> <parent>
<groupId>com.iluwatar</groupId> <groupId>com.iluwatar</groupId>
<artifactId>java-design-patterns</artifactId> <artifactId>java-design-patterns</artifactId>
<version>1.14.0</version> <version>1.16.0</version>
</parent> </parent>
<artifactId>business-delegate</artifactId> <artifactId>business-delegate</artifactId>
<dependencies> <dependencies>

View File

@@ -29,7 +29,7 @@
<parent> <parent>
<groupId>com.iluwatar</groupId> <groupId>com.iluwatar</groupId>
<artifactId>java-design-patterns</artifactId> <artifactId>java-design-patterns</artifactId>
<version>1.14.0</version> <version>1.16.0</version>
</parent> </parent>
<artifactId>caching</artifactId> <artifactId>caching</artifactId>
<dependencies> <dependencies>

View File

@@ -167,7 +167,7 @@ public class LruCache {
* Returns cache data in list form. * Returns cache data in list form.
*/ */
public List<UserAccount> getCacheDataInListForm() { public List<UserAccount> getCacheDataInListForm() {
ArrayList<UserAccount> listOfCacheData = new ArrayList<>(); List<UserAccount> listOfCacheData = new ArrayList<>();
Node temp = head; Node temp = head;
while (temp != null) { while (temp != null) {
listOfCacheData.add(temp.userAccount); listOfCacheData.add(temp.userAccount);

View File

@@ -29,7 +29,7 @@
<parent> <parent>
<groupId>com.iluwatar</groupId> <groupId>com.iluwatar</groupId>
<artifactId>java-design-patterns</artifactId> <artifactId>java-design-patterns</artifactId>
<version>1.14.0</version> <version>1.16.0</version>
</parent> </parent>
<artifactId>callback</artifactId> <artifactId>callback</artifactId>
<dependencies> <dependencies>

View File

@@ -53,8 +53,8 @@ package com.iluwatar.chain {
} }
} }
RequestHandler --> "-next" RequestHandler RequestHandler --> "-next" RequestHandler
OrcKing --> "-chain" RequestHandler
Request --> "-requestType" RequestType Request --> "-requestType" RequestType
OrcKing --> "-chain" RequestHandler
OrcCommander --|> RequestHandler OrcCommander --|> RequestHandler
OrcOfficer --|> RequestHandler OrcOfficer --|> RequestHandler
OrcSoldier --|> RequestHandler OrcSoldier --|> RequestHandler

View File

@@ -29,7 +29,7 @@
<parent> <parent>
<groupId>com.iluwatar</groupId> <groupId>com.iluwatar</groupId>
<artifactId>java-design-patterns</artifactId> <artifactId>java-design-patterns</artifactId>
<version>1.14.0</version> <version>1.16.0</version>
</parent> </parent>
<artifactId>chain</artifactId> <artifactId>chain</artifactId>
<dependencies> <dependencies>

View File

@@ -77,7 +77,6 @@
<property name="max" value="120"/> <property name="max" value="120"/>
<property name="ignorePattern" value="^package.*|^import.*|a href|href|http://|https://|ftp://"/> <property name="ignorePattern" value="^package.*|^import.*|a href|href|http://|https://|ftp://"/>
</module> </module>
<module name="AvoidStarImport"/>
<module name="OneTopLevelClass"/> <module name="OneTopLevelClass"/>
<module name="NoLineWrap"/> <module name="NoLineWrap"/>
<module name="EmptyBlock"> <module name="EmptyBlock">
@@ -185,6 +184,10 @@
<property name="allowedAnnotations" value="Override, Test, Before, After, Parameters, Given, When, BeforeClass, AfterClass, Parameterized"/> <property name="allowedAnnotations" value="Override, Test, Before, After, Parameters, Given, When, BeforeClass, AfterClass, Parameterized"/>
<property name="allowThrowsTagsForSubclasses" value="true"/> <property name="allowThrowsTagsForSubclasses" value="true"/>
</module> </module>
<module name="JavadocType">
<property name="scope" value="public"/>
<property name="allowUnknownTags" value="true"/>
</module>
<module name="MethodName"> <module name="MethodName">
<property name="format" value="^[a-z][a-z0-9][a-zA-Z0-9_]*$"/> <property name="format" value="^[a-z][a-z0-9][a-zA-Z0-9_]*$"/>
<message key="name.invalidPattern" <message key="name.invalidPattern"

View File

@@ -29,7 +29,7 @@
<parent> <parent>
<groupId>com.iluwatar</groupId> <groupId>com.iluwatar</groupId>
<artifactId>java-design-patterns</artifactId> <artifactId>java-design-patterns</artifactId>
<version>1.14.0</version> <version>1.16.0</version>
</parent> </parent>
<artifactId>command</artifactId> <artifactId>command</artifactId>
<dependencies> <dependencies>

View File

@@ -29,7 +29,7 @@
<parent> <parent>
<groupId>com.iluwatar</groupId> <groupId>com.iluwatar</groupId>
<artifactId>java-design-patterns</artifactId> <artifactId>java-design-patterns</artifactId>
<version>1.14.0</version> <version>1.16.0</version>
</parent> </parent>
<artifactId>composite</artifactId> <artifactId>composite</artifactId>
<dependencies> <dependencies>

29
converter/README.md Normal file
View File

@@ -0,0 +1,29 @@
---
layout: pattern
title: Converter
folder: converter
permalink: /patterns/converter/
categories:
tags:
- Java
- Difficulty-Beginner
---
## Intent
The purpose of the Converter Pattern is to provide a generic, common way of bidirectional
conversion between corresponding types, allowing a clean implementation in which the types do not
need to be aware of each other. Moreover, the Converter Pattern introduces bidirectional collection
mapping, reducing a boilerplate code to minimum.
![alt text](./etc/converter.png "Converter Pattern")
## Applicability
Use the Converter Pattern in the following situations:
* When you have types that logically correspond which other and you need to convert entities between them
* When you want to provide different ways of types conversions depending on a context
* Whenever you introduce a DTO (Data transfer object), you will probably need to convert it into the domain equivalence
## Credits
* [Converter](http://www.xsolve.pl/blog/converter-pattern-in-java-8/)

BIN
converter/etc/Converter.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 19 KiB

View File

@@ -0,0 +1,51 @@
<?xml version="1.0" encoding="UTF-8"?>
<class-diagram version="1.1.14" icons="true" automaticImage="PNG" always-add-relationships="false"
generalizations="true" realizations="true" associations="true" dependencies="false" nesting-relationships="true"
router="FAN">
<class id="1" language="java" name="com.iluwatar.converter.Converter" project="converter"
file="/converter/src/main/java/com/iluwatar/converter/Converter.java" binary="false" corner="BOTTOM_RIGHT">
<position height="189" width="226" x="41" y="37"/>
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
<operations public="true" package="true" protected="true" private="true" static="true"/>
</display>
</class>
<class id="2" language="java" name="com.iluwatar.converter.UserConverter" project="converter"
file="/converter/src/main/java/com/iluwatar/converter/UserConverter.java" binary="false" corner="BOTTOM_RIGHT">
<position height="81" width="107" x="41" y="356"/>
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
<operations public="true" package="true" protected="true" private="true" static="true"/>
</display>
</class>
<class id="3" language="java" name="com.iluwatar.converter.User" project="converter"
file="/converter/src/main/java/com/iluwatar/converter/User.java" binary="false" corner="BOTTOM_RIGHT">
<position height="279" width="188" x="307" y="37"/>
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
<operations public="true" package="true" protected="true" private="true" static="true"/>
</display>
</class>
<class id="4" language="java" name="com.iluwatar.converter.UserDto" project="converter"
file="/converter/src/main/java/com/iluwatar/converter/UserDto.java" binary="false" corner="BOTTOM_RIGHT">
<position height="279" width="204" x="535" y="37"/>
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
<operations public="true" package="true" protected="true" private="true" static="true"/>
</display>
</class>
<generalization id="5">
<end type="SOURCE" refId="2"/>
<end type="TARGET" refId="1"/>
</generalization>
<classifier-display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
<operations public="true" package="true" protected="true" private="true" static="true"/>
</classifier-display>
<association-display labels="true" multiplicity="true"/>
</class-diagram>

View File

@@ -0,0 +1,49 @@
@startuml
package com.iluwatar.converter {
class App {
+ App()
+ main(args : String[]) {static}
}
class Converter<T, U> {
- fromDto : Function<T, U>
- fromEntity : Function<U, T>
+ Converter<T, U>(fromDto : Function<T, U>, fromEntity : Function<U, T>)
+ convertFromDto(userDto : T) : U
+ convertFromEntity(user : U) : T
+ createFromDtos(dtoUsers : Collection<T>) : List<U>
+ createFromEntities(users : Collection<U>) : List<T>
}
class User {
- firstName : String
- isActive : boolean
- lastName : String
- userId : String
+ User(firstName : String, lastName : String, isActive : boolean, userId : String)
+ equals(o : Object) : boolean
+ getFirstName() : String
+ getLastName() : String
+ getUserId() : String
+ hashCode() : int
+ isActive() : boolean
+ toString() : String
}
class UserConverter {
+ UserConverter()
}
class UserDto {
- email : String
- firstName : String
- isActive : boolean
- lastName : String
+ UserDto(firstName : String, lastName : String, isActive : boolean, email : String)
+ equals(o : Object) : boolean
+ getEmail() : String
+ getFirstName() : String
+ getLastName() : String
+ hashCode() : int
+ isActive() : boolean
+ toString() : String
}
}
UserConverter --|> Converter
@enduml

49
converter/pom.xml Normal file
View File

@@ -0,0 +1,49 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
The MIT License
Copyright (c) 2014 Ilkka Seppälä
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>java-design-patterns</artifactId>
<groupId>com.iluwatar</groupId>
<version>1.16.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</dependency>
</dependencies>
<artifactId>converter</artifactId>
</project>

View File

@@ -0,0 +1,63 @@
/**
* The MIT License
* Copyright (c) 2014-2016 Ilkka Seppälä
* <p>
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* <p>
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* <p>
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.converter;
import com.google.common.collect.Lists;
import java.util.ArrayList;
import java.util.List;
/**
* The Converter pattern is a behavioral design pattern which allows a common way of bidirectional
* conversion between corresponding types (e.g. DTO and domain representations of the logically
* isomorphic types). Moreover, the pattern introduces a common way of converting a collection of
* objects between types.
*/
public class App {
/**
* Program entry point
*
* @param args command line args
*/
public static void main(String[] args) {
Converter<UserDto, User> userConverter = new Converter<>(
userDto -> new User(userDto.getFirstName(), userDto.getLastName(), userDto.isActive(),
userDto.getEmail()),
user -> new UserDto(user.getFirstName(), user.getLastName(), user.isActive(), user.getUserId()));
UserDto dtoUser = new UserDto("John", "Doe", true, "whatever[at]wherever.com");
User user = userConverter.convertFromDto(dtoUser);
System.out.println("Entity converted from DTO:" + user);
ArrayList<User> users = Lists.newArrayList(new User("Camile", "Tough", false, "124sad"),
new User("Marti", "Luther", true, "42309fd"), new User("Kate", "Smith", true, "if0243"));
System.out.println("Domain entities:");
users.forEach(System.out::println);
System.out.println("DTO entities converted from domain:");
List<UserDto> dtoEntities = userConverter.createFromEntities(users);
dtoEntities.forEach(System.out::println);
}
}

View File

@@ -0,0 +1,86 @@
/**
* The MIT License
* Copyright (c) 2014-2016 Ilkka Seppälä
* <p>
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* <p>
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* <p>
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.converter;
import java.util.Collection;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
* Generic converter, thanks to Java8 features not only provides a way of generic bidirectional
* conversion between coresponding types, but also a common way of converting a collection of objects
* of the same type, reducing boilerplate code to the absolute minimum.
* @param <T> DTO representation's type
* @param <U> Domain representation's type
*/
public class Converter<T, U> {
private final Function<T, U> fromDto;
private final Function<U, T> fromEntity;
/**
* @param fromDto Function that converts given dto entity into the domain entity.
* @param fromEntity Function that converts given domain entity into the dto entity.
*/
public Converter(final Function<T, U> fromDto, final Function<U, T> fromEntity) {
this.fromDto = fromDto;
this.fromEntity = fromEntity;
}
/**
* @param userDto DTO entity
* @return The domain representation - the result of the converting function application on dto entity.
*/
public final U convertFromDto(final T userDto) {
return fromDto.apply(userDto);
}
/**
* @param user domain entity
* @return The DTO representation - the result of the converting function application on domain entity.
*/
public final T convertFromEntity(final U user) {
return fromEntity.apply(user);
}
/**
* @param dtoUsers collection of DTO entities
* @return List of domain representation of provided entities retrieved by
* mapping each of them with the convertion function
*/
public final List<U> createFromDtos(final Collection<T> dtoUsers) {
return dtoUsers.stream().map(this::convertFromDto).collect(Collectors.toList());
}
/**
* @param users collection of domain entities
* @return List of domain representation of provided entities retrieved by
* mapping each of them with the convertion function
*/
public final List<T> createFromEntities(final Collection<U> users) {
return users.stream().map(this::convertFromEntity).collect(Collectors.toList());
}
}

View File

@@ -0,0 +1,86 @@
/**
* The MIT License
* Copyright (c) 2014-2016 Ilkka Seppälä
* <p>
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* <p>
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* <p>
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.converter;
import java.util.Objects;
/**
* User class
*/
public class User {
private String firstName;
private String lastName;
private boolean isActive;
private String userId;
/**
* @param firstName user's first name
* @param lastName user's last name
* @param isActive flag indicating whether the user is active
* @param userId user's identificator
*/
public User(String firstName, String lastName, boolean isActive, String userId) {
this.firstName = firstName;
this.lastName = lastName;
this.isActive = isActive;
this.userId = userId;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public boolean isActive() {
return isActive;
}
public String getUserId() {
return userId;
}
@Override public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
User user = (User) o;
return isActive == user.isActive && Objects.equals(firstName, user.firstName) && Objects
.equals(lastName, user.lastName) && Objects.equals(userId, user.userId);
}
@Override public int hashCode() {
return Objects.hash(firstName, lastName, isActive, userId);
}
@Override public String toString() {
return "User{" + "firstName='" + firstName + '\'' + ", lastName='" + lastName + '\''
+ ", isActive=" + isActive + ", userId='" + userId + '\'' + '}';
}
}

View File

@@ -0,0 +1,40 @@
/**
* The MIT License
* Copyright (c) 2014-2016 Ilkka Seppälä
* <p>
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* <p>
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* <p>
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.converter;
/**
* Example implementation of the simple User converter.
*/
public class UserConverter extends Converter<UserDto, User> {
/**
* Constructor.
*/
public UserConverter() {
super(userDto -> new User(userDto.getFirstName(), userDto.getLastName(), userDto.isActive(),
userDto.getEmail()),
user -> new UserDto(user.getFirstName(), user.getLastName(), user.isActive(),
user.getUserId()));
}
}

View File

@@ -0,0 +1,88 @@
/**
* The MIT License
* Copyright (c) 2014-2016 Ilkka Seppälä
* <p>
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* <p>
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* <p>
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.converter;
import java.util.Objects;
/**
* User DTO class
*/
public class UserDto {
private String firstName;
private String lastName;
private boolean isActive;
private String email;
/**
* @param firstName user's first name
* @param lastName user's last name
* @param isActive flag indicating whether the user is active
* @param email user's email address
*/
public UserDto(String firstName, String lastName, boolean isActive, String email) {
this.firstName = firstName;
this.lastName = lastName;
this.isActive = isActive;
this.email = email;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public boolean isActive() {
return isActive;
}
public String getEmail() {
return email;
}
@Override public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
UserDto userDto = (UserDto) o;
return isActive == userDto.isActive && Objects.equals(firstName, userDto.firstName) && Objects
.equals(lastName, userDto.lastName) && Objects.equals(email, userDto.email);
}
@Override public int hashCode() {
return Objects.hash(firstName, lastName, isActive, email);
}
@Override public String toString() {
return "UserDto{" + "firstName='" + firstName + '\'' + ", lastName='" + lastName + '\''
+ ", isActive=" + isActive + ", email='" + email + '\'' + '}';
}
}

View File

@@ -0,0 +1,38 @@
/**
* The MIT License
* Copyright (c) 2014-2016 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.converter;
import org.junit.Test;
/**
* App running test
*/
public class AppTest {
@Test
public void testMain() {
String[] args = {};
App.main(args);
}
}

View File

@@ -0,0 +1,84 @@
/**
* The MIT License
* Copyright (c) 2014 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.converter;
import com.google.common.collect.Lists;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import static junit.framework.TestCase.assertEquals;
/**
* Tests for {@link Converter}
*/
public class ConverterTest {
private UserConverter userConverter = new UserConverter();
/**
* Tests whether a converter created of opposite functions holds equality as a bijection.
*/
@Test public void testConversionsStartingFromDomain() {
User u1 = new User("Tom", "Hanks", true, "tom@hanks.com");
User u2 = userConverter.convertFromDto(userConverter.convertFromEntity(u1));
assertEquals(u1, u2);
}
/**
* Tests whether a converter created of opposite functions holds equality as a bijection.
*/
@Test public void testConversionsStartingFromDto() {
UserDto u1 = new UserDto("Tom", "Hanks", true, "tom@hanks.com");
UserDto u2 = userConverter.convertFromEntity(userConverter.convertFromDto(u1));
assertEquals(u1, u2);
}
/**
* Tests the custom users converter. Thanks to Java8 lambdas, converter can be easily and
* cleanly instantiated allowing various different conversion strategies to be implemented.
*/
@Test public void testCustomConverter() {
Converter<UserDto, User> converter = new Converter<>(
userDto -> new User(userDto.getFirstName(), userDto.getLastName(), userDto.isActive(),
String.valueOf(new Random().nextInt())),
user -> new UserDto(user.getFirstName(), user.getLastName(), user.isActive(),
user.getFirstName().toLowerCase() + user.getLastName().toLowerCase() + "@whatever.com"));
User u1 = new User("John", "Doe", false, "12324");
UserDto userDto = converter.convertFromEntity(u1);
assertEquals(userDto.getEmail(), "johndoe@whatever.com");
}
/**
* Test whether converting a collection of Users to DTO Users and then converting them back to domain
* users returns an equal collection.
*/
@Test public void testCollectionConversion() {
ArrayList<User> users = Lists.newArrayList(new User("Camile", "Tough", false, "124sad"),
new User("Marti", "Luther", true, "42309fd"), new User("Kate", "Smith", true, "if0243"));
List<User> fromDtos = userConverter.createFromDtos(userConverter.createFromEntities(users));
assertEquals(fromDtos, users);
}
}

View File

@@ -34,9 +34,10 @@ package com.iluwatar.dao {
+ getById(int) : Optional<Customer> {abstract} + getById(int) : Optional<Customer> {abstract}
+ update(Customer) : boolean {abstract} + update(Customer) : boolean {abstract}
} }
interface CustomerSchemaSql { class CustomerSchemaSql {
+ CREATE_SCHEMA_SQL : String {static} + CREATE_SCHEMA_SQL : String {static}
+ DELETE_SCHEMA_SQL : String {static} + DELETE_SCHEMA_SQL : String {static}
- CustomerSchemaSql()
} }
class DbCustomerDao { class DbCustomerDao {
- dataSource : DataSource - dataSource : DataSource

View File

@@ -30,7 +30,7 @@
<parent> <parent>
<groupId>com.iluwatar</groupId> <groupId>com.iluwatar</groupId>
<artifactId>java-design-patterns</artifactId> <artifactId>java-design-patterns</artifactId>
<version>1.14.0</version> <version>1.16.0</version>
</parent> </parent>
<artifactId>dao</artifactId> <artifactId>dao</artifactId>

View File

@@ -22,10 +22,16 @@
*/ */
package com.iluwatar.dao; package com.iluwatar.dao;
public interface CustomerSchemaSql { /**
* Customer Schema SQL Class
*/
public final class CustomerSchemaSql {
String CREATE_SCHEMA_SQL = "CREATE TABLE CUSTOMERS (ID NUMBER, FNAME VARCHAR(100), " private CustomerSchemaSql() {}
public static final String CREATE_SCHEMA_SQL = "CREATE TABLE CUSTOMERS (ID NUMBER, FNAME VARCHAR(100), "
+ "LNAME VARCHAR(100))"; + "LNAME VARCHAR(100))";
String DELETE_SCHEMA_SQL = "DROP TABLE CUSTOMERS"; public static final String DELETE_SCHEMA_SQL = "DROP TABLE CUSTOMERS";
} }

View File

@@ -29,6 +29,9 @@ import static org.junit.Assert.assertNotEquals;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
/**
* Tests {@link Customer}.
*/
public class CustomerTest { public class CustomerTest {
private Customer customer; private Customer customer;

1
data-bus/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
/target

33
data-bus/README.md Normal file
View File

@@ -0,0 +1,33 @@
---
layout: pattern
title: Data Bus
folder: data-bus
permalink: /patterns/data-bus/
categories: Architectural
tags:
- Java
- Difficulty-Intermediate
---
## Intent
Allows send of messages/events between components of an application
without them needing to know about each other. They only need to know
about the type of the message/event being sent.
![data bus pattern uml diagram](./etc/data-bus.urm.png "Data Bus pattern")
## Applicability
Use Data Bus pattern when
* you want your components to decide themselves which messages/events they want to receive
* you want to have many-to-many communication
* you want your components to know nothing about each other
## Related Patterns
Data Bus is similar to
* Mediator pattern with Data Bus Members deciding for themselves if they want to accept any given message
* Observer pattern but supporting many-to-many communication
* Publish/Subscribe pattern with the Data Bus decoupling the publisher and the subscriber

Binary file not shown.

After

Width:  |  Height:  |  Size: 60 KiB

View File

@@ -0,0 +1,77 @@
@startuml
package com.iluwatar.databus {
class AbstractDataType {
- dataBus : DataBus
+ AbstractDataType()
+ getDataBus() : DataBus
+ setDataBus(dataBus : DataBus)
}
~class App {
- log : Logger {static}
~ App()
+ main(args : String[]) {static}
}
class DataBus {
- INSTANCE : DataBus {static}
- listeners : Set<Member>
+ DataBus()
+ getInstance() : DataBus {static}
+ publish(event : DataType)
+ subscribe(member : Member)
+ unsubscribe(member : Member)
}
interface DataType {
+ getDataBus() : DataBus {abstract}
+ setDataBus(DataBus) {abstract}
}
interface Member {
+ accept(DataType) {abstract}
}
}
package com.iluwatar.databus.data {
class MessageData {
- message : String
+ MessageData(message : String)
+ getMessage() : String
+ of(message : String) : DataType {static}
}
class StartingData {
- when : LocalDateTime
+ StartingData(when : LocalDateTime)
+ getWhen() : LocalDateTime
+ of(when : LocalDateTime) : DataType {static}
}
class StoppingData {
- when : LocalDateTime
+ StoppingData(when : LocalDateTime)
+ getWhen() : LocalDateTime
+ of(when : LocalDateTime) : DataType {static}
}
}
package com.iluwatar.databus.members {
class CounterMember {
- log : Logger {static}
- name : String
+ CounterMember(name : String)
+ accept(data : DataType)
- handleEvent(data : MessageData)
}
class StatusMember {
- id : int
- log : Logger {static}
+ StatusMember(id : int)
+ accept(data : DataType)
- handleEvent(data : StartingData)
- handleEvent(data : StoppingData)
}
}
AbstractDataType --> "-dataBus" DataBus
DataBus --> "-INSTANCE" DataBus
DataBus --> "-listeners" Member
AbstractDataType ..|> DataType
MessageData --|> AbstractDataType
StartingData --|> AbstractDataType
StoppingData --|> AbstractDataType
CounterMember ..|> Member
StatusMember ..|> Member
@enduml

51
data-bus/pom.xml Normal file
View File

@@ -0,0 +1,51 @@
<?xml version="1.0"?>
<!--
The MIT License
Copyright (c) 2014-2016 Ilkka Seppälä
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-->
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<properties>
<lombok.version>1.16.14</lombok.version>
</properties>
<parent>
<groupId>com.iluwatar</groupId>
<artifactId>java-design-patterns</artifactId>
<version>1.16.0</version>
</parent>
<artifactId>data-bus</artifactId>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,67 @@
/**
* The MIT License
* Copyright (c) 2014 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
/*
The MIT License (MIT)
Copyright (c) 2016 Paul Campbell
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
package com.iluwatar.databus;
/**
* Base for data to send via the Data-Bus.
*
* @author Paul Campbell (pcampbell@kemitix.net)
*/
public class AbstractDataType implements DataType {
private DataBus dataBus;
@Override
public DataBus getDataBus() {
return dataBus;
}
@Override
public void setDataBus(DataBus dataBus) {
this.dataBus = dataBus;
}
}

View File

@@ -0,0 +1,79 @@
/**
* The MIT License
* Copyright (c) 2014-2016 Ilkka Seppälä
* <p>
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* <p>
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* <p>
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.databus;
import com.iluwatar.databus.data.MessageData;
import com.iluwatar.databus.data.StartingData;
import com.iluwatar.databus.data.StoppingData;
import com.iluwatar.databus.members.MessageCollectorMember;
import com.iluwatar.databus.members.StatusMember;
import java.time.LocalDateTime;
/**
* The Data Bus pattern
* <p>
* <p>{@see http://wiki.c2.com/?DataBusPattern}</p>
* <p>
* <p>The Data-Bus pattern provides a method where different parts of an application may
* pass messages between each other without needing to be aware of the other's existence.</p>
* <p>Similar to the {@code ObserverPattern}, members register themselves with the {@link DataBus}
* and may then receive each piece of data that is published to the Data-Bus. The member
* may react to any given message or not.</p>
* <p>It allows for Many-to-Many distribution of data, as there may be any number of
* publishers to a Data-Bus, and any number of members receiving the data. All members
* will receive the same data, the order each receives a given piece of data, is an
* implementation detail.</p>
* <p>Members may unsubscribe from the Data-Bus to stop receiving data.</p>
* <p>This example of the pattern implements a Synchronous Data-Bus, meaning that
* when data is published to the Data-Bus, the publish method will not return until
* all members have received the data and returned.</p>
* <p>The {@link DataBus} class is a Singleton.</p>
* <p>Members of the Data-Bus must implement the {@link Member} interface.</p>
* <p>Data to be published via the Data-Bus must implement the {@link DataType} interface.</p>
* <p>The {@code data} package contains example {@link DataType} implementations.</p>
* <p>The {@code members} package contains example {@link Member} implementations.</p>
* <p>The {@link StatusMember} demonstrates using the DataBus to publish a message
* to the Data-Bus when it receives a message.</p>
*
* @author Paul Campbell (pcampbell@kemitix.net)
*/
class App {
public static void main(String[] args) {
final DataBus bus = DataBus.getInstance();
bus.subscribe(new StatusMember(1));
bus.subscribe(new StatusMember(2));
final MessageCollectorMember foo = new MessageCollectorMember("Foo");
final MessageCollectorMember bar = new MessageCollectorMember("Bar");
bus.subscribe(foo);
bus.publish(StartingData.of(LocalDateTime.now()));
bus.publish(MessageData.of("Only Foo should see this"));
bus.subscribe(bar);
bus.publish(MessageData.of("Foo and Bar should see this"));
bus.unsubscribe(foo);
bus.publish(MessageData.of("Only Bar should see this"));
bus.publish(StoppingData.of(LocalDateTime.now()));
}
}

View File

@@ -0,0 +1,73 @@
/**
* The MIT License
* Copyright (c) 2014-2016 Ilkka Seppälä
* <p>
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* <p>
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* <p>
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.databus;
import java.util.HashSet;
import java.util.Set;
/**
* The Data-Bus implementation.
*
* <p>This implementation uses a Singleton.</p>
*
* @author Paul Campbell (pcampbell@kemitix.net)
*/
public class DataBus {
private static final DataBus INSTANCE = new DataBus();
private final Set<Member> listeners = new HashSet<>();
public static DataBus getInstance() {
return INSTANCE;
}
/**
* Register a member with the data-bus to start receiving events.
*
* @param member The member to register
*/
public void subscribe(final Member member) {
this.listeners.add(member);
}
/**
* Deregister a member to stop receiving events.
*
* @param member The member to deregister
*/
public void unsubscribe(final Member member) {
this.listeners.remove(member);
}
/**
* Publish and event to all members.
*
* @param event The event
*/
public void publish(final DataType event) {
event.setDataBus(this);
listeners.forEach(listener -> listener.accept(event));
}
}

View File

@@ -0,0 +1,70 @@
/**
* The MIT License
* Copyright (c) 2014 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
/*
The MIT License (MIT)
Copyright (c) 2016 Paul Campbell
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
package com.iluwatar.databus;
/**
* Events are sent via the Data-Bus.
*
* @author Paul Campbell (pcampbell@kemitix.net)
*/
public interface DataType {
/**
* Returns the data-bus the event is being sent on.
*
* @return The data-bus
*/
DataBus getDataBus();
/**
* Set the data-bus the event will be sent on.
*
* @param dataBus The data-bus
*/
void setDataBus(DataBus dataBus);
}

View File

@@ -0,0 +1,59 @@
/**
* The MIT License
* Copyright (c) 2014 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
/*
The MIT License (MIT)
Copyright (c) 2016 Paul Campbell
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
package com.iluwatar.databus;
import java.util.function.Consumer;
/**
* Members receive events from the Data-Bus.
*
* @author Paul Campbell (pcampbell@kemitix.net)
*/
public interface Member extends Consumer<DataType> {
void accept(DataType event);
}

View File

@@ -0,0 +1,49 @@
/**
* The MIT License
* Copyright (c) 2014-2016 Ilkka Seppälä
* <p>
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* <p>
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* <p>
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.databus.data;
import com.iluwatar.databus.AbstractDataType;
import com.iluwatar.databus.DataType;
/**
* An event raised when a string message is sent.
*
* @author Paul Campbell (pcampbell@kemitix.net)
*/
public class MessageData extends AbstractDataType {
private final String message;
public MessageData(String message) {
this.message = message;
}
public String getMessage() {
return message;
}
public static DataType of(final String message) {
return new MessageData(message);
}
}

View File

@@ -0,0 +1,51 @@
/**
* The MIT License
* Copyright (c) 2014-2016 Ilkka Seppälä
* <p>
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* <p>
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* <p>
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.databus.data;
import com.iluwatar.databus.AbstractDataType;
import com.iluwatar.databus.DataType;
import java.time.LocalDateTime;
/**
* An event raised when applications starts, containing the start time of the application.
*
* @author Paul Campbell (pcampbell@kemitix.net)
*/
public class StartingData extends AbstractDataType {
private final LocalDateTime when;
public StartingData(LocalDateTime when) {
this.when = when;
}
public LocalDateTime getWhen() {
return when;
}
public static DataType of(final LocalDateTime when) {
return new StartingData(when);
}
}

View File

@@ -0,0 +1,51 @@
/**
* The MIT License
* Copyright (c) 2014-2016 Ilkka Seppälä
* <p>
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* <p>
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* <p>
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.databus.data;
import com.iluwatar.databus.AbstractDataType;
import com.iluwatar.databus.DataType;
import java.time.LocalDateTime;
/**
* An event raised when applications stops, containing the stop time of the application.
*
* @author Paul Campbell (pcampbell@kemitix.net)
*/
public class StoppingData extends AbstractDataType {
private final LocalDateTime when;
public StoppingData(LocalDateTime when) {
this.when = when;
}
public LocalDateTime getWhen() {
return when;
}
public static DataType of(final LocalDateTime when) {
return new StoppingData(when);
}
}

View File

@@ -0,0 +1,67 @@
/**
* The MIT License
* Copyright (c) 2014-2016 Ilkka Seppälä
* <p>
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* <p>
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* <p>
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.databus.members;
import com.iluwatar.databus.DataType;
import com.iluwatar.databus.Member;
import com.iluwatar.databus.data.MessageData;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.logging.Logger;
/**
* Receiver of Data-Bus events that collects the messages from each {@link MessageData}.
*
* @author Paul Campbell (pcampbell@kemitix.net)
*/
public class MessageCollectorMember implements Member {
private static final Logger LOGGER = Logger.getLogger(MessageCollectorMember.class.getName());
private final String name;
private List<String> messages = new ArrayList<>();
public MessageCollectorMember(String name) {
this.name = name;
}
@Override
public void accept(final DataType data) {
if (data instanceof MessageData) {
handleEvent((MessageData) data);
}
}
private void handleEvent(MessageData data) {
LOGGER.info(String.format("%s sees message %s", name, data.getMessage()));
messages.add(data.getMessage());
}
public List<String> getMessages() {
return Collections.unmodifiableList(messages);
}
}

View File

@@ -0,0 +1,82 @@
/**
* The MIT License
* Copyright (c) 2014-2016 Ilkka Seppälä
* <p>
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* <p>
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* <p>
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.databus.members;
import com.iluwatar.databus.DataType;
import com.iluwatar.databus.Member;
import com.iluwatar.databus.data.MessageData;
import com.iluwatar.databus.data.StartingData;
import com.iluwatar.databus.data.StoppingData;
import java.time.LocalDateTime;
import java.util.logging.Logger;
/**
* Receiver of Data-Bus events.
*
* @author Paul Campbell (pcampbell@kemitix.net)
*/
public class StatusMember implements Member {
private static final Logger LOGGER = Logger.getLogger(StatusMember.class.getName());
private final int id;
private LocalDateTime started;
private LocalDateTime stopped;
public StatusMember(int id) {
this.id = id;
}
@Override
public void accept(final DataType data) {
if (data instanceof StartingData) {
handleEvent((StartingData) data);
} else if (data instanceof StoppingData) {
handleEvent((StoppingData) data);
}
}
private void handleEvent(StartingData data) {
started = data.getWhen();
LOGGER.info(String.format("Receiver #%d sees application started at %s", id, started));
}
private void handleEvent(StoppingData data) {
stopped = data.getWhen();
LOGGER.info(String.format("Receiver #%d sees application stopping at %s", id, stopped));
LOGGER.info(String.format("Receiver #%d sending goodbye message", id));
data.getDataBus().publish(MessageData.of(String.format("Goodbye cruel world from #%d!", id)));
}
public LocalDateTime getStarted() {
return started;
}
public LocalDateTime getStopped() {
return stopped;
}
}

View File

@@ -0,0 +1,74 @@
/**
* The MIT License
* Copyright (c) 2014 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.databus;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import static org.mockito.BDDMockito.then;
import static org.mockito.Mockito.never;
/**
* Tests for {@link DataBus}.
*
* @author Paul Campbell (pcampbell@kemitix.net)
*/
public class DataBusTest {
@Mock
private Member member;
@Mock
private DataType event;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
}
@Test
public void publishedEventIsReceivedBySubscribedMember() {
//given
final DataBus dataBus = DataBus.getInstance();
dataBus.subscribe(member);
//when
dataBus.publish(event);
//then
then(member).should().accept(event);
}
@Test
public void publishedEventIsNotReceivedByMemberAfterUnsubscribing() {
//given
final DataBus dataBus = DataBus.getInstance();
dataBus.subscribe(member);
dataBus.unsubscribe(member);
//when
dataBus.publish(event);
//then
then(member).should(never()).accept(event);
}
}

View File

@@ -0,0 +1,62 @@
/**
* The MIT License
* Copyright (c) 2014 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.databus.members;
import com.iluwatar.databus.data.MessageData;
import com.iluwatar.databus.data.StartingData;
import org.junit.Assert;
import org.junit.Test;
import java.time.LocalDateTime;
/**
* Tests for {@link MessageCollectorMember}.
*
* @author Paul Campbell (pcampbell@kemitix.net)
*/
public class MessageCollectorMemberTest {
@Test
public void collectMessageFromMessageData() {
//given
final String message = "message";
final MessageData messageData = new MessageData(message);
final MessageCollectorMember collector = new MessageCollectorMember("collector");
//when
collector.accept(messageData);
//then
Assert.assertTrue(collector.getMessages().contains(message));
}
@Test
public void collectIgnoresMessageFromOtherDataTypes() {
//given
final StartingData startingData = new StartingData(LocalDateTime.now());
final MessageCollectorMember collector = new MessageCollectorMember("collector");
//when
collector.accept(startingData);
//then
Assert.assertEquals(0, collector.getMessages().size());
}
}

View File

@@ -0,0 +1,79 @@
/**
* The MIT License
* Copyright (c) 2014 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.databus.members;
import com.iluwatar.databus.DataBus;
import com.iluwatar.databus.data.MessageData;
import com.iluwatar.databus.data.StartingData;
import com.iluwatar.databus.data.StoppingData;
import org.junit.Assert;
import org.junit.Test;
import java.time.LocalDateTime;
import java.time.Month;
/**
* Tests for {@link StatusMember}.
*
* @author Paul Campbell (pcampbell@kemitix.net)
*/
public class StatusMemberTest {
@Test
public void statusRecordsTheStartTime() {
//given
final LocalDateTime startTime = LocalDateTime.of(2017, Month.APRIL, 1, 19, 9);
final StartingData startingData = new StartingData(startTime);
final StatusMember statusMember = new StatusMember(1);
//when
statusMember.accept(startingData);
//then
Assert.assertEquals(startTime, statusMember.getStarted());
}
@Test
public void statusRecordsTheStopTime() {
//given
final LocalDateTime stop = LocalDateTime.of(2017, Month.APRIL, 1, 19, 12);
final StoppingData stoppingData = new StoppingData(stop);
stoppingData.setDataBus(DataBus.getInstance());
final StatusMember statusMember = new StatusMember(1);
//when
statusMember.accept(stoppingData);
//then
Assert.assertEquals(stop, statusMember.getStopped());
}
@Test
public void statusIgnoresMessageData() {
//given
final MessageData messageData = new MessageData("message");
final StatusMember statusMember = new StatusMember(1);
//when
statusMember.accept(messageData);
//then
Assert.assertNull(statusMember.getStarted());
Assert.assertNull(statusMember.getStopped());
}
}

View File

@@ -28,7 +28,7 @@
<parent> <parent>
<groupId>com.iluwatar</groupId> <groupId>com.iluwatar</groupId>
<artifactId>java-design-patterns</artifactId> <artifactId>java-design-patterns</artifactId>
<version>1.14.0</version> <version>1.16.0</version>
</parent> </parent>
<artifactId>data-mapper</artifactId> <artifactId>data-mapper</artifactId>
<dependencies> <dependencies>

View File

@@ -21,6 +21,9 @@ package com.iluwatar.datamapper;
import java.io.Serializable; import java.io.Serializable;
/**
* Class defining Student
*/
public final class Student implements Serializable { public final class Student implements Serializable {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;

View File

@@ -20,6 +20,9 @@ package com.iluwatar.datamapper;
import java.util.Optional; import java.util.Optional;
/**
* Interface lists out the possible behaviour for all possible student mappers
*/
public interface StudentDataMapper { public interface StudentDataMapper {
Optional<Student> find(int studentId); Optional<Student> find(int studentId);

View File

@@ -22,6 +22,9 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
/**
* Implementation of Actions on Students Data
*/
public final class StudentDataMapperImpl implements StudentDataMapper { public final class StudentDataMapperImpl implements StudentDataMapper {
/* Note: Normally this would be in the form of an actual database */ /* Note: Normally this would be in the form of an actual database */

View File

@@ -18,10 +18,14 @@
*/ */
package com.iluwatar.datamapper; package com.iluwatar.datamapper;
import org.junit.Test;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
/**
* Tests {@link Student}.
*/
public final class StudentTest { public final class StudentTest {
@Test @Test

View File

@@ -29,7 +29,7 @@
<parent> <parent>
<groupId>com.iluwatar</groupId> <groupId>com.iluwatar</groupId>
<artifactId>java-design-patterns</artifactId> <artifactId>java-design-patterns</artifactId>
<version>1.14.0</version> <version>1.16.0</version>
</parent> </parent>
<artifactId>decorator</artifactId> <artifactId>decorator</artifactId>
<dependencies> <dependencies>

View File

@@ -30,7 +30,7 @@
<parent> <parent>
<artifactId>java-design-patterns</artifactId> <artifactId>java-design-patterns</artifactId>
<groupId>com.iluwatar</groupId> <groupId>com.iluwatar</groupId>
<version>1.14.0</version> <version>1.16.0</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>

View File

@@ -22,6 +22,12 @@
*/ */
package com.iluwatar.delegation.simple; package com.iluwatar.delegation.simple;
/**
* Delegator Class to delegate the implementation of the Printer.
* This ensures two things:
* - when the actual implementation of the Printer class changes the delegation will still be operational
* - the actual benefit is observed when there are more than one implementors and they share a delegation control
*/
public class PrinterController implements Printer { public class PrinterController implements Printer {
private final Printer printer; private final Printer printer;

View File

@@ -23,7 +23,9 @@
package com.iluwatar.delegation.simple; package com.iluwatar.delegation.simple;
import org.junit.Test; import org.junit.Test;
/**
* Application Test Entry
*/
public class AppTest { public class AppTest {
@Test @Test

View File

@@ -22,22 +22,24 @@
*/ */
package com.iluwatar.delegation.simple; package com.iluwatar.delegation.simple;
import static org.junit.Assert.assertEquals;
import ch.qos.logback.classic.Logger; import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.spi.ILoggingEvent; import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.AppenderBase; import ch.qos.logback.core.AppenderBase;
import com.iluwatar.delegation.simple.printers.CanonPrinter; import com.iluwatar.delegation.simple.printers.CanonPrinter;
import com.iluwatar.delegation.simple.printers.EpsonPrinter; import com.iluwatar.delegation.simple.printers.EpsonPrinter;
import com.iluwatar.delegation.simple.printers.HpPrinter; import com.iluwatar.delegation.simple.printers.HpPrinter;
import java.util.LinkedList;
import java.util.List;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.util.LinkedList; /**
import java.util.List; * Test for Delegation Pattern
*/
import static org.junit.Assert.assertEquals;
public class DelegateTest { public class DelegateTest {
private InMemoryAppender appender; private InMemoryAppender appender;
@@ -78,6 +80,9 @@ public class DelegateTest {
assertEquals("Epson Printer : Test Message Printed", appender.getLastMessage()); assertEquals("Epson Printer : Test Message Printed", appender.getLastMessage());
} }
/**
* Logging Appender
*/
private class InMemoryAppender extends AppenderBase<ILoggingEvent> { private class InMemoryAppender extends AppenderBase<ILoggingEvent> {
private List<ILoggingEvent> log = new LinkedList<>(); private List<ILoggingEvent> log = new LinkedList<>();

View File

@@ -29,7 +29,7 @@
<parent> <parent>
<groupId>com.iluwatar</groupId> <groupId>com.iluwatar</groupId>
<artifactId>java-design-patterns</artifactId> <artifactId>java-design-patterns</artifactId>
<version>1.14.0</version> <version>1.16.0</version>
</parent> </parent>
<artifactId>dependency-injection</artifactId> <artifactId>dependency-injection</artifactId>
<dependencies> <dependencies>

View File

@@ -0,0 +1,65 @@
/**
* The MIT License
* Copyright (c) 2014 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.dependency.injection;
/**
* The MIT License
* Copyright (c) 2014-2017 Ilkka Seppälä
* <p>
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* <p>
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* <p>
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
/**
* AdvancedSorceress implements inversion of control. It depends on abstraction that can be injected
* through its setter.
*/
public class AdvancedSorceress implements Wizard {
private Tobacco tobacco;
public void setTobacco(Tobacco tobacco) {
this.tobacco = tobacco;
}
@Override
public void smoke() {
tobacco.smoke(this);
}
}

View File

@@ -1,17 +1,17 @@
/** /**
* The MIT License * The MIT License
* Copyright (c) 2014-2016 Ilkka Seppälä * Copyright (c) 2014-2016 Ilkka Seppälä
* * <p>
* Permission is hereby granted, free of charge, to any person obtaining a copy * Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal * of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights * in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is * copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions: * furnished to do so, subject to the following conditions:
* * <p>
* The above copyright notice and this permission notice shall be included in * The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software. * all copies or substantial portions of the Software.
* * <p>
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
@@ -26,7 +26,6 @@ import com.google.inject.Guice;
import com.google.inject.Injector; import com.google.inject.Injector;
/** /**
*
* Dependency Injection pattern deals with how objects handle their dependencies. The pattern * Dependency Injection pattern deals with how objects handle their dependencies. The pattern
* implements so called inversion of control principle. Inversion of control has two specific rules: * implements so called inversion of control principle. Inversion of control has two specific rules:
* - High-level modules should not depend on low-level modules. Both should depend on abstractions. * - High-level modules should not depend on low-level modules. Both should depend on abstractions.
@@ -36,15 +35,15 @@ import com.google.inject.Injector;
* naive implementation violating the inversion of control principle. It depends directly on a * naive implementation violating the inversion of control principle. It depends directly on a
* concrete implementation which cannot be changed. * concrete implementation which cannot be changed.
* <p> * <p>
* The second wizard ({@link AdvancedWizard}) is more flexible. It does not depend on any concrete * The second and third wizards({@link AdvancedWizard} and {@link AdvancedSorceress}) are more flexible.
* implementation but abstraction. It utilizes Dependency Injection pattern allowing its * They do not depend on any concrete implementation but abstraction. They utilizes Dependency Injection
* {@link Tobacco} dependency to be injected through its constructor. This way, handling the * pattern allowing their {@link Tobacco} dependency to be injected through constructor ({@link AdvancedWizard})
* dependency is no longer the wizard's responsibility. It is resolved outside the wizard class. * or setter ({@link AdvancedSorceress}). This way, handling the dependency is no longer the wizard's
* responsibility. It is resolved outside the wizard class.
* <p> * <p>
* The third example takes the pattern a step further. It uses Guice framework for Dependency * The fourth example takes the pattern a step further. It uses Guice framework for Dependency
* Injection. {@link TobaccoModule} binds a concrete implementation to abstraction. Injector is then * Injection. {@link TobaccoModule} binds a concrete implementation to abstraction. Injector is then
* used to create {@link GuiceWizard} object with correct dependencies. * used to create {@link GuiceWizard} object with correct dependencies.
*
*/ */
public class App { public class App {
@@ -60,6 +59,10 @@ public class App {
AdvancedWizard advancedWizard = new AdvancedWizard(new SecondBreakfastTobacco()); AdvancedWizard advancedWizard = new AdvancedWizard(new SecondBreakfastTobacco());
advancedWizard.smoke(); advancedWizard.smoke();
AdvancedSorceress advancedSorceress = new AdvancedSorceress();
advancedSorceress.setTobacco(new SecondBreakfastTobacco());
advancedSorceress.smoke();
Injector injector = Guice.createInjector(new TobaccoModule()); Injector injector = Guice.createInjector(new TobaccoModule());
GuiceWizard guiceWizard = injector.getInstance(GuiceWizard.class); GuiceWizard guiceWizard = injector.getInstance(GuiceWizard.class);
guiceWizard.smoke(); guiceWizard.smoke();

View File

@@ -0,0 +1,76 @@
/**
* The MIT License
* Copyright (c) 2014-2016 Ilkka Seppälä
* <p>
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* <p>
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* <p>
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.dependency.injection;
import com.iluwatar.dependency.injection.utils.InMemoryAppender;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
/**
* Date: 28/04/17 - 7:40 AM
*
* @author Stanislav Kapinus
*/
public class AdvancedSorceressTest {
private InMemoryAppender appender;
@Before
public void setUp() {
appender = new InMemoryAppender(Tobacco.class);
}
@After
public void tearDown() {
appender.stop();
}
/**
* Test if the {@link AdvancedSorceress} smokes whatever instance of {@link Tobacco} is passed to her
* through the setter's parameter
*/
@Test
public void testSmokeEveryThing() throws Exception {
final Tobacco[] tobaccos = {
new OldTobyTobacco(), new RivendellTobacco(), new SecondBreakfastTobacco()
};
for (final Tobacco tobacco : tobaccos) {
final AdvancedSorceress advancedSorceress = new AdvancedSorceress();
advancedSorceress.setTobacco(tobacco);
advancedSorceress.smoke();
// Verify if the sorceress is smoking the correct tobacco ...
assertEquals("AdvancedSorceress smoking " + tobacco.getClass().getSimpleName(), appender.getLastMessage());
}
// ... and nothing else is happening.
assertEquals(tobaccos.length, appender.getLogSize());
}
}

View File

@@ -30,6 +30,10 @@ import org.slf4j.LoggerFactory;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
/**
* InMemory Log Appender Util.
*/
public class InMemoryAppender extends AppenderBase<ILoggingEvent> { public class InMemoryAppender extends AppenderBase<ILoggingEvent> {
private List<ILoggingEvent> log = new LinkedList<>(); private List<ILoggingEvent> log = new LinkedList<>();

View File

@@ -27,7 +27,7 @@
<parent> <parent>
<groupId>com.iluwatar</groupId> <groupId>com.iluwatar</groupId>
<artifactId>java-design-patterns</artifactId> <artifactId>java-design-patterns</artifactId>
<version>1.14.0</version> <version>1.16.0</version>
</parent> </parent>
<artifactId>double-checked-locking</artifactId> <artifactId>double-checked-locking</artifactId>
<dependencies> <dependencies>

Some files were not shown because too many files have changed in this diff Show More