Compare commits

..

124 Commits

Author SHA1 Message Date
600553ec0f Merge pull request #583 from shaiktaj/patch-1
Updated ThreadSafeLazyLoadedivoryTower
2017-08-12 16:58:01 +03:00
54d8ec94be Merge pull request #599 from mookkiah/587_intercepting-filter
#587 sonarqube bugs
2017-07-30 08:49:17 +03:00
ffbf56f01a CQRS pattern: Add missing license headers 2017-07-29 21:41:11 +03:00
83fcbe0713 Merge pull request #603 from kevelbreh/patch-1
Update README.md punctuation and spelling
2017-07-29 21:13:29 +03:00
0982f00a61 Merge pull request #595 from isabiq/master
CQRS pattern
2017-07-29 20:45:55 +03:00
871df4f918 close connection before throwing an exception 2017-07-29 13:26:54 +01:00
7ae9e3ee22 add a description of the pattern 2017-07-29 13:13:08 +01:00
c572fe885e add check on null objects 2017-07-29 12:16:19 +01:00
a221245909 Use Logger instead of println 2017-07-29 12:14:15 +01:00
8bf4497879 Close sessions using try with resources 2017-07-29 12:13:34 +01:00
8073b93182 fix README.md 2017-07-29 12:04:04 +01:00
9b1a9aafc1 Update README.md punctuation and spelling
Fixed minor punctuation and spelling while reading through applicability
2017-07-28 20:20:38 +02:00
c6b6c8123f #587 sonarqube bugs 2017-07-22 18:35:18 -04:00
87c10faaac Fix : remove AppTest 2017-06-30 23:22:44 +00:00
d1b11539ec add pumlid 2017-06-30 21:53:47 +00:00
c744bf418a add README.md, cqrs.ucls and etc/cqrs.urm.puml 2017-06-30 21:41:25 +00:00
5873aeeb69 add cqrs module to parent pom.xml and update pom.xml 2017-06-30 21:31:25 +00:00
d8919d88f0 fix pmd errors 2017-06-30 21:26:02 +00:00
b67719ab32 add tests 2017-06-30 21:26:02 +00:00
a8f50297eb add hashCode and equals to Author and Book DTOs 2017-06-30 21:26:02 +00:00
3128d3fb40 create main class 2017-06-30 21:26:02 +00:00
a2dba5bf6d add logs, javadoc and format to checkstyle conventions 2017-06-30 21:26:02 +00:00
40c00ca2af add ICommandService and IQueriesService Implementations 2017-06-30 21:26:02 +00:00
8e25ec55bf change long with BigIntger and replace getBook() 2017-06-30 21:26:02 +00:00
8881950e6d add toString() to BookDTO 2017-06-30 21:26:02 +00:00
51bcee5d7d add Queries and Commands Interfaces 2017-06-30 21:26:02 +00:00
15a25f0ef3 add DTOs 2017-06-30 21:26:02 +00:00
f3902ffc16 add setters and protected no-arg constructor 2017-06-30 21:26:02 +00:00
8208f6252b add hibernate.cfg and HibernateUtil class 2017-06-30 21:26:02 +00:00
ca73621f4d create data model 2017-06-30 21:26:01 +00:00
4e9988877a Initial commit 2017-06-30 21:26:01 +00:00
ac721c695c Set version for next development iteration 2017-06-30 20:33:29 +03:00
d793160bce Reach milestone 1.16.0 2017-06-30 20:31:31 +03:00
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
edea7d2220 Refactored Function<Integer,T> to IntFunction<T>. 2017-06-14 11:58:05 -07:00
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
d13635533a Merge pull request #588 from prafful1/master
Few additions in readme
2017-06-14 20:14:09 +03:00
37cd4cbb3b Merge pull request #585 from fluency03/master
add RxJava to Real world examples of observer
2017-06-14 20:12:32 +03:00
3d29755842 Add license headers 2017-06-11 19:37:52 +03:00
5b47d48fc2 Merge pull request #573 from SrdjanPaunovic/extension-objects
#541 Extension objects pattern
2017-06-11 19:26:47 +03:00
73934e25e5 Refactored the Functional Interface to the Specialized Functional
Interface.
2017-06-09 17:17:56 -07:00
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
a0916aab6c Update README.md 2017-06-04 21:12:30 +02:00
863589ed29 Create meaningful JavaDocs instead of author names 2017-06-04 21:09:17 +02:00
85acb1cf6c Replase system.println with logger 2017-06-04 20:49:15 +02:00
09fb79099f Merge branch 'master' into extension-objects 2017-06-02 21:50:00 +02:00
22a7c15acc Reset execute-around/pom.xml from master 2017-06-02 21:42:59 +02:00
0182b840af add RxJava to Real world examples of observer 2017-05-30 13:22:52 +02:00
f87249e03b Add missing license headers 2017-05-29 21:53:05 +03:00
dd0ca2d16e Merge pull request #572 from 4lexis/master
#567 Marker Interface pull request
2017-05-29 21:41:27 +03:00
24f258848c Updated ThreadSafeLazyLoadedivoryTower
Added null check in private constructor to prevent instantiating by Reflection call
2017-05-25 11:04:30 -05:00
f2e35ec03d #567 fix version and javadoc 2017-05-23 01:38:02 +02:00
ae1d9cf7a8 Add missing license headers 2017-05-21 09:19:29 +03:00
1bbc597671 Merge pull request #575 from prafful1/master
Few additions in README
2017-05-21 09:11:00 +03:00
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
17039dc5e9 Update AdvancedSorceressTest.java 2017-05-18 20:27:16 +03:00
30dcbee2cc Update AdvancedSorceress.java 2017-05-18 20:26:47 +03:00
08901f3c26 Update AdvancedSorceress.java 2017-05-18 20:23:05 +03:00
c746004f73 Update AdvancedSorceress.java 2017-05-18 19:13:45 +03:00
09aa44ddcb Update App.java 2017-05-18 19:13:09 +03:00
992e76ac61 Merge pull request #566 from qpi/master
Event Queue pattern
2017-05-16 21:59:50 +03:00
167a43f72e updated model.png 2017-05-15 11:04:41 +02:00
fe1e45bd69 some fixes 2017-05-15 10:40:12 +02:00
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
0546223bba quick fix 2017-05-11 21:44:07 +02:00
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
0b57edd211 Merge pull request #568 from brandon-d-mckay/patch-1
Fix incorrect URL
2017-05-09 23:10:31 +03:00
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
6857486f27 #541 Create README.md file 2017-05-04 12:33:25 +02:00
1abd96a9c8 #567 checkstyle fix 2017-05-03 22:09:47 +02:00
08c4202852 #541 fix checkstyle errors 2017-05-03 22:08:04 +02:00
6ecf994258 #567 pom.xml align fix 2017-05-03 21:42:15 +02:00
0687a3f9f8 #541 pom.xml fix align 2017-05-03 21:40:54 +02:00
64337dff06 merge fix 2017-05-03 21:07:49 +02:00
34b09c75ec #567 added .gitignore 2017-05-03 20:51:51 +02:00
66c6f30c1c #567 Updated pom.xml 2017-05-03 20:34:27 +02:00
e413b116ac Merge branch 'master' of https://github.com/4lexis/java-design-patterns 2017-05-03 19:57:57 +02:00
6e0b3e37ea updated pom.xml 2017-05-03 19:57:14 +02:00
be3f4dce50 Update Picture 2017-05-03 19:49:25 +02:00
6d87f63ed5 Added README.md 2017-05-03 19:48:29 +02:00
c92a8daeda Class diagram & fix relation between units 2017-05-03 17:59:54 +02:00
20062faae6 All without Readme and pumlid 2017-05-03 17:50:35 +02:00
678524704c Test done 2017-05-03 13:21:13 +02:00
2b229d8ea1 Update AdvancedSorceressTest.java 2017-05-02 20:38:08 +03:00
03aa99c55f Update AdvancedSorceressTest.java 2017-05-02 20:18:05 +03:00
20a4c054a7 Merge branch 'master' of github.com:ytian90/java-design-patterns 2017-04-30 00:20:48 -07:00
3ed6cc19d2 fix Block 3 case 2017-04-30 00:19:50 -07:00
87a9387e62 Merge pull request #1 from iluwatar/master
Rebase
2017-04-29 16:03:26 -07:00
8530d01e10 code implemented 2017-04-29 16:35:57 +02:00
a34e7be9c2 check commit 2017-04-28 19:35:19 +02:00
ee3744cb0a added travis supported sound files 2017-04-28 13:47:56 +02:00
2830a407ba some fixes for testing in event queue 2017-04-28 12:19:57 +02:00
ccfb6709c7 Add files via upload 2017-04-28 13:00:01 +03:00
a1c5131304 Add files via upload 2017-04-28 12:59:09 +03:00
44401988d1 pom.xml fixes 2017-04-28 11:32:35 +02:00
6e8eaf7593 adding some test cases for the event queue 2017-04-28 10:38:40 +02:00
645e91ed23 first version of pattern without Tests 2017-04-27 13:58:58 +02:00
9e7e8a64f6 Fix incorrect URL 2017-04-26 04:22:44 -04:00
152b2762c3 read.me and the diagram is added 2017-04-22 17:16:38 +02:00
82b9f4fea7 Merge pull request #544 from kemitix/data-bus
[WIP] Data Bus
2017-04-22 15:17:31 +03:00
ff8d854a8d #467 data-bus: README.md: clean up 2017-04-16 06:51:10 +01:00
31f4b15e86 Merge pull request #565 from kapinuss/patch-1
Update ObjectPool.java
2017-04-16 08:17:56 +03:00
cd54cf5512 Merge pull request #563 from sunilmogadati/SonarQubeBlockerBugs
#507 SonarQube blocker severity bugs
2017-04-16 08:13:26 +03:00
798aee47b3 Update ObjectPool.java 2017-04-12 18:59:43 +03:00
dce767c1c5 first commit 2017-04-06 23:48:15 +02:00
60ebcc56f8 #507 SonarQube blocker severity bugs 2017-04-01 20:55:47 -06:00
2643dfa0b8 #467 data-bus: App: add notes about this implementation of the patter 2017-04-01 19:33:54 +01:00
c96ebcb197 #467 data-bus: App: add description of the pattern 2017-04-01 19:26:41 +01:00
b72d545349 #467 data-bus: members: StatusMemberTest: added 2017-04-01 19:17:09 +01:00
311bb79870 #467 data-bus: members: StatusMember: records start and stop times 2017-04-01 19:16:55 +01:00
f495a88e91 #467 data-bus: members: MessageCollectorMemberTest: added 2017-04-01 19:05:19 +01:00
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
b7a6a018e0 #467 data-bus: DataBusTest: added 2017-04-01 18:36:04 +01:00
46e0fa4825 #467 data-bus: pom.xml: add mockito dependency 2017-04-01 18:35:40 +01:00
86009f2261 #467 data-bus: add missing javadoc 2017-04-01 18:12:56 +01:00
146f367188 #467 data-bus: remove lombok 2017-04-01 18:12:32 +01:00
bc4d029a87 #467 data-bus: pom.xml: remove surefire plugin 2017-04-01 18:04:44 +01:00
960eee3d43 #467 update version 2017-04-01 18:04:16 +01:00
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
073d06c0ae Set version for next development iteration 2017-04-01 17:15:59 +03:00
6b795e52c3 #467 data-bus: README.md: updated for data-bus 2017-03-05 19:43:57 +00:00
b5bdf2d7d7 #467 data-bus: etc: add urm diagrams 2017-03-05 19:43:26 +00:00
3fd6887975 #467 data-bus: implement pattern 2017-03-05 18:55:34 +00:00
eecffb0ea5 #467 data-bus: add stub 2017-03-03 19:58:03 +00:00
215 changed files with 4920 additions and 217 deletions

View File

@ -29,7 +29,7 @@
<parent>
<artifactId>java-design-patterns</artifactId>
<groupId>com.iluwatar</groupId>
<version>1.15.0</version>
<version>1.17.0-SNAPSHOT</version>
</parent>
<artifactId>abstract-document</artifactId>
<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 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
* 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

View File

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

View File

@ -21,6 +21,9 @@ incompatible interfaces.
![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
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 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
* [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>
<groupId>com.iluwatar</groupId>
<artifactId>java-design-patterns</artifactId>
<version>1.15.0</version>
<version>1.17.0-SNAPSHOT</version>
</parent>
<artifactId>adapter</artifactId>
<dependencies>

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -29,7 +29,7 @@
<parent>
<artifactId>java-design-patterns</artifactId>
<groupId>com.iluwatar</groupId>
<version>1.15.0</version>
<version>1.17.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -29,7 +29,7 @@
<parent>
<artifactId>java-design-patterns</artifactId>
<groupId>com.iluwatar</groupId>
<version>1.15.0</version>
<version>1.17.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<dependencies>

29
cqrs/README.md Normal file
View File

@ -0,0 +1,29 @@
---
layout: pattern
title: CQRS
folder: cqrs
permalink: /patterns/cqrs/
pumlid: 7SPR4a0m3030gt00pR_RH6I8QQFouFgC_TfHb6gkd5Q7FQBx363ub4rYpoMTZKuDrYXqDX37HIuuyCPfPPTDfuuHREhGqBy0NUR0GNzAMYizMtq1
categories: Architectural
tags:
- Java
- Difficulty-Intermediate
---
## Intent
CQRS Command Query Responsibility Segregation - Separate the query side from the command side.
![alt text](./etc/cqrs.png "CQRS")
## Applicability
Use the CQRS pattern when
* you want to scale the queries and commands independently.
* you want to use different data models for queries and commands. Useful when dealing with complex domains.
* you want to use architectures like event sourcing or task based UI.
## Credits
* [Greg Young - CQRS, Task Based UIs, Event Sourcing agh!](http://codebetter.com/gregyoung/2010/02/16/cqrs-task-based-uis-event-sourcing-agh/)
* [Martin Fowler - CQRS](https://martinfowler.com/bliki/CQRS.html)
* [Oliver Wolf - CQRS for Great Good](https://www.youtube.com/watch?v=Ge53swja9Dw)

BIN
cqrs/etc/cqrs.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 103 KiB

115
cqrs/etc/cqrs.ucls Normal file
View File

@ -0,0 +1,115 @@
<?xml version="1.0" encoding="UTF-8"?>
<class-diagram version="1.2.0" icons="true" always-add-relationships="false" generalizations="true" realizations="true"
associations="true" dependencies="false" nesting-relationships="true" router="FAN">
<interface id="1" language="java" name="com.iluwatar.cqrs.commandes.ICommandService" project="cqrs"
file="/cqrs/src/main/java/com/iluwatar/cqrs/commandes/ICommandService.java" binary="false" corner="BOTTOM_RIGHT">
<position height="-1" width="-1" x="291" y="-49"/>
<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>
</interface>
<class id="2" language="java" name="com.iluwatar.cqrs.commandes.CommandServiceImpl" project="cqrs"
file="/cqrs/src/main/java/com/iluwatar/cqrs/commandes/CommandServiceImpl.java" binary="false" corner="BOTTOM_RIGHT">
<position height="263" width="256" x="170" y="87"/>
<display autosize="false" 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>
<interface id="3" language="java" name="com.iluwatar.cqrs.queries.IQueryService" project="cqrs"
file="/cqrs/src/main/java/com/iluwatar/cqrs/queries/IQueryService.java" binary="false" corner="BOTTOM_RIGHT">
<position height="182" width="248" x="176" y="428"/>
<display autosize="false" 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>
</interface>
<class id="4" language="java" name="com.iluwatar.cqrs.queries.QueryServiceImpl" project="cqrs"
file="/cqrs/src/main/java/com/iluwatar/cqrs/queries/QueryServiceImpl.java" binary="false" corner="BOTTOM_RIGHT">
<position height="258" width="253" x="169" y="665"/>
<display autosize="false" 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="5" language="java" name="com.iluwatar.cqrs.domain.model.Book" project="cqrs"
file="/cqrs/src/main/java/com/iluwatar/cqrs/domain/model/Book.java" binary="false" corner="BOTTOM_RIGHT">
<position height="326" width="158" x="778" y="-93"/>
<display autosize="false" 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="6" language="java" name="com.iluwatar.cqrs.dto.Book" project="cqrs"
file="/cqrs/src/main/java/com/iluwatar/cqrs/dto/Book.java" binary="false" corner="BOTTOM_RIGHT">
<position height="219" width="150" x="541" y="607"/>
<display autosize="false" 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="7" language="java" name="com.iluwatar.cqrs.domain.model.Author" project="cqrs"
file="/cqrs/src/main/java/com/iluwatar/cqrs/domain/model/Author.java" binary="false" corner="BOTTOM_RIGHT">
<position height="-1" width="-1" x="608" y="70"/>
<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="8" language="java" name="com.iluwatar.cqrs.dto.Author" project="cqrs"
file="/cqrs/src/main/java/com/iluwatar/cqrs/dto/Author.java" binary="false" corner="BOTTOM_RIGHT">
<position height="-1" width="-1" x="834" y="719"/>
<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="9" language="java" name="com.iluwatar.cqrs.util.HibernateUtil" project="cqrs"
file="/cqrs/src/main/java/com/iluwatar/cqrs/util/HibernateUtil.java" binary="false" corner="BOTTOM_RIGHT">
<position height="-1" width="-1" x="662" y="412"/>
<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>
<dependency id="10">
<end type="SOURCE" refId="4"/>
<end type="TARGET" refId="9"/>
</dependency>
<dependency id="11">
<end type="SOURCE" refId="2"/>
<end type="TARGET" refId="9"/>
</dependency>
<realization id="12">
<end type="SOURCE" refId="4"/>
<end type="TARGET" refId="3"/>
</realization>
<association id="13">
<end type="SOURCE" refId="5" navigable="false">
<attribute id="14" name="author"/>
<multiplicity id="15" minimum="0" maximum="1"/>
</end>
<end type="TARGET" refId="7" navigable="true"/>
<display labels="true" multiplicity="true"/>
</association>
<realization id="16">
<end type="SOURCE" refId="2"/>
<end type="TARGET" refId="1"/>
</realization>
<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>

124
cqrs/etc/cqrs.urm.puml Normal file
View File

@ -0,0 +1,124 @@
@startuml
package com.iluwatar.cqrs.util {
class HibernateUtil {
- LOGGER : Logger {static}
- SESSIONFACTORY : SessionFactory {static}
+ HibernateUtil()
- buildSessionFactory() : SessionFactory {static}
+ getSessionFactory() : SessionFactory {static}
}
}
package com.iluwatar.cqrs.app {
class App {
+ App()
+ main(args : String[]) {static}
}
}
package com.iluwatar.cqrs.dto {
class Author {
- email : String
- name : String
- username : String
+ Author()
+ Author(name : String, email : String, username : String)
+ equals(obj : Object) : boolean
+ getEmail() : String
+ getName() : String
+ getUsername() : String
+ hashCode() : int
+ toString() : String
}
class Book {
- price : double
- title : String
+ Book()
+ Book(title : String, price : double)
+ equals(obj : Object) : boolean
+ getPrice() : double
+ getTitle() : String
+ hashCode() : int
+ toString() : String
}
}
package com.iluwatar.cqrs.commandes {
class CommandServiceImpl {
- sessionFactory : SessionFactory
+ CommandServiceImpl()
+ authorCreated(username : String, name : String, email : String)
+ authorEmailUpdated(username : String, email : String)
+ authorNameUpdated(username : String, name : String)
+ authorUsernameUpdated(oldUsername : String, newUsername : String)
+ bookAddedToAuthor(title : String, price : double, username : String)
+ bookPriceUpdated(title : String, price : double)
+ bookTitleUpdated(oldTitle : String, newTitle : String)
- getAuthorByUsername(username : String) : Author
- getBookByTitle(title : String) : Book
}
interface ICommandService {
+ authorCreated(String, String, String) {abstract}
+ authorEmailUpdated(String, String) {abstract}
+ authorNameUpdated(String, String) {abstract}
+ authorUsernameUpdated(String, String) {abstract}
+ bookAddedToAuthor(String, double, String) {abstract}
+ bookPriceUpdated(String, double) {abstract}
+ bookTitleUpdated(String, String) {abstract}
}
}
package com.iluwatar.cqrs.queries {
interface IQueryService {
+ getAuthorBooks(String) : List<Book> {abstract}
+ getAuthorBooksCount(String) : BigInteger {abstract}
+ getAuthorByUsername(String) : Author {abstract}
+ getAuthorsCount() : BigInteger {abstract}
+ getBook(String) : Book {abstract}
}
class QueryServiceImpl {
- sessionFactory : SessionFactory
+ QueryServiceImpl()
+ getAuthorBooks(username : String) : List<Book>
+ getAuthorBooksCount(username : String) : BigInteger
+ getAuthorByUsername(username : String) : Author
+ getAuthorsCount() : BigInteger
+ getBook(title : String) : Book
}
}
package com.iluwatar.cqrs.domain.model {
class Author {
- email : String
- id : long
- name : String
- username : String
# Author()
+ Author(username : String, name : String, email : String)
+ getEmail() : String
+ getId() : long
+ getName() : String
+ getUsername() : String
+ setEmail(email : String)
+ setId(id : long)
+ setName(name : String)
+ setUsername(username : String)
+ toString() : String
}
class Book {
- author : Author
- id : long
- price : double
- title : String
# Book()
+ Book(title : String, price : double, author : Author)
+ getAuthor() : Author
+ getId() : long
+ getPrice() : double
+ getTitle() : String
+ setAuthor(author : Author)
+ setId(id : long)
+ setPrice(price : double)
+ setTitle(title : String)
+ toString() : String
}
}
Book --> "-author" Author
CommandServiceImpl ..|> ICommandService
QueryServiceImpl ..|> IQueryService
@enduml

42
cqrs/pom.xml Normal file
View File

@ -0,0 +1,42 @@
<?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>
<parent>
<groupId>com.iluwatar</groupId>
<artifactId>java-design-patterns</artifactId>
<version>1.17.0-SNAPSHOT</version>
</parent>
<artifactId>cqrs</artifactId>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,95 @@
/**
* 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.cqrs.app;
import java.math.BigInteger;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.iluwatar.cqrs.commandes.CommandServiceImpl;
import com.iluwatar.cqrs.commandes.ICommandService;
import com.iluwatar.cqrs.dto.Author;
import com.iluwatar.cqrs.dto.Book;
import com.iluwatar.cqrs.queries.IQueryService;
import com.iluwatar.cqrs.queries.QueryServiceImpl;
import com.iluwatar.cqrs.util.HibernateUtil;
/**
* CQRS : Command Query Responsibility Segregation. A pattern used to separate query services from commands or writes
* services. The pattern is very simple but it has many consequences. For example, it can be used to tackle down a
* complex domain, or to use other architectures that were hard to implement with the classical way.
*
* This implementation is an example of managing books and authors in a library. The persistence of books and authors is
* done according to the CQRS architecture. A command side that deals with a data model to persist(insert,update,delete)
* objects to a database. And a query side that uses native queries to get data from the database and return objects as
* DTOs (Data transfer Objects).
*
*/
public class App {
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
/**
* Program entry point
*
* @param args
* command line args
*/
public static void main(String[] args) {
ICommandService commands = new CommandServiceImpl();
// Create Authors and Books using CommandService
commands.authorCreated("eEvans", "Eric Evans", "eEvans@email.com");
commands.authorCreated("jBloch", "Joshua Bloch", "jBloch@email.com");
commands.authorCreated("mFowler", "Martin Fowler", "mFowler@email.com");
commands.bookAddedToAuthor("Domain-Driven Design", 60.08, "eEvans");
commands.bookAddedToAuthor("Effective Java", 40.54, "jBloch");
commands.bookAddedToAuthor("Java Puzzlers", 39.99, "jBloch");
commands.bookAddedToAuthor("Java Concurrency in Practice", 29.40, "jBloch");
commands.bookAddedToAuthor("Patterns of Enterprise Application Architecture", 54.01, "mFowler");
commands.bookAddedToAuthor("Domain Specific Languages", 48.89, "mFowler");
commands.authorNameUpdated("eEvans", "Eric J. Evans");
IQueryService queries = new QueryServiceImpl();
// Query the database using QueryService
Author nullAuthor = queries.getAuthorByUsername("username");
Author eEvans = queries.getAuthorByUsername("eEvans");
BigInteger jBlochBooksCount = queries.getAuthorBooksCount("jBloch");
BigInteger authorsCount = queries.getAuthorsCount();
Book dddBook = queries.getBook("Domain-Driven Design");
List<Book> jBlochBooks = queries.getAuthorBooks("jBloch");
LOGGER.info("Author username : {}", nullAuthor);
LOGGER.info("Author eEvans : {}", eEvans);
LOGGER.info("jBloch number of books : {}", jBlochBooksCount);
LOGGER.info("Number of authors : {}", authorsCount);
LOGGER.info("DDD book : {}", dddBook);
LOGGER.info("jBloch books : {}", jBlochBooks);
HibernateUtil.getSessionFactory().close();
}
}

View File

@ -0,0 +1,145 @@
/**
* 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.cqrs.commandes;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import com.iluwatar.cqrs.domain.model.Author;
import com.iluwatar.cqrs.domain.model.Book;
import com.iluwatar.cqrs.util.HibernateUtil;
/**
* This class is an implementation of {@link ICommandService} interface. It uses Hibernate as an api for persistence.
*
*/
public class CommandServiceImpl implements ICommandService {
private SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
private Author getAuthorByUsername(String username) {
Author author = null;
try (Session session = sessionFactory.openSession()) {
Query query = session.createQuery("from Author where username=:username");
query.setParameter("username", username);
author = (Author) query.uniqueResult();
}
if (author == null) {
HibernateUtil.getSessionFactory().close();
throw new NullPointerException("Author " + username + " doesn't exist!");
}
return author;
}
private Book getBookByTitle(String title) {
Book book = null;
try (Session session = sessionFactory.openSession()) {
Query query = session.createQuery("from Book where title=:title");
query.setParameter("title", title);
book = (Book) query.uniqueResult();
}
if (book == null) {
HibernateUtil.getSessionFactory().close();
throw new NullPointerException("Book " + title + " doesn't exist!");
}
return book;
}
@Override
public void authorCreated(String username, String name, String email) {
Author author = new Author(username, name, email);
try (Session session = sessionFactory.openSession()) {
session.beginTransaction();
session.save(author);
session.getTransaction().commit();
}
}
@Override
public void bookAddedToAuthor(String title, double price, String username) {
Author author = getAuthorByUsername(username);
Book book = new Book(title, price, author);
try (Session session = sessionFactory.openSession()) {
session.beginTransaction();
session.save(book);
session.getTransaction().commit();
}
}
@Override
public void authorNameUpdated(String username, String name) {
Author author = getAuthorByUsername(username);
author.setName(name);
try (Session session = sessionFactory.openSession()) {
session.beginTransaction();
session.update(author);
session.getTransaction().commit();
}
}
@Override
public void authorUsernameUpdated(String oldUsername, String newUsername) {
Author author = getAuthorByUsername(oldUsername);
author.setUsername(newUsername);
try (Session session = sessionFactory.openSession()) {
session.beginTransaction();
session.update(author);
session.getTransaction().commit();
}
}
@Override
public void authorEmailUpdated(String username, String email) {
Author author = getAuthorByUsername(username);
author.setEmail(email);
try (Session session = sessionFactory.openSession()) {
session.beginTransaction();
session.update(author);
session.getTransaction().commit();
}
}
@Override
public void bookTitleUpdated(String oldTitle, String newTitle) {
Book book = getBookByTitle(oldTitle);
book.setTitle(newTitle);
try (Session session = sessionFactory.openSession()) {
session.beginTransaction();
session.update(book);
session.getTransaction().commit();
}
}
@Override
public void bookPriceUpdated(String title, double price) {
Book book = getBookByTitle(title);
book.setPrice(price);
try (Session session = sessionFactory.openSession()) {
session.beginTransaction();
session.update(book);
session.getTransaction().commit();
}
}
}

View File

@ -0,0 +1,45 @@
/**
* 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.cqrs.commandes;
/**
* This interface represents the commands of the CQRS pattern
*
*/
public interface ICommandService {
void authorCreated(String username, String name, String email);
void bookAddedToAuthor(String title, double price, String username);
void authorNameUpdated(String username, String name);
void authorUsernameUpdated(String oldUsername, String newUsername);
void authorEmailUpdated(String username, String email);
void bookTitleUpdated(String oldTitle, String newTitle);
void bookPriceUpdated(String title, double price);
}

View File

@ -0,0 +1,100 @@
/**
* 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.cqrs.domain.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
/**
* This is an Author entity. It is used by Hibernate for persistence.
*
*/
@Entity
public class Author {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
private String username;
private String name;
private String email;
/**
*
* @param username
* username of the author
* @param name
* name of the author
* @param email
* email of the author
*/
public Author(String username, String name, String email) {
super();
this.username = username;
this.name = name;
this.email = email;
}
protected Author() {
super();
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "Author [name=" + name + ", email=" + email + "]";
}
}

View File

@ -0,0 +1,102 @@
/**
* 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.cqrs.domain.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
/**
* This is a Book entity. It is used by Hibernate for persistence. Many books can be written by one {@link Author}
*
*/
@Entity
public class Book {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
private String title;
private double price;
@ManyToOne
private Author author;
/**
*
* @param title
* title of the book
* @param price
* price of the book
* @param author
* author of the book
*/
public Book(String title, double price, Author author) {
super();
this.title = title;
this.price = price;
this.author = author;
}
protected Book() {
super();
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public Author getAuthor() {
return author;
}
public void setAuthor(Author author) {
this.author = author;
}
@Override
public String toString() {
return "Book [title=" + title + ", price=" + price + ", author=" + author + "]";
}
}

View File

@ -0,0 +1,93 @@
/**
* 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.cqrs.dto;
import java.util.Objects;
/**
*
* This is a DTO (Data Transfer Object) author, contains only useful information to be returned
*
*/
public class Author {
private String name;
private String email;
private String username;
/**
*
* @param name
* name of the author
* @param email
* email of the author
* @param username
* username of the author
*/
public Author(String name, String email, String username) {
super();
this.name = name;
this.email = email;
this.username = username;
}
public Author() {
super();
}
public String getName() {
return name;
}
public String getEmail() {
return email;
}
public String getUsername() {
return username;
}
@Override
public String toString() {
return "AuthorDTO [name=" + name + ", email=" + email + ", username=" + username + "]";
}
@Override
public int hashCode() {
return Objects.hash(username, name, email);
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof Author)) {
return false;
}
Author other = (Author) obj;
return username.equals(other.getUsername()) && email.equals(other.getEmail()) && name.equals(other.getName());
}
}

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.cqrs.dto;
import java.util.Objects;
/**
*
* This is a DTO (Data Transfer Object) book, contains only useful information to be returned
*
*/
public class Book {
private String title;
private double price;
/**
*
* @param title
* title of the book
* @param price
* price of the book
*/
public Book(String title, double price) {
super();
this.title = title;
this.price = price;
}
public Book() {
super();
}
public String getTitle() {
return title;
}
public double getPrice() {
return price;
}
@Override
public String toString() {
return "BookDTO [title=" + title + ", price=" + price + "]";
}
@Override
public int hashCode() {
return Objects.hash(title, price);
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof Book)) {
return false;
}
Book book = (Book) obj;
return title.equals(book.getTitle()) && price == book.getPrice();
}
}

View File

@ -0,0 +1,48 @@
/**
* 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.cqrs.queries;
import java.math.BigInteger;
import java.util.List;
import com.iluwatar.cqrs.dto.Author;
import com.iluwatar.cqrs.dto.Book;
/**
*
* This interface represents the query methods of the CQRS pattern
*
*/
public interface IQueryService {
Author getAuthorByUsername(String username);
Book getBook(String title);
List<Book> getAuthorBooks(String username);
BigInteger getAuthorBooksCount(String username);
BigInteger getAuthorsCount();
}

View File

@ -0,0 +1,105 @@
/**
* 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.cqrs.queries;
import java.math.BigInteger;
import java.util.List;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.transform.Transformers;
import com.iluwatar.cqrs.dto.Author;
import com.iluwatar.cqrs.dto.Book;
import com.iluwatar.cqrs.util.HibernateUtil;
/**
* This class is an implementation of {@link IQueryService}. It uses Hibernate native queries to return DTOs from the
* database.
*
*/
public class QueryServiceImpl implements IQueryService {
private SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
@Override
public Author getAuthorByUsername(String username) {
Author authorDTo = null;
try (Session session = sessionFactory.openSession()) {
SQLQuery sqlQuery = session
.createSQLQuery("SELECT a.username as \"username\", a.name as \"name\", a.email as \"email\""
+ "FROM Author a where a.username=:username");
sqlQuery.setParameter("username", username);
authorDTo = (Author) sqlQuery.setResultTransformer(Transformers.aliasToBean(Author.class)).uniqueResult();
}
return authorDTo;
}
@Override
public Book getBook(String title) {
Book bookDTo = null;
try (Session session = sessionFactory.openSession()) {
SQLQuery sqlQuery = session
.createSQLQuery("SELECT b.title as \"title\", b.price as \"price\"" + " FROM Book b where b.title=:title");
sqlQuery.setParameter("title", title);
bookDTo = (Book) sqlQuery.setResultTransformer(Transformers.aliasToBean(Book.class)).uniqueResult();
}
return bookDTo;
}
@Override
public List<Book> getAuthorBooks(String username) {
List<Book> bookDTos = null;
try (Session session = sessionFactory.openSession()) {
SQLQuery sqlQuery = session.createSQLQuery("SELECT b.title as \"title\", b.price as \"price\""
+ " FROM Author a , Book b where b.author_id = a.id and a.username=:username");
sqlQuery.setParameter("username", username);
bookDTos = sqlQuery.setResultTransformer(Transformers.aliasToBean(Book.class)).list();
}
return bookDTos;
}
@Override
public BigInteger getAuthorBooksCount(String username) {
BigInteger bookcount = null;
try (Session session = sessionFactory.openSession()) {
SQLQuery sqlQuery = session.createSQLQuery(
"SELECT count(b.title)" + " FROM Book b, Author a where b.author_id = a.id and a.username=:username");
sqlQuery.setParameter("username", username);
bookcount = (BigInteger) sqlQuery.uniqueResult();
}
return bookcount;
}
@Override
public BigInteger getAuthorsCount() {
BigInteger authorcount = null;
try (Session session = sessionFactory.openSession()) {
SQLQuery sqlQuery = session.createSQLQuery("SELECT count(id) from Author");
authorcount = (BigInteger) sqlQuery.uniqueResult();
}
return authorcount;
}
}

View File

@ -0,0 +1,58 @@
/**
* 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.cqrs.util;
import org.hibernate.SessionFactory;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* This class simply returns one instance of {@link SessionFactory} initialized when the application is started
*
*/
public class HibernateUtil {
private static final SessionFactory SESSIONFACTORY = buildSessionFactory();
private static final Logger LOGGER = LoggerFactory.getLogger(HibernateUtil.class);
private static SessionFactory buildSessionFactory() {
// configures settings from hibernate.cfg.xml
final StandardServiceRegistry registry = new StandardServiceRegistryBuilder().configure().build();
try {
return new MetadataSources(registry).buildMetadata().buildSessionFactory();
} catch (Exception ex) {
StandardServiceRegistryBuilder.destroy(registry);
LOGGER.error("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return SESSIONFACTORY;
}
}

View File

@ -0,0 +1,39 @@
<?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.
-->
<!DOCTYPE hibernate-configuration SYSTEM
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="dialect">org.hibernate.dialect.H2Dialect</property>
<property name="connection.driver_class">org.h2.Driver</property>
<property name="connection.url">jdbc:h2:mem:test</property>
<property name="connection.username">sa</property>
<property name="hbm2ddl.auto">create</property>
<mapping class="com.iluwatar.cqrs.domain.model.Author" />
<mapping class="com.iluwatar.cqrs.domain.model.Book" />
</session-factory>
</hibernate-configuration>

View File

@ -0,0 +1,37 @@
<?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.
-->
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
</pattern>
</encoder>
</appender>
<root level="info">
<appender-ref ref="STDOUT" />
</root>
</configuration>

View File

@ -0,0 +1,120 @@
/**
* 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.cqrs;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.math.BigInteger;
import java.util.List;
import org.junit.BeforeClass;
import org.junit.Test;
import com.iluwatar.cqrs.commandes.CommandServiceImpl;
import com.iluwatar.cqrs.commandes.ICommandService;
import com.iluwatar.cqrs.dto.Author;
import com.iluwatar.cqrs.dto.Book;
import com.iluwatar.cqrs.queries.IQueryService;
import com.iluwatar.cqrs.queries.QueryServiceImpl;
/**
* Integration test of IQueryService and ICommandService with h2 data
*
*/
public class IntegrationTest {
private static IQueryService queryService;
private static ICommandService commandService;
@BeforeClass
public static void initialize() {
commandService = new CommandServiceImpl();
queryService = new QueryServiceImpl();
}
@BeforeClass
public static void populateDatabase() {
// create first author1
commandService.authorCreated("username1", "name1", "email1");
// create author1 and update all its data
commandService.authorCreated("username2", "name2", "email2");
commandService.authorEmailUpdated("username2", "new_email2");
commandService.authorNameUpdated("username2", "new_name2");
commandService.authorUsernameUpdated("username2", "new_username2");
// add book1 to author1
commandService.bookAddedToAuthor("title1", 10, "username1");
// add book2 to author1 and update all its data
commandService.bookAddedToAuthor("title2", 20, "username1");
commandService.bookPriceUpdated("title2", 30);
commandService.bookTitleUpdated("title2", "new_title2");
}
@Test
public void testGetAuthorByUsername() {
Author author = queryService.getAuthorByUsername("username1");
assertEquals("username1", author.getUsername());
assertEquals("name1", author.getName());
assertEquals("email1", author.getEmail());
}
@Test
public void testGetUpdatedAuthorByUsername() {
Author author = queryService.getAuthorByUsername("new_username2");
Author expectedAuthor = new Author("new_name2", "new_email2", "new_username2");
assertEquals(expectedAuthor, author);
}
@Test
public void testGetBook() {
Book book = queryService.getBook("title1");
assertEquals("title1", book.getTitle());
assertEquals(10, book.getPrice(), 0);
}
@Test
public void testGetAuthorBooks() {
List<Book> books = queryService.getAuthorBooks("username1");
assertTrue(books.size() == 2);
assertTrue(books.contains(new Book("title1", 10)));
assertTrue(books.contains(new Book("new_title2", 30)));
}
@Test
public void testGetAuthorBooksCount() {
BigInteger bookCount = queryService.getAuthorBooksCount("username1");
assertEquals(new BigInteger("2"), bookCount);
}
@Test
public void testGetAuthorsCount() {
BigInteger authorCount = queryService.getAuthorsCount();
assertEquals(new BigInteger("2"), authorCount);
}
}

View File

@ -0,0 +1,39 @@
<?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.
-->
<!DOCTYPE hibernate-configuration SYSTEM
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="dialect">org.hibernate.dialect.H2Dialect</property>
<property name="connection.driver_class">org.h2.Driver</property>
<property name="connection.url">jdbc:h2:mem:test</property>
<property name="connection.username">sa</property>
<property name="hbm2ddl.auto">create</property>
<mapping class="com.iluwatar.cqrs.domain.model.Author" />
<mapping class="com.iluwatar.cqrs.domain.model.Book" />
</session-factory>
</hibernate-configuration>

View File

@ -0,0 +1,37 @@
<?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.
-->
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
</pattern>
</encoder>
</appender>
<root level="info">
<appender-ref ref="STDOUT" />
</root>
</configuration>

View File

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

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.17.0-SNAPSHOT</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>
<groupId>com.iluwatar</groupId>
<artifactId>java-design-patterns</artifactId>
<version>1.15.0</version>
<version>1.17.0-SNAPSHOT</version>
</parent>
<artifactId>data-mapper</artifactId>
<dependencies>

View File

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

View File

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

View File

@ -29,7 +29,7 @@
<parent>
<groupId>com.iluwatar</groupId>
<artifactId>java-design-patterns</artifactId>
<version>1.15.0</version>
<version>1.17.0-SNAPSHOT</version>
</parent>
<artifactId>dependency-injection</artifactId>
<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
* 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
@ -26,7 +26,6 @@ import com.google.inject.Guice;
import com.google.inject.Injector;
/**
*
* 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:
* - High-level modules should not depend on low-level modules. Both should depend on abstractions.
@ -36,21 +35,21 @@ import com.google.inject.Injector;
* naive implementation violating the inversion of control principle. It depends directly on a
* concrete implementation which cannot be changed.
* <p>
* The second wizard ({@link AdvancedWizard}) is more flexible. It does not depend on any concrete
* implementation but abstraction. It utilizes Dependency Injection pattern allowing its
* {@link Tobacco} dependency to be injected through its constructor. This way, handling the
* dependency is no longer the wizard's responsibility. It is resolved outside the wizard class.
* The second and third wizards({@link AdvancedWizard} and {@link AdvancedSorceress}) are more flexible.
* They do not depend on any concrete implementation but abstraction. They utilizes Dependency Injection
* pattern allowing their {@link Tobacco} dependency to be injected through constructor ({@link AdvancedWizard})
* or setter ({@link AdvancedSorceress}). This way, handling the dependency is no longer the wizard's
* responsibility. It is resolved outside the wizard class.
* <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
* used to create {@link GuiceWizard} object with correct dependencies.
*
*/
public class App {
/**
* Program entry point
*
*
* @param args command line args
*/
public static void main(String[] args) {
@ -60,6 +59,10 @@ public class App {
AdvancedWizard advancedWizard = new AdvancedWizard(new SecondBreakfastTobacco());
advancedWizard.smoke();
AdvancedSorceress advancedSorceress = new AdvancedSorceress();
advancedSorceress.setTobacco(new SecondBreakfastTobacco());
advancedSorceress.smoke();
Injector injector = Guice.createInjector(new TobaccoModule());
GuiceWizard guiceWizard = injector.getInstance(GuiceWizard.class);
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

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

View File

@ -29,7 +29,7 @@
<parent>
<groupId>com.iluwatar</groupId>
<artifactId>java-design-patterns</artifactId>
<version>1.15.0</version>
<version>1.17.0-SNAPSHOT</version>
</parent>
<artifactId>double-dispatch</artifactId>
<dependencies>

View File

@ -28,7 +28,7 @@
<parent>
<groupId>com.iluwatar</groupId>
<artifactId>java-design-patterns</artifactId>
<version>1.15.0</version>
<version>1.17.0-SNAPSHOT</version>
</parent>
<artifactId>event-aggregator</artifactId>
<dependencies>

View File

@ -29,7 +29,7 @@
<parent>
<groupId>com.iluwatar</groupId>
<artifactId>java-design-patterns</artifactId>
<version>1.15.0</version>
<version>1.17.0-SNAPSHOT</version>
</parent>
<artifactId>event-asynchronous</artifactId>
<dependencies>

View File

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

View File

@ -31,7 +31,7 @@
<parent>
<groupId>com.iluwatar</groupId>
<artifactId>java-design-patterns</artifactId>
<version>1.15.0</version>
<version>1.17.0-SNAPSHOT</version>
</parent>
<artifactId>event-driven-architecture</artifactId>

29
event-queue/README.md Normal file
View File

@ -0,0 +1,29 @@
---
layout: pattern
title: Event Queue
folder: event-queue
permalink: /patterns/event-queue/
categories: Concurrency
tags:
- Java
- Difficulty Intermediate
- Queue
---
## Intent
Event Queue is a good pattern if You have a limited accesibility resource (for example:
Audio or Database), but You need to handle all the requests that want to use that.
It puts all the requests in a queue and process them asynchronously.
Gives the resource for the event when it is the next in the queue and in same time
removes it from the queue.
![alt text](./etc/model.png "Event Queue")
## Applicability
Use the Event Queue pattern when
* You have a limited accesibility resource and the asynchronous process is acceptable to reach that
## Credits
* [Mihaly Kuprivecz - Event Queue] (http://gameprogrammingpatterns.com/event-queue.html)

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1,26 @@
@startuml
package com.iluwatar.event.queue {
class App {
+ App()
+ getAudioStream(filePath : String) : AudioInputStream {static}
+ main(args : String[]) {static}
}
class Audio {
- MAX_PENDING : int {static}
- headIndex : int {static}
- pendingAudio : PlayMessage[] {static}
- tailIndex : int {static}
- updateThread : Thread {static}
+ Audio()
+ init() {static}
+ playSound(stream : AudioInputStream, volume : float) {static}
+ stopService() {static}
+ update() {static}
}
class PlayMessage {
~ stream : AudioInputStream
~ volume : float
+ PlayMessage()
}
}
@enduml

BIN
event-queue/etc/model.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

View File

@ -0,0 +1,36 @@
<?xml version="1.0" encoding="UTF-8"?>
<class-diagram version="1.2.0" 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.event.queue.Audio" project="event-queue"
file="/event-queue/src/main/java/com/iluwatar/event/queue/Audio.java" binary="false" corner="BOTTOM_RIGHT">
<position height="-1" width="-1" x="285" y="179"/>
<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.event.queue.PlayMessage" project="event-queue"
file="/event-queue/src/main/java/com/iluwatar/event/queue/PlayMessage.java" binary="false" corner="BOTTOM_RIGHT">
<position height="-1" width="-1" x="633" y="179"/>
<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>
<association id="3">
<end type="SOURCE" refId="1" navigable="false">
<attribute id="4" name="pendingAudio"/>
<multiplicity id="5" minimum="0" maximum="2147483647"/>
</end>
<end type="TARGET" refId="2" 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>

43
event-queue/pom.xml Normal file
View File

@ -0,0 +1,43 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
The MIT License
Copyright (c) 2014-2016 Ilkka Sepp<70>l<EFBFBD>
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">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>java-design-patterns</artifactId>
<groupId>com.iluwatar</groupId>
<version>1.17.0-SNAPSHOT</version>
</parent>
<artifactId>event-queue</artifactId>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,59 @@
/**
* 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.event.queue;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import javax.sound.sampled.UnsupportedAudioFileException;
/**
* Event or message queues provide an asynchronous communications protocol, meaning that the sender
* and receiver of the message do not need to interact with the message queue at the same time.
* Events or messages placed onto the queue are stored until the recipient retrieves them. Event
* or message queues have implicit or explicit limits on the size of data that may be transmitted
* in a single message and the number of messages that may remain outstanding on the queue.
* A queue stores a series of notifications or requests in first-in, first-out order.
* Sending a notification enqueues the request and returns. The request processor then processes
* items from the queue at a later time.
*/
public class App {
/**
* Program entry point.
*
* @param args command line args
* @throws IOException when there is a problem with the audio file loading
* @throws UnsupportedAudioFileException when the loaded audio file is unsupported
*/
public static void main(String[] args) throws UnsupportedAudioFileException, IOException {
Audio.playSound(Audio.getAudioStream("./etc/Bass-Drum-1.wav"), -10.0f);
Audio.playSound(Audio.getAudioStream("./etc/Closed-Hi-Hat-1.wav"), -8.0f);
System.out.println("Press Enter key to stop the program...");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
br.read();
Audio.stopService();
}
}

View File

@ -0,0 +1,169 @@
/**
* 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.event.queue;
import java.io.File;
import java.io.IOException;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
/**
* This class implements the Event Queue pattern.
* @author mkuprivecz
*
*/
public class Audio {
private static final int MAX_PENDING = 16;
private static int headIndex;
private static int tailIndex;
private static Thread updateThread = null;
private static PlayMessage[] pendingAudio = new PlayMessage[MAX_PENDING];
/**
* This method stops the Update Method's thread.
*/
public static synchronized void stopService() {
if (updateThread != null) {
updateThread.interrupt();
}
}
/**
* This method stops the Update Method's thread.
* @return boolean
*/
public static synchronized boolean isServiceRunning() {
if (updateThread != null && updateThread.isAlive() ) {
return true;
} else {
return false;
}
}
/**
* Starts the thread for the Update Method pattern if it was not started previously.
* Also when the thread is is ready initializes the indexes of the queue
*/
public static void init() {
if (updateThread == null) {
updateThread = new Thread(new Runnable() {
public void run() {
while (!Thread.currentThread().isInterrupted()) {
Audio.update();
}
}
});
}
startThread();
}
/**
* This is a synchronized thread starter
*/
public static synchronized void startThread() {
if (!updateThread.isAlive()) {
updateThread.start();
headIndex = 0;
tailIndex = 0;
}
}
/**
* This method adds a new audio into the queue.
* @param stream is the AudioInputStream for the method
* @param volume is the level of the audio's volume
*/
public static void playSound(AudioInputStream stream, float volume) {
init();
// Walk the pending requests.
for (int i = headIndex; i != tailIndex; i = (i + 1) % MAX_PENDING) {
if (getPendingAudio()[i].getStream() == stream) {
// Use the larger of the two volumes.
getPendingAudio()[i].setVolume(Math.max(volume, getPendingAudio()[i].getVolume()));
// Don't need to enqueue.
return;
}
}
getPendingAudio()[tailIndex] = new PlayMessage(stream, volume);
tailIndex = (tailIndex + 1) % MAX_PENDING;
}
/**
* This method uses the Update Method pattern.
* It takes the audio from the queue and plays it
*/
public static void update() {
// If there are no pending requests, do nothing.
if (headIndex == tailIndex) {
return;
}
Clip clip = null;
try {
AudioInputStream audioStream = getPendingAudio()[headIndex].getStream();
headIndex++;
clip = AudioSystem.getClip();
clip.open(audioStream);
clip.start();
} catch (LineUnavailableException e) {
System.err.println("Error occoured while loading the audio: The line is unavailable");
e.printStackTrace();
} catch (IOException e) {
System.err.println("Input/Output error while loading the audio");
e.printStackTrace();
} catch (IllegalArgumentException e) {
System.err.println("The system doesn't support the sound: " + e.getMessage());
}
}
/**
* Returns the AudioInputStream of a file
* @param filePath is the path of the audio file
* @return AudioInputStream
* @throws UnsupportedAudioFileException when the audio file is not supported
* @throws IOException when the file is not readable
*/
public static AudioInputStream getAudioStream(String filePath)
throws UnsupportedAudioFileException, IOException {
return AudioSystem.getAudioInputStream(new File(filePath).getAbsoluteFile());
}
/**
* Returns with the message array of the queue
* @return PlayMessage[]
*/
public static PlayMessage[] getPendingAudio() {
return pendingAudio;
}
}

View File

@ -0,0 +1,59 @@
/**
* 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.event.queue;
import javax.sound.sampled.AudioInputStream;
/**
* The Event Queue's queue will store the instances of this class.
* @author mkuprivecz
*
*/
public class PlayMessage {
private AudioInputStream stream;
private float volume;
public PlayMessage(AudioInputStream stream, float volume) {
setStream(stream);
setVolume(volume);
}
public AudioInputStream getStream() {
return stream;
}
private void setStream(AudioInputStream stream) {
this.stream = stream;
}
public float getVolume() {
return volume;
}
public void setVolume(float volume) {
this.volume = volume;
}
}

View File

@ -0,0 +1,77 @@
/**
* 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.event.queue;
import static org.junit.Assert.*;
import java.io.IOException;
import javax.sound.sampled.UnsupportedAudioFileException;
import org.junit.Test;
/**
* Testing the Audio service of the Queue
* @author mkuprivecz
*
*/
public class AudioTest {
/**
* Test here that the playSound method works correctly
* @throws UnsupportedAudioFileException when the audio file is not supported
* @throws IOException when the file is not readable
* @throws InterruptedException when the test is interrupted externally
*/
@Test
public void testPlaySound() throws UnsupportedAudioFileException, IOException, InterruptedException {
Audio.playSound(Audio.getAudioStream("./etc/Bass-Drum-1.wav"), -10.0f);
// test that service is started
assertTrue(Audio.isServiceRunning());
// adding a small pause to be sure that the sound is ended
Thread.sleep(5000);
// test that service is finished
assertFalse(!Audio.isServiceRunning());
}
/**
* Test here that the Queue
* @throws UnsupportedAudioFileException when the audio file is not supported
* @throws IOException when the file is not readable
* @throws InterruptedException when the test is interrupted externally
*/
@Test
public void testQueue() throws UnsupportedAudioFileException, IOException, InterruptedException {
Audio.playSound(Audio.getAudioStream("./etc/Bass-Drum-1.aif"), -10.0f);
Audio.playSound(Audio.getAudioStream("./etc/Bass-Drum-1.aif"), -10.0f);
Audio.playSound(Audio.getAudioStream("./etc/Bass-Drum-1.aif"), -10.0f);
assertTrue(Audio.getPendingAudio().length > 0);
// test that service is started
assertTrue(Audio.isServiceRunning());
// adding a small pause to be sure that the sound is ended
Thread.sleep(10000);
// test that service is finished
assertFalse(!Audio.isServiceRunning());
}
}

View File

@ -29,7 +29,7 @@
<parent>
<groupId>com.iluwatar</groupId>
<artifactId>java-design-patterns</artifactId>
<version>1.15.0</version>
<version>1.17.0-SNAPSHOT</version>
</parent>
<artifactId>execute-around</artifactId>
<dependencies>

View File

@ -0,0 +1,28 @@
---
layout: pattern
title: Extension objects
folder: extension-objects
permalink: /patterns/extension-objects/
categories: Behavioral
tags:
- Java
- Difficulty-Intermediate
---
## Intent
Anticipate that an objects interface needs to be extended in the future. Additional
interfaces are defined by extension objects.
![Extension_objects](./etc/extension_obj.png "Extension objects")
## Applicability
Use the Extension Objects pattern when:
* you need to support the addition of new or unforeseen interfaces to existing classes and you don't want to impact clients that don't need this new interface. Extension Objects lets you keep related operations together by defining them in a separate class
* a class representing a key abstraction plays different roles for different clients. The number of roles the class can play should be open-ended. There is a need to preserve the key abstraction itself. For example, a customer object is still a customer object even if different subsystems view it differently.
* a class should be extensible with new behavior without subclassing from it.
## Real world examples
* [OpenDoc](https://en.wikipedia.org/wiki/OpenDoc)
* [Object Linking and Embedding](https://en.wikipedia.org/wiki/Object_Linking_and_Embedding)

View File

@ -0,0 +1,2 @@
@startuml
@enduml

Binary file not shown.

After

Width:  |  Height:  |  Size: 37 KiB

View File

@ -0,0 +1,180 @@
<?xml version="1.0" encoding="UTF-8"?>
<class-diagram version="1.2.0" 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="concreteextensions.Soldier" project="extension-objects"
file="/extension-objects/src/main/java/concreteextensions/Soldier.java" binary="false" corner="BOTTOM_RIGHT">
<position height="-1" width="-1" x="483" y="339"/>
<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="units.Unit" project="extension-objects"
file="/extension-objects/src/main/java/units/Unit.java" binary="false" corner="BOTTOM_RIGHT">
<position height="-1" width="-1" x="192" y="115"/>
<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>
<interface id="3" language="java" name="abstractextensions.SoldierExtension" project="extension-objects"
file="/extension-objects/src/main/java/abstractextensions/SoldierExtension.java" binary="false"
corner="BOTTOM_RIGHT">
<position height="-1" width="-1" x="510" y="229"/>
<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>
</interface>
<interface id="4" language="java" name="abstractextensions.UnitExtension" project="extension-objects"
file="/extension-objects/src/main/java/abstractextensions/UnitExtension.java" binary="false" corner="BOTTOM_RIGHT">
<position height="-1" width="-1" x="510" y="116"/>
<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>
</interface>
<class id="5" language="java" name="units.SoldierUnit" project="extension-objects"
file="/extension-objects/src/main/java/units/SoldierUnit.java" binary="false" corner="BOTTOM_RIGHT">
<position height="-1" width="-1" x="157" y="355"/>
<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="6" language="java" name="concreteextensions.Sergeant" project="extension-objects"
file="/extension-objects/src/main/java/concreteextensions/Sergeant.java" binary="false" corner="BOTTOM_RIGHT">
<position height="-1" width="-1" x="650" y="375"/>
<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>
<interface id="7" language="java" name="abstractextensions.SergeantExtension" project="extension-objects"
file="/extension-objects/src/main/java/abstractextensions/SergeantExtension.java" binary="false"
corner="BOTTOM_RIGHT">
<position height="-1" width="-1" x="672" y="230"/>
<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>
</interface>
<class id="8" language="java" name="units.SergeantUnit" project="extension-objects"
file="/extension-objects/src/main/java/units/SergeantUnit.java" binary="false" corner="BOTTOM_RIGHT">
<position height="-1" width="-1" x="315" y="460"/>
<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="9" language="java" name="units.CommanderUnit" project="extension-objects"
file="/extension-objects/src/main/java/units/CommanderUnit.java" binary="false" corner="BOTTOM_RIGHT">
<position height="99" width="210" x="429" y="476"/>
<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="10" language="java" name="concreteextensions.Commander" project="extension-objects"
file="/extension-objects/src/main/java/concreteextensions/Commander.java" binary="false" corner="BOTTOM_RIGHT">
<position height="-1" width="-1" x="823" y="477"/>
<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>
<interface id="11" language="java" name="abstractextensions.CommanderExtension" project="extension-objects"
file="/extension-objects/src/main/java/abstractextensions/CommanderExtension.java" binary="false"
corner="BOTTOM_RIGHT">
<position height="-1" width="-1" x="827" y="217"/>
<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>
</interface>
<realization id="12">
<end type="SOURCE" refId="1"/>
<end type="TARGET" refId="3"/>
</realization>
<realization id="13">
<end type="SOURCE" refId="10"/>
<end type="TARGET" refId="11"/>
</realization>
<generalization id="14">
<end type="SOURCE" refId="9"/>
<end type="TARGET" refId="2"/>
</generalization>
<association id="15">
<end type="SOURCE" refId="1" navigable="false">
<attribute id="16" name="unit"/>
<multiplicity id="17" minimum="0" maximum="1"/>
</end>
<end type="TARGET" refId="5" navigable="true"/>
<display labels="true" multiplicity="true"/>
</association>
<association id="18">
<end type="SOURCE" refId="10" navigable="false">
<attribute id="19" name="unit"/>
<multiplicity id="20" minimum="0" maximum="1"/>
</end>
<end type="TARGET" refId="9" navigable="true"/>
<display labels="true" multiplicity="true"/>
</association>
<association id="21">
<end type="SOURCE" refId="2" navigable="false">
<attribute id="22" name="unitExtension"/>
<multiplicity id="23" minimum="0" maximum="1"/>
</end>
<end type="TARGET" refId="4" navigable="true"/>
<display labels="true" multiplicity="true"/>
</association>
<realization id="24">
<end type="SOURCE" refId="6"/>
<end type="TARGET" refId="7"/>
</realization>
<generalization id="25">
<end type="SOURCE" refId="3"/>
<end type="TARGET" refId="4"/>
</generalization>
<generalization id="26">
<end type="SOURCE" refId="5"/>
<end type="TARGET" refId="2"/>
</generalization>
<generalization id="27">
<end type="SOURCE" refId="7"/>
<end type="TARGET" refId="4"/>
</generalization>
<generalization id="28">
<end type="SOURCE" refId="8"/>
<end type="TARGET" refId="2"/>
</generalization>
<association id="29">
<end type="SOURCE" refId="6" navigable="false">
<attribute id="30" name="unit"/>
<multiplicity id="31" minimum="0" maximum="1"/>
</end>
<end type="TARGET" refId="8" navigable="true"/>
<display labels="true" multiplicity="true"/>
</association>
<generalization id="32">
<end type="SOURCE" refId="11"/>
<end type="TARGET" refId="4"/>
</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>

45
extension-objects/pom.xml Normal file
View File

@ -0,0 +1,45 @@
<?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.17.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>extension-objects</artifactId>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>

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.
*/
import abstractextensions.CommanderExtension;
import abstractextensions.SergeantExtension;
import abstractextensions.SoldierExtension;
import units.CommanderUnit;
import units.SergeantUnit;
import units.SoldierUnit;
import units.Unit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Anticipate that an objects interface needs to be extended in the future.
* Additional interfaces are defined by extension objects.
*/
public class App {
/**
* Program entry point
*
* @param args command line args
*/
public static void main(String[] args) {
//Create 3 different units
Unit soldierUnit = new SoldierUnit("SoldierUnit1");
Unit sergeantUnit = new SergeantUnit("SergeantUnit1");
Unit commanderUnit = new CommanderUnit("CommanderUnit1");
//check for each unit to have an extension
checkExtensionsForUnit(soldierUnit);
checkExtensionsForUnit(sergeantUnit);
checkExtensionsForUnit(commanderUnit);
}
private static void checkExtensionsForUnit(Unit unit) {
final Logger logger = LoggerFactory.getLogger(App.class);
SoldierExtension soldierExtension = (SoldierExtension) unit.getUnitExtension("SoldierExtension");
SergeantExtension sergeantExtension = (SergeantExtension) unit.getUnitExtension("SergeantExtension");
CommanderExtension commanderExtension = (CommanderExtension) unit.getUnitExtension("CommanderExtension");
//if unit have extension call the method
if (soldierExtension != null) {
soldierExtension.soldierReady();
} else {
logger.info(unit.getName() + " without SoldierExtension");
}
if (sergeantExtension != null) {
sergeantExtension.sergeantReady();
} else {
logger.info(unit.getName() + " without SergeantExtension");
}
if (commanderExtension != null) {
commanderExtension.commanderReady();
} else {
logger.info(unit.getName() + " without CommanderExtension");
}
}
}

View File

@ -0,0 +1,31 @@
/**
* 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 abstractextensions;
/**
* Interface with their method
*/
public interface CommanderExtension extends UnitExtension {
void commanderReady();
}

View File

@ -0,0 +1,31 @@
/**
* 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 abstractextensions;
/**
* Interface with their method
*/
public interface SergeantExtension extends UnitExtension {
void sergeantReady();
}

View File

@ -0,0 +1,30 @@
/**
* 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 abstractextensions;
/**
* Interface with their method
*/
public interface SoldierExtension extends UnitExtension {
void soldierReady();
}

View File

@ -0,0 +1,29 @@
/**
* 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 abstractextensions;
/**
* Other Extensions will extend this interface
*/
public interface UnitExtension {
}

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