Compare commits

..

275 Commits

Author SHA1 Message Date
bbe68f6957 Update README.md 2020-09-06 20:06:54 +03:00
ef326ee77e Update README.md 2020-09-06 19:56:07 +03:00
8b5f532a50 Update README.md 2020-09-06 19:55:38 +03:00
a1da1e4973 Cleanup factory 2020-09-06 19:46:13 +03:00
9d21dff855 Merge pull request #1520 from iluwatar/all-contributors/add-ravening
docs: add ravening as a contributor
2020-09-06 19:39:08 +03:00
9d75592e8b Merge branch 'master' into all-contributors/add-ravening 2020-09-06 19:38:44 +03:00
8d6738b729 docs: update .all-contributorsrc [skip ci] 2020-09-06 16:37:04 +00:00
3205dc2cf0 docs: update README.md [skip ci] 2020-09-06 16:37:03 +00:00
16e1863ae7 Merge pull request #1519 from iluwatar/all-contributors/add-samilAyoub
docs: add samilAyoub as a contributor
2020-09-06 19:36:35 +03:00
b9db3c4763 docs: update .all-contributorsrc [skip ci] 2020-09-06 16:35:58 +00:00
d72206ba72 docs: update README.md [skip ci] 2020-09-06 16:35:57 +00:00
922c699e49 Merge pull request #1516 from samilAyoub/add-simple-factory
Add Simple Factory Pattern implementation
2020-09-06 19:33:03 +03:00
bf41b1d9c9 Updates README.md:
- Adding class diagram
- Adding Pros and Cons
- replace "" with ''
2020-09-05 18:39:28 +01:00
b3ef214cd6 Change tabs to spaces in pom.xml 2020-09-04 22:02:19 +01:00
6caf78e4e5 updates :
- Using lambda expression to create cars
- Using spaces instead of tabs in pom.xml
2020-09-04 21:21:51 +01:00
8b26452c75 bug fixing 2020-09-03 22:58:15 +01:00
2bb252e08f Clean the code 2020-09-03 22:41:55 +01:00
a023cfbb1a Merge branch 'master' into add-simple-factory 2020-09-03 22:26:49 +01:00
badf0c6b8c - README.md is added
- Change the name to factory is done
- Local variable type inference is used
2020-09-03 22:08:54 +01:00
c9718a5227 add README file 2020-09-03 21:08:28 +01:00
b423fd30d4 Fix bugs, clean the code and add unit tests. 2020-09-02 18:12:42 +01:00
3df8472bf8 Merge pull request #1515 from fedorskvorcov/fix-typo
Remove unnecessary word from text
2020-09-02 19:59:07 +03:00
ac98b31b68 Add Maven Assembly plugin to pom.xml 2020-09-02 14:09:44 +01:00
46b23f322f Add Simple Factory Pattern implementation
Java source code demonstrate simple factory design pattern
2020-09-02 13:46:53 +01:00
e231cd8d1a Remove unnecessary word from text 2020-09-02 12:32:02 +03:00
19378f3fdd Update README.md 2020-09-01 20:25:39 +03:00
3f4d637510 Update README.md 2020-09-01 20:18:10 +03:00
25cca3547d Update README.md 2020-09-01 20:06:47 +03:00
b9b6777d15 Update README.md 2020-09-01 19:55:22 +03:00
2a5b8c977a Merge pull request #1514 from iluwatar/all-contributors/add-fedorskvorcov
docs: add fedorskvorcov as a contributor
2020-09-01 16:40:23 +03:00
daa94c7b6d docs: update .all-contributorsrc [skip ci] 2020-09-01 13:39:33 +00:00
7aca64a3c9 docs: update README.md [skip ci] 2020-09-01 13:39:32 +00:00
6628bccecc Merge pull request #1513 from fedorskvorcov/fix-typo
Fix typo in comment
2020-09-01 16:38:44 +03:00
aebdb88a83 Fix typo in comment 2020-09-01 09:51:46 +03:00
9c0c17b87a Merge pull request #1512 from xiaod-dev/translation-zh
Translation zh
2020-08-31 20:17:39 +03:00
20fac32ac2 finish Chinese translation of observer and strategy 2020-08-31 17:18:59 +08:00
82842d614b Update README.md 2020-08-30 20:08:34 +03:00
9b71479d04 Update README.md 2020-08-30 19:34:10 +03:00
3c4ae6c4ca Update README.md 2020-08-30 19:17:45 +03:00
9ff42389c6 Merge pull request #1511 from ohbus/master
Configure build to disable shallow clone
2020-08-30 11:52:13 +03:00
b80b9354c6 Update maven-ci.yml 2020-08-30 02:31:06 +05:30
67d1d16e1f Update README.md 2020-08-29 22:13:55 +03:00
bc35911475 Update README.md 2020-08-29 22:02:10 +03:00
b5c6a89ec9 Update README.md 2020-08-29 21:55:51 +03:00
74360a7ecb Update README.md 2020-08-29 21:51:32 +03:00
3544a8366f Update README.md 2020-08-29 21:38:58 +03:00
bcca9beb4d Update README.md 2020-08-29 21:29:15 +03:00
6606d6cd08 Update README.md 2020-08-29 21:21:01 +03:00
f3fd49870c Update README.md 2020-08-29 21:05:30 +03:00
1fbef60f37 Update README.md 2020-08-29 21:00:17 +03:00
b77a05f0fb Update README.md 2020-08-29 20:46:40 +03:00
0ee03db4d0 Update README.md 2020-08-29 20:38:04 +03:00
c541176b38 Update README.md 2020-08-29 20:26:37 +03:00
b53856b64f Merge pull request #1509 from ohbus/master
Cleaning up unnecessary code from the CI yaml and CI server version upgrade
2020-08-29 20:19:42 +03:00
1973d1bc63 Update maven-pr-builder.yml
upgraded build server runtime from Ubuntu 18.04 to 20.04 LTS for the PR builder as well
2020-08-29 19:35:20 +05:30
f5886325ec Update maven-ci.yml
upgraded build server runtime from Ubuntu 18.04 to 20.04 LTS
2020-08-29 19:34:43 +05:30
8afe4c314a Update README.md 2020-08-29 16:56:52 +03:00
2dd2cfb8ca Update README.md 2020-08-29 16:49:45 +03:00
8512c65aef Update README.md 2020-08-29 16:42:46 +03:00
6373f7b115 Update README.md 2020-08-29 16:38:20 +03:00
e8b42bd135 Update README.md 2020-08-29 16:23:28 +03:00
2bb2134636 Update README.md 2020-08-29 16:17:15 +03:00
675b2f14b2 Update README.md 2020-08-29 16:10:59 +03:00
338c146c78 Update maven-pr-builder.yml 2020-08-29 17:39:50 +05:30
8135dbecdb Update maven-ci.yml 2020-08-29 17:36:31 +05:30
a4f2d14848 Update README.md 2020-08-29 12:01:23 +03:00
96c16a8f3a Update README.md 2020-08-29 11:29:30 +03:00
47e746c3ba Update maven-pr-builder.yml
removed the if checking block for building Pull Requests as this was redundant code.
2020-08-28 12:26:27 +05:30
7118ccafa9 Update maven-ci.yml
Removed if clause for building code in main codebase from the CI pipeline.
2020-08-28 12:17:38 +05:30
8983f9c11c Update README.md 2020-08-26 21:55:05 +03:00
9b464e0be1 Merge pull request #1507 from amit1307/upgrade-mockito-1486
Upgrade Mockito version
2020-08-26 21:20:11 +03:00
b07d33f332 Upgrade Mockito version to latest 2020-08-26 00:09:30 +01:00
6c4c6097be Merge branch 'master' of https://github.com/iluwatar/java-design-patterns 2020-08-25 23:21:25 +01:00
9dd46d7b4a Update README.md 2020-08-25 21:42:42 +03:00
723afb85ba Set version for next development iteration 2020-08-25 21:21:36 +03:00
a0e5d061cb Milestone 1.23.0 2020-08-25 21:20:30 +03:00
687648af0a Merge pull request #1506 from iluwatar/all-contributors/add-stefanbirkner
docs: add stefanbirkner as a contributor
2020-08-25 17:43:29 +03:00
96aa21d0e8 docs: update .all-contributorsrc [skip ci] 2020-08-25 14:42:54 +00:00
b8f83c326d docs: update README.md [skip ci] 2020-08-25 14:42:53 +00:00
15d795bf8a Merge pull request #1505 from stefanbirkner/system-lambda
Replace System Rules with System Lambda
2020-08-25 17:41:09 +03:00
3754c66604 Replace System Rules with System Lambda
System Lambda is more specific. It only wraps the part of the code that
produces the output.
2020-08-24 23:20:03 +02:00
6d83ceba28 Update README.md 2020-08-23 18:53:57 +03:00
015b418114 Update README.md 2020-08-23 18:03:29 +03:00
0cba307844 Fix readme filename 2020-08-23 17:06:37 +03:00
a66edc84a0 Merge pull request #1504 from iluwatar/all-contributors/add-mkrzywanski
docs: add mkrzywanski as a contributor
2020-08-23 17:00:32 +03:00
c18282ad5d docs: update .all-contributorsrc [skip ci] 2020-08-23 13:59:15 +00:00
7411ea86bf docs: update README.md [skip ci] 2020-08-23 13:59:14 +00:00
ef033b66a6 Merge pull request #1499 from mkrzywanski/pattern/filterer
Filterer pattern
2020-08-23 16:56:06 +03:00
e65b65257e fixing typos in readme file, introducing var local type inference where possible 2020-08-23 11:00:15 +02:00
3d9afbaeec fixed typo in read me 2020-08-22 18:24:41 +02:00
61a819aab8 Added fixes after review. Changed example pattern application to threat detection domain 2020-08-22 18:11:14 +02:00
61b95c294b Update github token 2020-08-22 14:49:56 +03:00
4068d1fead Update sonar badges 2020-08-22 14:03:21 +03:00
b284230ecf Merge pull request #1502 from iluwatar/all-contributors/add-edycutjong
docs: add edycutjong as a contributor
2020-08-21 18:01:31 +03:00
0529b77abb docs: update .all-contributorsrc [skip ci] 2020-08-21 15:00:49 +00:00
a2ae5d1324 docs: update README.md [skip ci] 2020-08-21 15:00:48 +00:00
25159ed9a8 Merge pull request #1501 from iluwatar/all-contributors/add-ToxicDreamz
docs: add ToxicDreamz as a contributor
2020-08-21 17:35:37 +03:00
7f09cd5b2d docs: update .all-contributorsrc [skip ci] 2020-08-21 14:34:24 +00:00
b388020fc0 docs: update README.md [skip ci] 2020-08-21 14:34:23 +00:00
0c552effc3 Merge pull request #1492 from ToxicDreamz/SonarCloud-Reports-Issue#1012
Fixed most reported issues by SonarCloud.
2020-08-21 17:25:16 +03:00
905b5dc6d8 Implemented filterer pattern 2020-08-21 11:51:43 +02:00
885c8a6765 Fixed a test-case issue within the dirty-flag module. 2020-08-20 01:21:03 +04:00
6b5b2ac1e8 Merge remote-tracking branch 'origin/SonarCloud-Reports-Issue#1012' into SonarCloud-Reports-Issue#1012 2020-08-20 01:03:50 +04:00
c35b98b4d7 Fixed pom.xml issues within the dirty-flag and partial-response modules that were causing build failures. 2020-08-20 01:01:03 +04:00
f5fddeb7f0 Merge branch 'master' into SonarCloud-Reports-Issue#1012 2020-08-19 23:29:36 +04:00
292ec5b8e5 Merge branch 'SonarCloud-Reports-Issue#1012'
# Conflicts:
#	pom.xml
2020-08-19 23:27:43 +04:00
60d87789a6 Merge pull request #1 from iluwatar/master
Fork Update
2020-08-19 23:26:19 +04:00
860453b46b Fixed JUnit tests causing build issues due to mixing JUnit 4 & JUnit 5 2020-08-19 23:14:37 +04:00
6921b0dce0 Fixed checkstyle errors causing build failures. 2020-08-19 13:27:08 +04:00
1e385056fc finish translate adapter pattern into chinese 2020-08-19 17:10:14 +08:00
847585334c Update README.md 2020-08-18 20:09:04 +03:00
9bb0b6fe6f Merge pull request #1498 from iluwatar/all-contributors/add-ohbus
docs: add ohbus as a contributor
2020-08-18 19:46:13 +03:00
31881f500d docs: update .all-contributorsrc [skip ci] 2020-08-18 16:45:38 +00:00
06f20570b6 docs: update README.md [skip ci] 2020-08-18 16:45:37 +00:00
017ee23793 Merge pull request #1497 from iluwatar/all-contributors/add-nahteb
docs: add nahteb as a contributor
2020-08-18 19:42:38 +03:00
6c7715ace6 docs: update .all-contributorsrc [skip ci] 2020-08-18 16:42:03 +00:00
ef43af3b23 docs: update README.md [skip ci] 2020-08-18 16:42:02 +00:00
95e513b6ec Edit readme 2020-08-17 19:49:00 +03:00
194040543e Merge pull request #1493 from ohbus/master
updated workflows
2020-08-17 19:18:39 +03:00
846a174ade Merge pull request #1496 from iluwatar/all-contributors/add-ohbus
docs: add ohbus as a contributor
2020-08-17 19:14:47 +03:00
7f60f7be25 docs: update .all-contributorsrc [skip ci] 2020-08-17 16:14:04 +00:00
cfb58191ea docs: update README.md [skip ci] 2020-08-17 16:14:03 +00:00
82bf9fdd61 Merge pull request #1494 from nahteb/fix-typo
Fix typo in README for Observer pattern
2020-08-17 19:13:25 +03:00
be552d0ece Merge pull request #1495 from iluwatar/all-contributors/add-xdvrx1
docs: add xdvrx1 as a contributor
2020-08-17 19:11:05 +03:00
d584404df6 docs: update .all-contributorsrc [skip ci] 2020-08-17 16:10:21 +00:00
353a2d9fcf docs: update README.md [skip ci] 2020-08-17 16:10:20 +00:00
8c5740563d reverted the copyright year back to 2019 2020-08-17 19:12:54 +05:30
89bda692f8 Fix typo in README
The log message in the Orcs class should say orcs instead of hobbits.

This is correct in the code example but wrong in the README.
2020-08-17 07:47:30 +01:00
241f93f3cc updated cache to v2 and removed SQ analysis 2020-08-17 11:28:33 +05:30
57e45a329f Fixed a whitespace and spelling issue that was causing the test case to fail. 2020-08-16 22:35:15 +04:00
79a6af703e updated copyright year to 2020 2020-08-16 12:49:21 +05:30
00d06871f4 updated workflows 2020-08-16 12:42:08 +05:30
133ef52898 Fixed an issue with the order of imports that was causing build failures. 2020-08-15 22:19:27 +04:00
7afb065a10 Upgrade urm plugin to latest version 2020-08-15 20:59:11 +03:00
af9f8fe4e1 Merge pull request #1491 from iluwatar/all-contributors/add-xdvrx1
docs: add xdvrx1 as a contributor
2020-08-15 20:54:25 +03:00
a1c96ede13 docs: update .all-contributorsrc [skip ci] 2020-08-15 17:53:38 +00:00
0d2c4abe5a docs: update README.md [skip ci] 2020-08-15 17:53:37 +00:00
31471acb69 Fixed most reported issues by SonarCloud. 2020-08-15 21:47:39 +04:00
e7e3ace01f Merge pull request #1489 from iluwatar/all-contributors/add-RayYH
docs: add RayYH as a contributor
2020-08-11 17:30:33 +03:00
41dacc2e2e docs: update .all-contributorsrc [skip ci] 2020-08-11 14:29:45 +00:00
dc7bf6190d docs: update README.md [skip ci] 2020-08-11 14:29:44 +00:00
98d2dc0104 Merge pull request #1487 from RayYH/master
fix word typo
2020-08-11 17:29:06 +03:00
870d44b127 Fix layout 2020-08-11 17:26:05 +03:00
834f20911b Merge pull request #1488 from iluwatar/all-contributors/add-ashishtrivedi16
docs: add ashishtrivedi16 as a contributor
2020-08-11 17:23:22 +03:00
2b7949ce6a docs: update .all-contributorsrc [skip ci] 2020-08-11 14:21:59 +00:00
4ffab7da85 docs: update README.md [skip ci] 2020-08-11 14:21:58 +00:00
1683fbdf9c Clean up Transaction Script 2020-08-11 17:17:25 +03:00
d091e369ec Merge pull request #1480 from ashishtrivedi16/master
#1321 Transaction script model implementation
2020-08-11 16:59:48 +03:00
b91bbc773d fix word typo 2020-08-11 14:19:39 +08:00
3c07a42e9d #1321 Updated UML diagram 2020-08-11 00:23:00 +05:30
e99bcf772b #1321 Added comments about TS 2020-08-11 00:21:46 +05:30
2d16e5afbf Merge branch 'master' of https://github.com/iluwatar/java-design-patterns 2020-08-10 16:27:28 +03:00
2c6f1832b0 Merge pull request #1485 from amit1307/logging-api-gateway-1338
Add logging in API Gateway
2020-08-10 16:26:18 +03:00
04a2be0c99 Update readme 2020-08-10 16:25:06 +03:00
10815b6469 Fix tags 2020-08-10 16:05:52 +03:00
75b10ed657 #1321 2020-08-10 13:21:24 +05:30
108532d8dd #1321 2020-08-10 13:12:12 +05:30
5527cf4234 #1321 Renamed main class 2020-08-10 13:06:46 +05:30
4008ae41b5 #1321 2020-08-10 13:02:35 +05:30
8166a1835d Merge remote-tracking branch 'origin/master' 2020-08-10 13:01:30 +05:30
4c9cad5475 #1321 Updated README 2020-08-10 13:01:12 +05:30
24126edd86 Update Hotel.java 2020-08-10 12:54:46 +05:30
c0edac0046 Rename TransactionScriptApp.java to App.java 2020-08-10 12:53:13 +05:30
f5c337981b Rename Readme.md to README.md 2020-08-10 12:52:37 +05:30
5441db6582 Update pom.xml 2020-08-10 12:52:15 +05:30
87f3a4d956 Delete module-info.java 2020-08-10 12:48:55 +05:30
31d753e59d Update transaction-script/Readme.md
Co-authored-by: Ilkka Seppälä <iluwatar@users.noreply.github.com>
2020-08-10 00:25:43 +05:30
6cef98d41e Update transaction-script/Readme.md
Co-authored-by: Ilkka Seppälä <iluwatar@users.noreply.github.com>
2020-08-10 00:25:01 +05:30
7450456dae Add syntax highlighting 2020-08-09 21:54:54 +03:00
45e416928d Update transaction-script/Readme.md
Co-authored-by: Ilkka Seppälä <iluwatar@users.noreply.github.com>
2020-08-10 00:24:44 +05:30
94c131f7e9 Update transaction-script/Readme.md
Co-authored-by: Ilkka Seppälä <iluwatar@users.noreply.github.com>
2020-08-10 00:24:33 +05:30
5eb9b98e78 Update transaction-script/Readme.md
Co-authored-by: Ilkka Seppälä <iluwatar@users.noreply.github.com>
2020-08-10 00:24:20 +05:30
8305e9365d Fix typos 2020-08-09 21:49:00 +03:00
04e3368a81 Merge pull request #1482 from vINCENT8888801/private-class-data-explanation
Private class data explanation
2020-08-09 21:27:34 +03:00
d219a104c7 Add logging in API Gateway 2020-08-09 18:13:24 +01:00
700f5c6d27 corrected some typo 2020-08-09 18:22:40 +08:00
f4fa73cd48 Update README.md
Increase clarity
2020-08-09 16:28:40 +08:00
e09de2fb36 #1321 2020-08-09 01:58:19 +05:30
a59c9bba97 #1321 2020-08-09 01:39:51 +05:30
7acc5fbf95 #1321
Resolved conflicts and used var wherever possible
2020-08-09 01:33:19 +05:30
bdf2145b3f Merge branch 'master' into master 2020-08-09 01:17:07 +05:30
1e90d0d645 Merge pull request #1484 from iluwatar/all-contributors/add-vINCENT8888801
docs: add vINCENT8888801 as a contributor
2020-08-08 22:44:51 +03:00
b8c0985b97 docs: update .all-contributorsrc [skip ci] 2020-08-08 19:44:04 +00:00
c40d6ae9d7 docs: update README.md [skip ci] 2020-08-08 19:44:03 +00:00
76bfc68cd6 Merge pull request #1481 from vINCENT8888801/some-minor-spelling-error
Some minor spelling error in comment
2020-08-08 22:43:05 +03:00
d0ed10a619 Merge pull request #1479 from anuragagarwal561994/java-11
Java 11 remaining pattern
2020-08-08 22:32:43 +03:00
5bfaeffecf Update transaction-script/Readme.md
Co-authored-by: Ilkka Seppälä <iluwatar@users.noreply.github.com>
2020-08-09 00:51:44 +05:30
c0acaf073b Update transaction-script/Readme.md
Co-authored-by: Ilkka Seppälä <iluwatar@users.noreply.github.com>
2020-08-09 00:51:36 +05:30
9f190c59c4 Update transaction-script/Readme.md
Co-authored-by: Ilkka Seppälä <iluwatar@users.noreply.github.com>
2020-08-09 00:51:28 +05:30
50ed5ca699 Some minor spelling error in comment 2020-08-09 03:15:26 +08:00
08cec0e254 Update README.md 2020-08-09 03:10:43 +08:00
1f6e6f34ea Update README.md 2020-08-09 03:09:53 +08:00
134ccdb5a1 Corrects condition 2020-08-08 11:56:34 +00:00
fa68789cd5 #1321 Updated readme and add UML diagram 2020-08-08 15:49:14 +05:30
8b92bc6bb6 Corrects README.md 2020-08-08 00:53:30 +00:00
8e060ad0ad Refactors null object pattern to java-11 2020-08-08 00:46:08 +00:00
a5038c4329 Uses java-11 in naked objects 2020-08-08 00:41:58 +00:00
b0ac4c1ca3 #590 explanation for Arrange/Act/Assert 2020-08-07 19:10:50 +03:00
0ead283f55 Merge pull request #1475 from ravening/simplify_observer
Use enums instead os switch blocks
2020-08-05 17:59:52 +03:00
888af23219 Merge pull request #1220 from anuragagarwal561994/java-11
Java 11 (patterns with m)
2020-08-05 17:38:54 +03:00
0c83ccc2fe Use enums instead os switch blocks
Its better to use enums instead of switch
blocks which makes the code longer and difficult
to maintain as and when new state appears.
2020-08-05 15:50:05 +02:00
a7095602d6 Refactors using var 2020-08-04 21:46:30 +00:00
4b38746ce9 Removes usage of Dictionary 2020-08-04 21:41:25 +00:00
cd20e7a3f4 Minor readme fixes 2020-08-04 21:45:16 +03:00
7f29c2455f #590 explanation for Ambassador 2020-08-04 21:36:08 +03:00
51e8900d31 Merge pull request #1474 from iluwatar/all-contributors/add-ravening
docs: add ravening as a contributor
2020-08-04 17:48:52 +03:00
20a5dde8a4 docs: update .all-contributorsrc [skip ci] 2020-08-04 14:48:06 +00:00
bf4706addf docs: update README.md [skip ci] 2020-08-04 14:48:05 +00:00
97285d3724 Merge pull request #1472 from ravening/command
#1324 Use interfaces in command pattern
2020-08-04 17:47:32 +03:00
f234baf258 Cleanup code 2020-08-04 16:34:41 +02:00
ca58fa3f21 #590 add related patterns to Abstract Factory 2020-08-04 17:31:33 +03:00
ca7192889d Fixes test cases for master-worker-pattern 2020-08-03 18:50:45 +00:00
fa3b93bf8d #590 fix diagram 2020-08-03 21:25:13 +03:00
a7b4194a71 #590 explanation for Aggregator Microservices 2020-08-03 21:09:31 +03:00
054b1eaac6 Resolves test failures 2020-08-03 15:59:28 +00:00
44a654a2e3 Resolves CR comments 2020-08-03 15:45:29 +00:00
10988526a2 Merge branch 'master' of github.com:iluwatar/java-design-patterns into java-11 2020-08-03 15:42:18 +00:00
b9f17824fa removing unwanted modifiers 2020-08-03 17:38:03 +02:00
3ae7466647 Typically command pattern is implemented using
interfaces and concrete classes. Refactor the
code to use the same
2020-08-03 16:51:30 +02:00
9ff5b9e7c0 Fix merge 2020-08-03 16:49:46 +03:00
4941e8ebc4 Merge pull request #1471 from iluwatar/all-contributors/add-charlesfinley
docs: add charlesfinley as a contributor
2020-08-03 16:35:28 +03:00
081d789749 Merge branch 'master' into all-contributors/add-charlesfinley 2020-08-03 16:34:41 +03:00
f84f7b973c docs: update .all-contributorsrc [skip ci] 2020-08-03 13:30:52 +00:00
83acaa82d4 docs: update README.md [skip ci] 2020-08-03 13:30:51 +00:00
b0ded54c66 Cleanup 2020-08-02 22:48:54 +03:00
689cc8b59b Update surefire and minor improvements 2020-08-02 11:55:13 +03:00
8c7dd72d5a Merge pull request #1470 from charlesfinley/master
Updated ${artifactId} to ${project.artifactId}
2020-08-02 11:51:14 +03:00
14487261d0 Use of ${artifactId} is deprecated and should be updated to ${project.artifactId} 2020-08-02 00:03:36 -04:00
a9b7111b39 work on Abstract Factory readme 2020-08-01 16:26:14 +03:00
b3bfd43bff #590 update Acyclic Visitor class diagram 2020-08-01 15:54:46 +03:00
6a8297598e #1047 remove module infos 2020-08-01 15:19:09 +03:00
7ac8eba434 #590 explanation for Acyclic Visitor 2020-08-01 15:18:32 +03:00
595086aa13 Merge pull request #1467 from iluwatar/all-contributors/add-raja-peeyush-kumar-singh
docs: add raja-peeyush-kumar-singh as a contributor
2020-08-01 12:49:06 +03:00
38791a6a66 docs: update .all-contributorsrc [skip ci] 2020-08-01 09:48:24 +00:00
27c40826de docs: update README.md [skip ci] 2020-08-01 09:48:23 +00:00
d1d67bcb6c Merge pull request #1466 from iluwatar/all-contributors/add-nishant
docs: add nishant as a contributor
2020-08-01 11:46:28 +03:00
d41077f355 docs: update .all-contributorsrc [skip ci] 2020-08-01 08:45:39 +00:00
e4473e5c88 docs: update README.md [skip ci] 2020-08-01 08:45:38 +00:00
7af544998d Merge pull request #1465 from iluwatar/all-contributors/add-MananS77
docs: add MananS77 as a contributor
2020-08-01 11:08:05 +03:00
0a2c87d49a docs: update .all-contributorsrc [skip ci] 2020-08-01 08:07:21 +00:00
15eb49e574 docs: update README.md [skip ci] 2020-08-01 08:07:20 +00:00
661f9e1373 Merge pull request #1464 from iluwatar/all-contributors/add-charlesfinley
docs: add charlesfinley as a contributor
2020-08-01 10:42:44 +03:00
7908d38604 docs: update .all-contributorsrc [skip ci] 2020-08-01 07:41:48 +00:00
41020982de docs: update README.md [skip ci] 2020-08-01 07:41:47 +00:00
43569ecd66 Merge pull request #1463 from charlesfinley/master
Fixes Issue #1462 - Unit Test assertEquals parameters are reversed
2020-08-01 10:39:20 +03:00
fb6507ceda Corrected assertEquals order for expected, actual. 2020-07-31 22:52:23 -04:00
29f799c815 Corrected assertEquals order for expected, actual. 2020-07-31 22:50:48 -04:00
55bb1f11e0 #590 fix typo 2020-07-30 21:57:07 +03:00
8364b289b4 #590 explanation for Abstract Document 2020-07-30 21:39:11 +03:00
417f21ed3d Code cleanup (#1461)
* Code cleanup

* Fix flux tests

* Fix checkstyle errors

* Fix compile error
2020-07-30 20:28:47 +03:00
9b25d302b7 Fix broken logging in service layer 2020-07-26 21:57:15 +01:00
12d392931e #1321 Added UML diagram 2020-07-27 00:39:53 +05:30
4017c37b6f #1321 Fixed a test 2020-07-26 18:34:42 +05:30
9d191324ff #1321 2020-07-26 18:17:00 +05:30
75ab3b5245 #1321 Added Tests 2020-07-26 17:38:33 +05:30
e0fb2d014a #1321 Added comments and refactored code 2020-07-26 15:56:55 +05:30
c4b1b89f1f #1321 2020-07-19 22:20:15 +05:30
52e6ea2b68 #1321
Implemented DAO
2020-07-19 20:33:52 +05:30
112973482d #1321
Added basic structure and planned out the roadmap
2020-07-19 16:51:16 +05:30
58e3fbfc6a #1321
Added gitignore
2020-07-19 16:19:01 +05:30
bf01f3e68b #1321
Added Readme file
2020-07-19 15:35:53 +05:30
58b98c54e5 #1321
Initial folder setup
2020-07-19 15:30:30 +05:30
daf53225d8 Resolves CR comments 2020-05-01 08:04:45 +00:00
d733122e7a Java 11 migraiton: mutex 2020-04-12 23:00:49 +00:00
2fa938c02d Java 11 migraiton: mute-idiom 2020-04-12 22:58:50 +00:00
9b105d770d Java 11 migraiton: multiton 2020-04-12 22:51:37 +00:00
109d33c710 Java 11 migraiton: monostate 2020-04-12 22:49:00 +00:00
a142c06048 Java 11 migraiton: monad 2020-04-12 22:45:54 +00:00
f1b27ef5c7 Java 11 migraiton: module 2020-04-12 22:38:00 +00:00
99c70af16a Java 11 migraiton: model-view-presenter 2020-04-12 22:35:33 +00:00
edcb520d08 Java 11 migraiton: model-view-controller 2020-04-12 22:30:18 +00:00
a00622c656 Java 11 migraiton: memento 2020-04-12 22:21:48 +00:00
93e5570778 Java 11 migraiton: mediator pattern 2020-04-12 22:19:11 +00:00
59e050b20b Java 11 migraiton: master-worker-pattern 2020-04-12 22:05:13 +00:00
e6c74a5fb9 Java 11 migraiton: marker 2020-04-12 21:35:48 +00:00
574 changed files with 8210 additions and 3632 deletions

View File

@ -1038,6 +1038,163 @@
"contributions": [
"translation"
]
},
{
"login": "charlesfinley",
"name": "Matt Dolan",
"avatar_url": "https://avatars1.githubusercontent.com/u/6307904?v=4",
"profile": "https://github.com/charlesfinley",
"contributions": [
"code",
"review"
]
},
{
"login": "MananS77",
"name": "Manan",
"avatar_url": "https://avatars3.githubusercontent.com/u/21033516?v=4",
"profile": "https://github.com/MananS77",
"contributions": [
"review"
]
},
{
"login": "nishant",
"name": "Nishant Arora",
"avatar_url": "https://avatars2.githubusercontent.com/u/15331971?v=4",
"profile": "https://github.com/nishant",
"contributions": [
"code"
]
},
{
"login": "raja-peeyush-kumar-singh",
"name": "Peeyush",
"avatar_url": "https://avatars0.githubusercontent.com/u/5496024?v=4",
"profile": "https://github.com/raja-peeyush-kumar-singh",
"contributions": [
"code"
]
},
{
"login": "ravening",
"name": "Rakesh",
"avatar_url": "https://avatars1.githubusercontent.com/u/10645273?v=4",
"profile": "https://github.com/ravening",
"contributions": [
"code",
"review"
]
},
{
"login": "vINCENT8888801",
"name": "Wei Seng",
"avatar_url": "https://avatars0.githubusercontent.com/u/8037883?v=4",
"profile": "https://github.com/vINCENT8888801",
"contributions": [
"code"
]
},
{
"login": "ashishtrivedi16",
"name": "Ashish Trivedi",
"avatar_url": "https://avatars3.githubusercontent.com/u/23194128?v=4",
"profile": "https://www.linkedin.com/in/ashish-trivedi-218379135/",
"contributions": [
"code"
]
},
{
"login": "RayYH",
"name": "洪月阳",
"avatar_url": "https://avatars1.githubusercontent.com/u/41055099?v=4",
"profile": "https://rayyounghong.com",
"contributions": [
"code"
]
},
{
"login": "xdvrx1",
"name": "xdvrx1",
"avatar_url": "https://avatars0.githubusercontent.com/u/47092464?v=4",
"profile": "https://xdvrx1.github.io/",
"contributions": [
"review",
"ideas"
]
},
{
"login": "ohbus",
"name": "Subhrodip Mohanta",
"avatar_url": "https://avatars0.githubusercontent.com/u/13291222?v=4",
"profile": "http://subho.xyz",
"contributions": [
"code",
"review"
]
},
{
"login": "nahteb",
"name": "Bethan Palmer",
"avatar_url": "https://avatars3.githubusercontent.com/u/13121570?v=4",
"profile": "https://github.com/nahteb",
"contributions": [
"code"
]
},
{
"login": "ToxicDreamz",
"name": "Toxic Dreamz",
"avatar_url": "https://avatars0.githubusercontent.com/u/45225562?v=4",
"profile": "https://github.com/ToxicDreamz",
"contributions": [
"code"
]
},
{
"login": "edycutjong",
"name": "Edy Cu Tjong",
"avatar_url": "https://avatars1.githubusercontent.com/u/1098102?v=4",
"profile": "http://www.edycutjong.com",
"contributions": [
"doc"
]
},
{
"login": "mkrzywanski",
"name": "Michał Krzywański",
"avatar_url": "https://avatars0.githubusercontent.com/u/15279585?v=4",
"profile": "https://github.com/mkrzywanski",
"contributions": [
"code"
]
},
{
"login": "stefanbirkner",
"name": "Stefan Birkner",
"avatar_url": "https://avatars1.githubusercontent.com/u/711349?v=4",
"profile": "https://www.stefan-birkner.de",
"contributions": [
"code"
]
},
{
"login": "fedorskvorcov",
"name": "Fedor Skvorcov",
"avatar_url": "https://avatars3.githubusercontent.com/u/43882212?v=4",
"profile": "https://github.com/fedorskvorcov",
"contributions": [
"code"
]
},
{
"login": "samilAyoub",
"name": "samilAyoub",
"avatar_url": "https://avatars0.githubusercontent.com/u/61546990?v=4",
"profile": "https://github.com/samilAyoub",
"contributions": [
"code"
]
}
],
"contributorsPerLine": 4,

View File

@ -24,37 +24,41 @@
# This workflow will build a Java project with Maven
# For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-maven
name: Java CI with Maven
name: Java CI
on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
jobs:
build:
runs-on: ubuntu-18.04
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v2
with:
# Disabling shallow clone for improving relevancy of SonarQube reporting
fetch-depth: 0
- name: Set up JDK 11
uses: actions/setup-java@v1
with:
java-version: 11
- uses: actions/cache@v2
with:
path: ~/.m2/repository
key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }}
restore-keys: |
${{ runner.os }}-maven-
# Some tests need screen access
- name: Install xvfb
run: sudo apt-get install xvfb
# SonarQube scan does not work for forked repositories
run: sudo apt-get install -y xvfb
# The SonarQube analysis is only for the master branch of the main repository.
# SonarQube scan does not work for forked repositories try changing it to xvfb-run mvn clean verify
# See https://jira.sonarsource.com/browse/MMF-1371
- name: Build with Maven
if: github.ref != 'refs/heads/master'
run: xvfb-run mvn clean verify
- name: Build with Maven and run SonarQube analysis
if: github.ref == 'refs/heads/master'
run: xvfb-run mvn clean verify org.sonarsource.scanner.maven:sonar-maven-plugin:sonar
env:
# These two env variables are needed for sonar analysis
GITHUB_TOKEN: ${{ secrets.REPOSITORY_ACCESS_TOKEN }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}

57
.github/workflows/maven-pr-builder.yml vendored Normal file
View File

@ -0,0 +1,57 @@
#
# The MIT License
# Copyright © 2014-2019 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.
#
# This workflow will build a Java project with Maven
# For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-maven
name: Java PR Builder
on:
pull_request:
branches: [ master ]
jobs:
build:
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v2
- name: Set up JDK 11
uses: actions/setup-java@v1
with:
java-version: 11
- uses: actions/cache@v2
with:
path: ~/.m2/repository
key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }}
restore-keys: |
${{ runner.os }}-maven-
# Some tests need screen access
- name: Install xvfb
run: sudo apt-get install -y xvfb
# This worflow is only for building Pull Requests, the master branch runs Sonar analysis on the main repository.
# SonarQube scan does not work for forked repositories.
# See https://jira.sonarsource.com/browse/MMF-1371
- name: Build with Maven
run: xvfb-run mvn clean verify

View File

@ -6,10 +6,11 @@
![Java CI with Maven](https://github.com/iluwatar/java-design-patterns/workflows/Java%20CI%20with%20Maven/badge.svg)
[![License MIT](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/iluwatar/java-design-patterns/master/LICENSE.md)
[![Lines of Code](https://sonarcloud.io/api/project_badges/measure?project=iluwatar_java-design-patterns&metric=ncloc)](https://sonarcloud.io/dashboard?id=iluwatar_java-design-patterns)
[![Coverage](https://sonarcloud.io/api/project_badges/measure?project=iluwatar_java-design-patterns&metric=coverage)](https://sonarcloud.io/dashboard?id=iluwatar_java-design-patterns)
[![Join the chat at https://gitter.im/iluwatar/java-design-patterns](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/iluwatar/java-design-patterns?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
[![Sonarcloud Status](https://sonarcloud.io/api/project_badges/measure?project=iluwatar_java-design-patterns&metric=alert_status)](https://sonarcloud.io/dashboard?id=iluwatar_java-design-patterns)
<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
[![All Contributors](https://img.shields.io/badge/all_contributors-114-orange.svg?style=flat-square)](#contributors-)
[![All Contributors](https://img.shields.io/badge/all_contributors-131-orange.svg?style=flat-square)](#contributors-)
<!-- ALL-CONTRIBUTORS-BADGE:END -->
# Introduction
@ -239,6 +240,32 @@ This project is licensed under the terms of the MIT license.
<tr>
<td align="center"><a href="https://webpro.nl"><img src="https://avatars1.githubusercontent.com/u/456426?v=4" width="100px;" alt=""/><br /><sub><b>Lars Kappert</b></sub></a><br /><a href="#content-webpro" title="Content">🖋</a></td>
<td align="center"><a href="https://xiaod.info"><img src="https://avatars2.githubusercontent.com/u/21277644?v=4" width="100px;" alt=""/><br /><sub><b>Mike Liu</b></sub></a><br /><a href="#translation-xiaod-dev" title="Translation">🌍</a></td>
<td align="center"><a href="https://github.com/charlesfinley"><img src="https://avatars1.githubusercontent.com/u/6307904?v=4" width="100px;" alt=""/><br /><sub><b>Matt Dolan</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/commits?author=charlesfinley" title="Code">💻</a> <a href="https://github.com/iluwatar/java-design-patterns/pulls?q=is%3Apr+reviewed-by%3Acharlesfinley" title="Reviewed Pull Requests">👀</a></td>
<td align="center"><a href="https://github.com/MananS77"><img src="https://avatars3.githubusercontent.com/u/21033516?v=4" width="100px;" alt=""/><br /><sub><b>Manan</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/pulls?q=is%3Apr+reviewed-by%3AMananS77" title="Reviewed Pull Requests">👀</a></td>
</tr>
<tr>
<td align="center"><a href="https://github.com/nishant"><img src="https://avatars2.githubusercontent.com/u/15331971?v=4" width="100px;" alt=""/><br /><sub><b>Nishant Arora</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/commits?author=nishant" title="Code">💻</a></td>
<td align="center"><a href="https://github.com/raja-peeyush-kumar-singh"><img src="https://avatars0.githubusercontent.com/u/5496024?v=4" width="100px;" alt=""/><br /><sub><b>Peeyush</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/commits?author=raja-peeyush-kumar-singh" title="Code">💻</a></td>
<td align="center"><a href="https://github.com/ravening"><img src="https://avatars1.githubusercontent.com/u/10645273?v=4" width="100px;" alt=""/><br /><sub><b>Rakesh</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/commits?author=ravening" title="Code">💻</a></td>
<td align="center"><a href="https://github.com/vINCENT8888801"><img src="https://avatars0.githubusercontent.com/u/8037883?v=4" width="100px;" alt=""/><br /><sub><b>Wei Seng</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/commits?author=vINCENT8888801" title="Code">💻</a></td>
</tr>
<tr>
<td align="center"><a href="https://www.linkedin.com/in/ashish-trivedi-218379135/"><img src="https://avatars3.githubusercontent.com/u/23194128?v=4" width="100px;" alt=""/><br /><sub><b>Ashish Trivedi</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/commits?author=ashishtrivedi16" title="Code">💻</a></td>
<td align="center"><a href="https://rayyounghong.com"><img src="https://avatars1.githubusercontent.com/u/41055099?v=4" width="100px;" alt=""/><br /><sub><b>洪月阳</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/commits?author=RayYH" title="Code">💻</a></td>
<td align="center"><a href="https://xdvrx1.github.io/"><img src="https://avatars0.githubusercontent.com/u/47092464?v=4" width="100px;" alt=""/><br /><sub><b>xdvrx1</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/pulls?q=is%3Apr+reviewed-by%3Axdvrx1" title="Reviewed Pull Requests">👀</a> <a href="#ideas-xdvrx1" title="Ideas, Planning, & Feedback">🤔</a></td>
<td align="center"><a href="http://subho.xyz"><img src="https://avatars0.githubusercontent.com/u/13291222?v=4" width="100px;" alt=""/><br /><sub><b>Subhrodip Mohanta</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/commits?author=ohbus" title="Code">💻</a> <a href="https://github.com/iluwatar/java-design-patterns/pulls?q=is%3Apr+reviewed-by%3Aohbus" title="Reviewed Pull Requests">👀</a></td>
</tr>
<tr>
<td align="center"><a href="https://github.com/nahteb"><img src="https://avatars3.githubusercontent.com/u/13121570?v=4" width="100px;" alt=""/><br /><sub><b>Bethan Palmer</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/commits?author=nahteb" title="Code">💻</a></td>
<td align="center"><a href="https://github.com/ToxicDreamz"><img src="https://avatars0.githubusercontent.com/u/45225562?v=4" width="100px;" alt=""/><br /><sub><b>Toxic Dreamz</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/commits?author=ToxicDreamz" title="Code">💻</a></td>
<td align="center"><a href="http://www.edycutjong.com"><img src="https://avatars1.githubusercontent.com/u/1098102?v=4" width="100px;" alt=""/><br /><sub><b>Edy Cu Tjong</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/commits?author=edycutjong" title="Documentation">📖</a></td>
<td align="center"><a href="https://github.com/mkrzywanski"><img src="https://avatars0.githubusercontent.com/u/15279585?v=4" width="100px;" alt=""/><br /><sub><b>Michał Krzywański</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/commits?author=mkrzywanski" title="Code">💻</a></td>
</tr>
<tr>
<td align="center"><a href="https://github.com/ravening"><img src="https://avatars1.githubusercontent.com/u/10645273?v=4" width="100px;" alt=""/><br /><sub><b>Rakesh</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/commits?author=ravening" title="Code">💻</a> <a href="https://github.com/iluwatar/java-design-patterns/pulls?q=is%3Apr+reviewed-by%3Aravening" title="Reviewed Pull Requests">👀</a></td>
<td align="center"><a href="https://www.stefan-birkner.de"><img src="https://avatars1.githubusercontent.com/u/711349?v=4" width="100px;" alt=""/><br /><sub><b>Stefan Birkner</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/commits?author=stefanbirkner" title="Code">💻</a></td>
<td align="center"><a href="https://github.com/fedorskvorcov"><img src="https://avatars3.githubusercontent.com/u/43882212?v=4" width="100px;" alt=""/><br /><sub><b>Fedor Skvorcov</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/commits?author=fedorskvorcov" title="Code">💻</a></td>
<td align="center"><a href="https://github.com/samilAyoub"><img src="https://avatars0.githubusercontent.com/u/61546990?v=4" width="100px;" alt=""/><br /><sub><b>samilAyoub</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/commits?author=samilAyoub" title="Code">💻</a></td>
</tr>
</table>

View File

@ -9,21 +9,182 @@ tags:
---
## Intent
Achieve flexibility of untyped languages and keep the type-safety
Use dynamic properties and achieve flexibility of untyped languages while keeping type-safety.
## Explanation
The Abstract Document pattern enables handling additional, non-static properties. This pattern
uses concept of traits to enable type safety and separate properties of different classes into
set of interfaces.
Real world example
> Consider a car that consists of multiple parts. However we don't know if the specific car really has all the parts, or just some of them. Our cars are dynamic and extremely flexible.
In plain words
> Abstract Document pattern allows attaching properties to objects without them knowing about it.
Wikipedia says
> An object-oriented structural design pattern for organizing objects in loosely typed key-value stores and exposing
the data using typed views. The purpose of the pattern is to achieve a high degree of flexibility between components
in a strongly typed language where new properties can be added to the object-tree on the fly, without losing the
support of type-safety. The pattern makes use of traits to separate different properties of a class into different
interfaces.
**Programmatic Example**
Let's first define the base classes `Document` and `AbstractDocument`. They basically make the object hold a property
map and any amount of child objects.
```java
public interface Document {
Void put(String key, Object value);
Object get(String key);
<T> Stream<T> children(String key, Function<Map<String, Object>, T> constructor);
}
public abstract class AbstractDocument implements Document {
private final Map<String, Object> properties;
protected AbstractDocument(Map<String, Object> properties) {
Objects.requireNonNull(properties, "properties map is required");
this.properties = properties;
}
@Override
public Void put(String key, Object value) {
properties.put(key, value);
return null;
}
@Override
public Object get(String key) {
return properties.get(key);
}
@Override
public <T> Stream<T> children(String key, Function<Map<String, Object>, T> constructor) {
return Stream.ofNullable(get(key))
.filter(Objects::nonNull)
.map(el -> (List<Map<String, Object>>) el)
.findAny()
.stream()
.flatMap(Collection::stream)
.map(constructor);
}
...
}
```
Next we define an enum `Property` and a set of interfaces for type, price, model and parts. This allows us to create
static looking interface to our `Car` class.
```java
public enum Property {
PARTS, TYPE, PRICE, MODEL
}
public interface HasType extends Document {
default Optional<String> getType() {
return Optional.ofNullable((String) get(Property.TYPE.toString()));
}
}
public interface HasPrice extends Document {
default Optional<Number> getPrice() {
return Optional.ofNullable((Number) get(Property.PRICE.toString()));
}
}
public interface HasModel extends Document {
default Optional<String> getModel() {
return Optional.ofNullable((String) get(Property.MODEL.toString()));
}
}
public interface HasParts extends Document {
default Stream<Part> getParts() {
return children(Property.PARTS.toString(), Part::new);
}
}
```
Now we are ready to introduce the `Car`.
```java
public class Car extends AbstractDocument implements HasModel, HasPrice, HasParts {
public Car(Map<String, Object> properties) {
super(properties);
}
}
```
And finally here's how we construct and use the `Car` in a full example.
```java
LOGGER.info("Constructing parts and car");
var wheelProperties = Map.of(
Property.TYPE.toString(), "wheel",
Property.MODEL.toString(), "15C",
Property.PRICE.toString(), 100L);
var doorProperties = Map.of(
Property.TYPE.toString(), "door",
Property.MODEL.toString(), "Lambo",
Property.PRICE.toString(), 300L);
var carProperties = Map.of(
Property.MODEL.toString(), "300SL",
Property.PRICE.toString(), 10000L,
Property.PARTS.toString(), List.of(wheelProperties, doorProperties));
var car = new Car(carProperties);
LOGGER.info("Here is our car:");
LOGGER.info("-> model: {}", car.getModel().orElseThrow());
LOGGER.info("-> price: {}", car.getPrice().orElseThrow());
LOGGER.info("-> parts: ");
car.getParts().forEach(p -> LOGGER.info("\t{}/{}/{}",
p.getType().orElse(null),
p.getModel().orElse(null),
p.getPrice().orElse(null))
);
// Constructing parts and car
// Here is our car:
// model: 300SL
// price: 10000
// parts:
// wheel/15C/100
// door/Lambo/300
```
## Class diagram
![alt text](./etc/abstract-document.png "Abstract Document Traits and Domain")
## Applicability
Use the Abstract Document Pattern when
* there is a need to add new properties on the fly
* you want a flexible way to organize domain in tree like structure
* you want more loosely coupled system
* There is a need to add new properties on the fly
* You want a flexible way to organize domain in tree like structure
* You want more loosely coupled system
## Credits
* [Wikipedia: Abstract Document Pattern](https://en.wikipedia.org/wiki/Abstract_Document_Pattern)
* [Martin Fowler: Dealing with properties](http://martinfowler.com/apsupp/properties.pdf)
* [Pattern-Oriented Software Architecture Volume 4: A Pattern Language for Distributed Computing (v. 4)](https://www.amazon.com/gp/product/0470059028/ref=as_li_qf_asin_il_tl?ie=UTF8&tag=javadesignpat-20&creative=9325&linkCode=as2&creativeASIN=0470059028&linkId=e3aacaea7017258acf184f9f3283b492)

View File

@ -21,7 +21,7 @@
<parent>
<artifactId>java-design-patterns</artifactId>
<groupId>com.iluwatar</groupId>
<version>1.23.0-SNAPSHOT</version>
<version>1.24.0-SNAPSHOT</version>
</parent>
<artifactId>abstract-document</artifactId>
<dependencies>

View File

@ -43,9 +43,11 @@ public class App {
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
/**
* Executes the App.
* Program entry point.
*
* @param args command line args
*/
public App() {
public static void main(String[] args) {
LOGGER.info("Constructing parts and car");
var wheelProperties = Map.of(
@ -75,14 +77,4 @@ public class App {
p.getPrice().orElse(null))
);
}
/**
* Program entry point.
*
* @param args command line args
*/
public static void main(String[] args) {
new App();
}
}

View File

@ -32,7 +32,6 @@ import java.util.stream.Stream;
*/
public interface HasParts extends Document {
default Stream<Part> getParts() {
return children(Property.PARTS.toString(), Part::new);
}

View File

@ -32,7 +32,6 @@ import java.util.Optional;
*/
public interface HasPrice extends Document {
default Optional<Number> getPrice() {
return Optional.ofNullable((Number) get(Property.PRICE.toString()));
}

View File

@ -32,7 +32,6 @@ import java.util.Optional;
*/
public interface HasType extends Document {
default Optional<String> getType() {
return Optional.ofNullable((String) get(Property.TYPE.toString()));
}

View File

@ -40,7 +40,7 @@ public class AbstractDocumentTest {
private static final String KEY = "key";
private static final String VALUE = "value";
private class DocumentImplementation extends AbstractDocument {
private static class DocumentImplementation extends AbstractDocument {
DocumentImplementation(Map<String, Object> properties) {
super(properties);

View File

@ -25,14 +25,23 @@ package com.iluwatar.abstractdocument;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
/**
* Simple App test
*/
public class AppTest {
class AppTest {
/**
* Issue: Add at least one assertion to this test case.
*
* Solution: Inserted assertion to check whether the execution of the main method in {@link App}
* throws an exception.
*/
@Test
public void shouldExecuteAppWithoutException() {
App.main(null);
void shouldExecuteAppWithoutException() {
assertDoesNotThrow(() -> App.main(null));
}
}

View File

@ -9,16 +9,19 @@ tags:
---
## Also known as
Kit
## Intent
Provide an interface for creating families of related or dependent
objects without specifying their concrete classes.
## Explanation
Real world example
> To create a kingdom we need objects with common theme. Elven kingdom needs an Elven king, Elven castle and Elven army whereas Orcish kingdom needs an Orcish king, Orcish castle and Orcish army. There is a dependency between the objects in the kingdom.
> To create a kingdom we need objects with a common theme. Elven kingdom needs an Elven king, Elven castle and Elven army whereas Orcish kingdom needs an Orcish king, Orcish castle and Orcish army. There is a dependency between the objects in the kingdom.
In plain words
@ -30,15 +33,18 @@ Wikipedia says
**Programmatic Example**
Translating the kingdom example above. First of all we have some interfaces and implementation for the objects in the kingdom
Translating the kingdom example above. First of all we have some interfaces and implementation for the objects in the
kingdom.
```java
public interface Castle {
String getDescription();
}
public interface King {
String getDescription();
}
public interface Army {
String getDescription();
}
@ -66,7 +72,7 @@ public class ElfArmy implements Army {
}
}
// Orcish implementations similarly...
// Orcish implementations similarly -> ...
```
@ -112,9 +118,17 @@ var castle = factory.createCastle();
var king = factory.createKing();
var army = factory.createArmy();
castle.getDescription(); // Output: This is the Elven castle!
king.getDescription(); // Output: This is the Elven king!
army.getDescription(); // Output: This is the Elven Army!
castle.getDescription();
king.getDescription();
army.getDescription();
```
Program output:
```java
This is the Elven castle!
This is the Elven king!
This is the Elven Army!
```
Now, we can design a factory for our different kingdom factories. In this example, we created FactoryMaker, responsible for returning an instance of either ElfKingdomFactory or OrcKingdomFactory.
@ -156,46 +170,52 @@ public static void main(String[] args) {
```
## Class diagram
![alt text](./etc/abstract-factory.urm.png "Abstract Factory class diagram")
## Applicability
Use the Abstract Factory pattern when
* a system should be independent of how its products are created, composed and represented
* 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.
* when you need consistency among products
* you dont want to change existing code when adding new products or families of products to the program.
* The system should be independent of how its products are created, composed and represented
* The system should be configured with one of multiple families of products
* The 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.
* When you need consistency among products
* You dont want to change existing code when adding new products or families of products to the program.
## Use Cases:
Example use cases
* Selecting to call the appropriate implementation of FileSystemAcmeService or DatabaseAcmeService or NetworkAcmeService at runtime.
* Unit test case writing becomes much easier
* Selecting to call to the appropriate implementation of FileSystemAcmeService or DatabaseAcmeService or NetworkAcmeService at runtime.
* Unit test case writing becomes much easier
* UI tools for different OS
## Consequences:
* Dependency injection in java hides the service class dependencies that can lead to runtime errors that would have been caught at compile time.
* Dependency injection in java hides the service class dependencies that can lead to runtime errors that would have been caught at compile time.
* While the pattern is great when creating predefined objects, adding the new ones might be challenging.
* The code may become more complicated than it should be, since a lot of new interfaces and classes are introduced along with the pattern.
* The code becomes more complicated than it should be, since a lot of new interfaces and classes are introduced along with the pattern.
## Tutorial
* [Abstract Factory Pattern Tutorial](https://www.journaldev.com/1418/abstract-factory-design-pattern-in-java)
## Real world examples
## Known uses
* [javax.xml.parsers.DocumentBuilderFactory](http://docs.oracle.com/javase/8/docs/api/javax/xml/parsers/DocumentBuilderFactory.html)
* [javax.xml.transform.TransformerFactory](http://docs.oracle.com/javase/8/docs/api/javax/xml/transform/TransformerFactory.html#newInstance--)
* [javax.xml.xpath.XPathFactory](http://docs.oracle.com/javase/8/docs/api/javax/xml/xpath/XPathFactory.html#newInstance--)
## Related patterns
[Factory Method](https://java-design-patterns.com/patterns/factory-method/)
[Factory Kit](https://java-design-patterns.com/patterns/factory-kit/)
## Credits
* [Design Patterns: Elements of Reusable Object-Oriented Software](https://www.amazon.com/gp/product/0201633612/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0201633612&linkCode=as2&tag=javadesignpat-20&linkId=675d49790ce11db99d90bde47f1aeb59)

View File

@ -22,7 +22,7 @@
<parent>
<groupId>com.iluwatar</groupId>
<artifactId>java-design-patterns</artifactId>
<version>1.23.0-SNAPSHOT</version>
<version>1.24.0-SNAPSHOT</version>
</parent>
<artifactId>abstract-factory</artifactId>
<dependencies>

View File

@ -25,12 +25,23 @@ package com.iluwatar.abstractfactory;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
/**
* Tests that Abstract Factory example runs without errors.
*/
public class AppTest {
class AppTest {
/**
* Issue: Add at least one assertion to this test case.
*
* Solution: Inserted assertion to check whether the execution of the main method in {@link App}
* throws an exception.
*/
@Test
public void test() {
App.main(new String[]{});
void shouldExecuteApplicationWithoutException() {
assertDoesNotThrow(() -> App.main(new String[]{}));
}
}

View File

@ -9,12 +9,126 @@ tags:
---
## Intent
Allow new functions to be added to existing class hierarchies without affecting those hierarchies, and without creating the troublesome dependency cycles that are inherent to the GOF VISITOR Pattern.
Allow new functions to be added to existing class hierarchies without affecting those hierarchies, and without creating
the troublesome dependency cycles that are inherent to the GoF Visitor Pattern.
## Explanation
Real world example
> We have a hierarchy of modem classes. The modems in this hierarchy need to be visited by an external algorithm based
> on filtering criteria (is it Unix or DOS compatible modem).
In plain words
> Acyclic Visitor allows functions to be added to existing class hierarchies without modifying the hierarchies.
[WikiWikiWeb](https://wiki.c2.com/?AcyclicVisitor) says
> The Acyclic Visitor pattern allows new functions to be added to existing class hierarchies without affecting those
> hierarchies, and without creating the dependency cycles that are inherent to the GangOfFour VisitorPattern.
**Programmatic Example**
Here's the `Modem` hierarchy.
```java
public abstract class Modem {
public abstract void accept(ModemVisitor modemVisitor);
}
public class Zoom extends Modem {
...
@Override
public void accept(ModemVisitor modemVisitor) {
if (modemVisitor instanceof ZoomVisitor) {
((ZoomVisitor) modemVisitor).visit(this);
} else {
LOGGER.info("Only ZoomVisitor is allowed to visit Zoom modem");
}
}
}
public class Hayes extends Modem {
...
@Override
public void accept(ModemVisitor modemVisitor) {
if (modemVisitor instanceof HayesVisitor) {
((HayesVisitor) modemVisitor).visit(this);
} else {
LOGGER.info("Only HayesVisitor is allowed to visit Hayes modem");
}
}
}
```
Next we introduce the `ModemVisitor` hierarchy.
```java
public interface ModemVisitor {
}
public interface HayesVisitor extends ModemVisitor {
void visit(Hayes hayes);
}
public interface ZoomVisitor extends ModemVisitor {
void visit(Zoom zoom);
}
public interface AllModemVisitor extends ZoomVisitor, HayesVisitor {
}
public class ConfigureForDosVisitor implements AllModemVisitor {
...
@Override
public void visit(Hayes hayes) {
LOGGER.info(hayes + " used with Dos configurator.");
}
@Override
public void visit(Zoom zoom) {
LOGGER.info(zoom + " used with Dos configurator.");
}
}
public class ConfigureForUnixVisitor implements ZoomVisitor {
...
@Override
public void visit(Zoom zoom) {
LOGGER.info(zoom + " used with Unix configurator.");
}
}
```
Finally, here are the visitors in action.
```java
var conUnix = new ConfigureForUnixVisitor();
var conDos = new ConfigureForDosVisitor();
var zoom = new Zoom();
var hayes = new Hayes();
hayes.accept(conDos);
zoom.accept(conDos);
hayes.accept(conUnix);
zoom.accept(conUnix);
```
Program output:
```
// Hayes modem used with Dos configurator.
// Zoom modem used with Dos configurator.
// Only HayesVisitor is allowed to visit Hayes modem
// Zoom modem used with Unix configurator.
```
## Class diagram
![alt text](./etc/acyclic-visitor.png "Acyclic Visitor")
## Applicability
This pattern can be used:
* When you need to add a new function to an existing hierarchy without the need to alter or affect that hierarchy.
@ -24,6 +138,7 @@ This pattern can be used:
* When the recompilation, relinking, retesting or redistribution of the derivatives of Element is very expensive.
## Consequences
The good:
* No dependency cycles between class hierarchies.
@ -32,11 +147,14 @@ The good:
The bad:
* Violates the principle of least surprise or Liskov's Substitution principle by showing that it can accept all visitors but actually only being interested in particular visitors.
* Violates [Liskov's Substitution Principle](https://java-design-patterns.com/principles/#liskov-substitution-principle) by showing that it can accept all visitors but actually only being interested in particular visitors.
* Parallel hierarchy of visitors has to be created for all members in visitable class hierarchy.
## Related patterns
* [Visitor Pattern](../visitor/)
* [Visitor Pattern](https://java-design-patterns.com/patterns/visitor/)
## Credits
* [Acyclic Visitor](http://condor.depaul.edu/dmumaugh/OOT/Design-Principles/acv.pdf)
* [Acyclic Visitor by Robert C. Martin](http://condor.depaul.edu/dmumaugh/OOT/Design-Principles/acv.pdf)
* [Acyclic Visitor in WikiWikiWeb](https://wiki.c2.com/?AcyclicVisitor)

View File

@ -1,115 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<class-diagram version="1.2.2" 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.acyclicvisitor.ModemVisitor" project="acyclic-visitor"
file="/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/ModemVisitor.java" binary="false"
corner="BOTTOM_RIGHT">
<position height="-1" width="-1" x="860" y="67"/>
<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.acyclicvisitor.Modem" project="acyclic-visitor"
file="/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/Modem.java" binary="false" corner="BOTTOM_RIGHT">
<position height="-1" width="-1" x="327" y="77"/>
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
<operations public="true" package="true" protected="true" private="true" static="true"/>
</display>
</class>
<class id="3" language="java" name="com.iluwatar.acyclicvisitor.ConfigureForUnixVisitor" project="acyclic-visitor"
file="/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/ConfigureForUnixVisitor.java" binary="false"
corner="BOTTOM_RIGHT">
<position height="124" width="196" x="647" y="225"/>
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
<operations public="true" package="true" protected="true" private="true" static="true"/>
</display>
</class>
<class id="4" language="java" name="com.iluwatar.acyclicvisitor.Zoom" project="acyclic-visitor"
file="/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/Zoom.java" binary="false" corner="BOTTOM_RIGHT">
<position height="-1" width="-1" x="203" y="305"/>
<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="5" language="java" name="com.iluwatar.acyclicvisitor.HayesVisitor" project="acyclic-visitor"
file="/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/HayesVisitor.java" binary="false"
corner="BOTTOM_RIGHT">
<position height="-1" width="-1" x="1019" y="468"/>
<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="6" language="java" name="com.iluwatar.acyclicvisitor.ZoomVisitor" project="acyclic-visitor"
file="/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/ZoomVisitor.java" binary="false"
corner="BOTTOM_RIGHT">
<position height="-1" width="-1" x="758" y="467"/>
<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="7" language="java" name="com.iluwatar.acyclicvisitor.Hayes" project="acyclic-visitor"
file="/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/Hayes.java" binary="false" corner="BOTTOM_RIGHT">
<position height="-1" width="-1" x="479" y="307"/>
<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.acyclicvisitor.ConfigureForDosVisitor" project="acyclic-visitor"
file="/acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/ConfigureForDosVisitor.java" binary="false"
corner="BOTTOM_RIGHT">
<position height="142" width="192" x="883" y="225"/>
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
<operations public="true" package="true" protected="true" private="true" static="true"/>
</display>
</class>
<generalization id="9">
<end type="SOURCE" refId="7"/>
<end type="TARGET" refId="2"/>
</generalization>
<realization id="10">
<end type="SOURCE" refId="8"/>
<end type="TARGET" refId="6"/>
</realization>
<realization id="11">
<end type="SOURCE" refId="8"/>
<end type="TARGET" refId="5"/>
</realization>
<realization id="12">
<end type="SOURCE" refId="3"/>
<end type="TARGET" refId="6"/>
</realization>
<realization id="13">
<end type="SOURCE" refId="3"/>
<end type="TARGET" refId="1"/>
</realization>
<realization id="14">
<end type="SOURCE" refId="8"/>
<end type="TARGET" refId="1"/>
</realization>
<generalization id="15">
<end type="SOURCE" refId="4"/>
<end type="TARGET" refId="2"/>
</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>

Binary file not shown.

Before

Width:  |  Height:  |  Size: 26 KiB

After

Width:  |  Height:  |  Size: 48 KiB

View File

@ -21,7 +21,7 @@
<parent>
<groupId>com.iluwatar</groupId>
<artifactId>java-design-patterns</artifactId>
<version>1.23.0-SNAPSHOT</version>
<version>1.24.0-SNAPSHOT</version>
</parent>
<artifactId>acyclic-visitor</artifactId>

View File

@ -25,13 +25,23 @@ package com.iluwatar.acyclicvisitor;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
/**
* Tests that the Acyclic Visitor example runs without errors.
*/
public class AppTest {
class AppTest {
/**
* Issue: Add at least one assertion to this test case.
*
* Solution: Inserted assertion to check whether the execution of the main method in {@link App}
* throws an exception.
*/
@Test
public void test() {
App.main(new String[]{});
void shouldExecuteApplicationWithoutException() {
assertDoesNotThrow(() -> App.main(new String[]{}));
}
}

View File

@ -12,9 +12,8 @@ tags:
Wrapper
## Intent
Convert the interface of a class into another interface the clients
expect. Adapter lets classes work together that couldn't otherwise because of
incompatible interfaces.
Convert the interface of a class into another interface the clients expect. Adapter lets classes work together that
couldn't otherwise because of incompatible interfaces.
## Explanation

View File

@ -22,7 +22,7 @@
<parent>
<groupId>com.iluwatar</groupId>
<artifactId>java-design-patterns</artifactId>
<version>1.23.0-SNAPSHOT</version>
<version>1.24.0-SNAPSHOT</version>
</parent>
<artifactId>adapter</artifactId>
<dependencies>

View File

@ -25,12 +25,23 @@ package com.iluwatar.adapter;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
/**
* Tests that Adapter example runs without errors.
*/
public class AppTest {
class AppTest {
/**
* Issue: Add at least one assertion to this test case.
*
* Solution: Inserted assertion to check whether the execution of the main method in {@link App}
* throws an exception.
*/
@Test
public void test() {
App.main(new String[]{});
void shouldExecuteApplicationWithoutException() {
assertDoesNotThrow(() -> App.main(new String[]{}));
}
}

View File

@ -6,20 +6,97 @@ permalink: /patterns/aggregator-microservices/
categories: Architectural
tags:
- Cloud distributed
- Decoupling
- Microservices
---
## Intent
The user makes a single call to the Aggregator, and the aggregator then calls each relevant microservice and collects
the data, apply business logic to it, and further publish is as a REST Endpoint.
More variations of the aggregator are:
- Proxy Microservice Design Pattern: A different microservice is called upon the business need.
- Chained Microservice Design Pattern: In this case each microservice is dependent/ chained to a series
of other microservices.
The user makes a single call to the aggregator service, and the aggregator then calls each relevant microservice.
## Explanation
Real world example
> Our web marketplace needs information about products and their current inventory. It makes a call to an aggregator
> service which in turn calls the product information microservice and product inventory microservice returning the
> combined information.
In plain words
> Aggregator Microservice collects pieces of data from various microservices and returns an aggregate for processing.
Stack Overflow says
> Aggregator Microservice invokes multiple services to achieve the functionality required by the application.
**Programmatic Example**
Let's start from the data model. Here's our `Product`.
```java
public class Product {
private String title;
private int productInventories;
// getters and setters ->
...
}
```
Next we can introduce our `Aggregator` microservice. It contains clients `ProductInformationClient` and
`ProductInventoryClient` for calling respective microservices.
```java
@RestController
public class Aggregator {
@Resource
private ProductInformationClient informationClient;
@Resource
private ProductInventoryClient inventoryClient;
@RequestMapping(path = "/product", method = RequestMethod.GET)
public Product getProduct() {
var product = new Product();
var productTitle = informationClient.getProductTitle();
var productInventory = inventoryClient.getProductInventories();
//Fallback to error message
product.setTitle(requireNonNullElse(productTitle, "Error: Fetching Product Title Failed"));
//Fallback to default error inventory
product.setProductInventories(requireNonNullElse(productInventory, -1));
return product;
}
}
```
Here's the essence of information microservice implementation. Inventory microservice is similar, it just returns
inventory counts.
```java
@RestController
public class InformationController {
@RequestMapping(value = "/information", method = RequestMethod.GET)
public String getProductTitle() {
return "The Product Title.";
}
}
```
Now calling our `Aggregator` REST API returns the product information.
```bash
curl http://localhost:50004/product
{"title":"The Product Title.","productInventories":5}
```
## Class diagram
![alt text](./etc/aggregator-microservice.png "Aggregator Microservice")
![alt text](./aggregator-service/etc/aggregator-service.png "Aggregator Microservice")
## Applicability
@ -28,3 +105,5 @@ Use the Aggregator Microservices pattern when you need a unified API for various
## Credits
* [Microservice Design Patterns](http://web.archive.org/web/20190705163602/http://blog.arungupta.me/microservice-design-patterns/)
* [Microservices Patterns: With examples in Java](https://www.amazon.com/gp/product/1617294543/ref=as_li_qf_asin_il_tl?ie=UTF8&tag=javadesignpat-20&creative=9325&linkCode=as2&creativeASIN=1617294543&linkId=8b4e570267bc5fb8b8189917b461dc60)
* [Architectural Patterns: Uncover essential patterns in the most indispensable realm of enterprise architecture](https://www.amazon.com/gp/product/B077T7V8RC/ref=as_li_qf_asin_il_tl?ie=UTF8&tag=javadesignpat-20&creative=9325&linkCode=as2&creativeASIN=B077T7V8RC&linkId=c34d204bfe1b277914b420189f09c1a4)

Binary file not shown.

After

Width:  |  Height:  |  Size: 40 KiB

View File

@ -20,7 +20,7 @@
<parent>
<artifactId>aggregator-microservices</artifactId>
<groupId>com.iluwatar</groupId>
<version>1.23.0-SNAPSHOT</version>
<version>1.24.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>aggregator-service</artifactId>

Binary file not shown.

Before

Width:  |  Height:  |  Size: 40 KiB

View File

@ -20,7 +20,7 @@
<parent>
<artifactId>aggregator-microservices</artifactId>
<groupId>com.iluwatar</groupId>
<version>1.23.0-SNAPSHOT</version>
<version>1.24.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -20,7 +20,7 @@
<parent>
<artifactId>aggregator-microservices</artifactId>
<groupId>com.iluwatar</groupId>
<version>1.23.0-SNAPSHOT</version>
<version>1.24.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>inventory-microservice</artifactId>

View File

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

View File

@ -10,28 +10,37 @@ tags:
---
## Intent
Provide a helper service instance on a client and offload common functionality away from a shared resource.
## Explanation
Real world example
> A remote service has many clients accessing a function it provides. The service is a legacy application and is impossible to update. Large numbers of requests from users are causing connectivity issues. New rules for request frequency should be implemented along with latency checks and client-side logging.
> A remote service has many clients accessing a function it provides. The service is a legacy application and is
> impossible to update. Large numbers of requests from users are causing connectivity issues. New rules for request
> frequency should be implemented along with latency checks and client-side logging.
In plain words
> Using the ambassador pattern, we can implement less-frequent polling from clients along with latency checks and logging.
> With the Ambassador pattern, we can implement less-frequent polling from clients along with latency checks and
> logging.
Microsoft documentation states
> An ambassador service can be thought of as an out-of-process proxy that is co-located with the client. This pattern can be useful for offloading common client connectivity tasks such as monitoring, logging, routing, security (such as TLS), and resiliency patterns in a language agnostic way. It is often used with legacy applications, or other applications that are difficult to modify, in order to extend their networking capabilities. It can also enable a specialized team to implement those features.
> An ambassador service can be thought of as an out-of-process proxy which is co-located with the client. This pattern
> can be useful for offloading common client connectivity tasks such as monitoring, logging, routing,
> security (such as TLS), and resiliency patterns in a language agnostic way. It is often used with legacy applications,
> or other applications that are difficult to modify, in order to extend their networking capabilities. It can also
> enable a specialized team to implement those features.
**Programmatic Example**
With the above example in mind we will imitate the functionality in a simple manner. We have an interface implemented by the remote service as well as the ambassador service:
With the above introduction in mind we will imitate the functionality in this example. We have an interface implemented
by the remote service as well as the ambassador service:
```java
interface RemoteServiceInterface {
long doRemoteFunction(int value) throws Exception;
}
```
@ -136,7 +145,7 @@ public class Client {
}
```
And here are two clients using the service.
Here are two clients using the service.
```java
public class App {
@ -149,13 +158,29 @@ public class App {
}
```
Here's the output for running the example:
```java
Time taken (ms): 111
Service result: 120
Time taken (ms): 931
Failed to reach remote: (1)
Time taken (ms): 665
Failed to reach remote: (2)
Time taken (ms): 538
Failed to reach remote: (3)
Service result: -1
```
## Class diagram
![alt text](./etc/ambassador.urm.png "Ambassador class diagram")
## Applicability
Ambassador is applicable when working with a legacy remote service that cannot
be modified or would be extremely difficult to modify. Connectivity features can
be implemented on the client avoiding the need for changes on the remote service.
Ambassador is applicable when working with a legacy remote service which cannot be modified or would be extremely
difficult to modify. Connectivity features can be implemented on the client avoiding the need for changes on the remote
service.
* Ambassador provides a local interface for a remote service.
* Ambassador provides logging, circuit breaking, retries and security on the client.
@ -168,10 +193,14 @@ be implemented on the client avoiding the need for changes on the remote service
* Offload remote service tasks
* Facilitate network connection
## Real world examples
## Known uses
* [Kubernetes-native API gateway for microservices](https://github.com/datawire/ambassador)
## Related patterns
* [Proxy](https://java-design-patterns.com/patterns/proxy/)
## Credits
* [Ambassador pattern](https://docs.microsoft.com/en-us/azure/architecture/patterns/ambassador)

View File

@ -20,7 +20,7 @@
<parent>
<artifactId>java-design-patterns</artifactId>
<groupId>com.iluwatar</groupId>
<version>1.23.0-SNAPSHOT</version>
<version>1.24.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>ambassador</artifactId>

View File

@ -62,7 +62,7 @@ public class RemoteService implements RemoteServiceInterface {
*
* @param value integer value to be multiplied.
* @return if waitTime is less than {@link RemoteService#THRESHOLD}, it returns value * 10,
* otherwise {@link RemoteServiceInterface#FAILURE}.
* otherwise {@link RemoteServiceStatus#FAILURE}.
*/
@Override
public long doRemoteFunction(int value) {
@ -74,6 +74,7 @@ public class RemoteService implements RemoteServiceInterface {
} catch (InterruptedException e) {
LOGGER.error("Thread sleep state interrupted", e);
}
return waitTime <= THRESHOLD ? value * 10 : FAILURE;
return waitTime <= THRESHOLD ? value * 10
: RemoteServiceStatus.FAILURE.getRemoteServiceStatusValue();
}
}

View File

@ -27,7 +27,6 @@ package com.iluwatar.ambassador;
* Interface shared by ({@link RemoteService}) and ({@link ServiceAmbassador}).
*/
interface RemoteServiceInterface {
int FAILURE = -1;
long doRemoteFunction(int value) throws Exception;
long doRemoteFunction(int value);
}

View File

@ -0,0 +1,48 @@
/*
* The MIT License
* Copyright © 2014-2019 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.ambassador;
/**
* Holds information regarding the status of the Remote Service.
*
* <p> This Enum replaces the integer value previously
* stored in {@link RemoteServiceInterface} as SonarCloud was identifying
* it as an issue. All test cases have been checked after changes,
* without failures. </p>
*/
public enum RemoteServiceStatus {
FAILURE(-1)
;
private final long remoteServiceStatusValue;
RemoteServiceStatus(long remoteServiceStatusValue) {
this.remoteServiceStatusValue = remoteServiceStatusValue;
}
public long getRemoteServiceStatusValue() {
return remoteServiceStatusValue;
}
}

View File

@ -23,6 +23,7 @@
package com.iluwatar.ambassador;
import static com.iluwatar.ambassador.RemoteServiceStatus.FAILURE;
import static java.lang.Thread.sleep;
import org.slf4j.Logger;
@ -58,14 +59,14 @@ public class ServiceAmbassador implements RemoteServiceInterface {
private long safeCall(int value) {
var retries = 0;
var result = (long) FAILURE;
var result = FAILURE.getRemoteServiceStatusValue();
for (int i = 0; i < RETRIES; i++) {
if (retries >= RETRIES) {
return FAILURE;
return FAILURE.getRemoteServiceStatusValue();
}
if ((result = checkLatency(value)) == FAILURE) {
if ((result = checkLatency(value)) == FAILURE.getRemoteServiceStatusValue()) {
LOGGER.info("Failed to reach remote: (" + (i + 1) + ")");
retries++;
try {

View File

@ -25,13 +25,23 @@ package com.iluwatar.ambassador;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
/**
* Application test
*/
class AppTest {
/**
* Issue: Add at least one assertion to this test case.
*
* Solution: Inserted assertion to check whether the execution of the main method in {@link App}
* throws an exception.
*/
@Test
void test() {
App.main(new String[]{});
void shouldExecuteApplicationWithoutException() {
assertDoesNotThrow(() -> App.main(new String[]{}));
}
}

View File

@ -37,6 +37,6 @@ class ClientTest {
Client client = new Client();
var result = client.useService(10);
assertTrue(result == 100 || result == RemoteService.FAILURE);
assertTrue(result == 100 || result == RemoteServiceStatus.FAILURE.getRemoteServiceStatusValue());
}
}

View File

@ -37,7 +37,7 @@ class RemoteServiceTest {
void testFailedCall() {
var remoteService = new RemoteService(new StaticRandomProvider(0.21));
var result = remoteService.doRemoteFunction(10);
assertEquals(RemoteServiceInterface.FAILURE, result);
assertEquals(RemoteServiceStatus.FAILURE.getRemoteServiceStatusValue(), result);
}
@Test

View File

@ -35,6 +35,6 @@ class ServiceAmbassadorTest {
@Test
void test() {
long result = new ServiceAmbassador().doRemoteFunction(10);
assertTrue(result == 100 || result == RemoteServiceInterface.FAILURE);
assertTrue(result == 100 || result == RemoteServiceStatus.FAILURE.getRemoteServiceStatusValue());
}
}

View File

@ -10,51 +10,59 @@ tags:
- Microservices
---
## Also knows as
Gateway Routing
## Intent
Aggregate calls to microservices in a single location: the API Gateway. The user makes a single call to the API Gateway,
and the API Gateway then calls each relevant microservice.
Aggregate calls to microservices in a single location, the API Gateway. The user makes a single call
to the API Gateway, and the API Gateway then calls each relevant microservice.
## Explanation
With the Microservices pattern, a client may need data from multiple different microservices. If the client called each
microservice directly, that could contribute to longer load times, since the client would have to make a network request
for each microservice called. Moreover, having the client call each microservice directly ties the client to that
microservice - if the internal implementations of the microservices change (for example, if two microservices are
combined sometime in the future) or if the location (host and port) of a microservice changes, then every client that
With the Microservices pattern, a client may need data from multiple different microservices. If the
client called each microservice directly, that could contribute to longer load times, since the
client would have to make a network request for each microservice called. Moreover, having the
client call each microservice directly ties the client to that microservice - if the internal
implementations of the microservices change (for example, if two microservices are combined sometime
in the future) or if the location (host and port) of a microservice changes, then every client that
makes use of those microservices must be updated.
The intent of the API Gateway pattern is to alleviate some of these issues. In the API Gateway pattern, an additional
entity (the API Gateway) is placed between the client and the microservices. The job of the API Gateway is to aggregate
the calls to the microservices. Rather than the client calling each microservice individually, the client calls the
API Gateway a single time. The API Gateway then calls each of the microservices that the client needs.
The intent of the API Gateway pattern is to alleviate some of these issues. In the API Gateway
pattern, an additional entity (the API Gateway) is placed between the client and the microservices.
The job of the API Gateway is to aggregate the calls to the microservices. Rather than the client
calling each microservice individually, the client calls the API Gateway a single time. The API
Gateway then calls each of the microservices that the client needs.
Real world example
> We are implementing microservices and API Gateway pattern for an e-commerce site. In this system the API Gateway makes
calls to the Image and Price microservices.
> We are implementing microservices and API Gateway pattern for an e-commerce site. In this system
> the API Gateway makes calls to the Image and Price microservices.
In plain words
> For a system implemented using microservices architecture, API Gateway is the single entry point that aggregates the
calls to the individual microservices.
> For a system implemented using microservices architecture, API Gateway is the single entry point
> that aggregates the calls to the individual microservices.
Wikipedia says
> API Gateway is a server that acts as an API front-end, receives API requests, enforces throttling and security
policies, passes requests to the back-end service and then passes the response back to the requester. A gateway often
includes a transformation engine to orchestrate and modify the requests and responses on the fly. A gateway can also
provide functionality such as collecting analytics data and providing caching. The gateway can provide functionality to
support authentication, authorization, security, audit and regulatory compliance.
> API Gateway is a server that acts as an API front-end, receives API requests, enforces throttling
> and security policies, passes requests to the back-end service and then passes the response back
> to the requester. A gateway often includes a transformation engine to orchestrate and modify the
> requests and responses on the fly. A gateway can also provide functionality such as collecting
> analytics data and providing caching. The gateway can provide functionality to support
> authentication, authorization, security, audit and regulatory compliance.
**Programmatic Example**
This implementation shows what the API Gateway pattern could look like for an e-commerce site. The `ApiGateway` makes
calls to the Image and Price microservices using the `ImageClientImpl` and `PriceClientImpl` respectively. Customers
viewing the site on a desktop device can see both price information and an image of a product, so the `ApiGateway` calls
both of the microservices and aggregates the data in the `DesktopProduct` model. However, mobile users only see price
information; they do not see a product image. For mobile users, the `ApiGateway` only retrieves price information, which
it uses to populate the `MobileProduct`.
This implementation shows what the API Gateway pattern could look like for an e-commerce site. The
`ApiGateway` makes calls to the Image and Price microservices using the `ImageClientImpl` and
`PriceClientImpl` respectively. Customers viewing the site on a desktop device can see both price
information and an image of a product, so the `ApiGateway` calls both of the microservices and
aggregates the data in the `DesktopProduct` model. However, mobile users only see price information;
they do not see a product image. For mobile users, the `ApiGateway` only retrieves price
information, which it uses to populate the `MobileProduct`.
Here's the Image microservice implementation.
@ -64,7 +72,6 @@ public interface ImageClient {
}
public class ImageClientImpl implements ImageClient {
@Override
public String getImagePath() {
var httpClient = HttpClient.newHttpClient();
@ -114,7 +121,7 @@ public class PriceClientImpl implements PriceClient {
}
```
And here we can see how API Gateway maps the requests to the microservices.
Here we can see how API Gateway maps the requests to the microservices.
```java
public class ApiGateway {
@ -143,6 +150,7 @@ public class ApiGateway {
```
## Class diagram
![alt text](./etc/api-gateway.png "API Gateway")
## Applicability
@ -157,3 +165,4 @@ Use the API Gateway pattern when
* [NGINX - Building Microservices: Using an API Gateway](https://www.nginx.com/blog/building-microservices-using-an-api-gateway/)
* [Microservices Patterns: With examples in Java](https://www.amazon.com/gp/product/1617294543/ref=as_li_qf_asin_il_tl?ie=UTF8&tag=javadesignpat-20&creative=9325&linkCode=as2&creativeASIN=1617294543&linkId=ac7b6a57f866ac006a309d9086e8cfbd)
* [Building Microservices: Designing Fine-Grained Systems](https://www.amazon.com/gp/product/1491950358/ref=as_li_qf_asin_il_tl?ie=UTF8&tag=javadesignpat-20&creative=9325&linkCode=as2&creativeASIN=1491950358&linkId=4c95ca9831e05e3f0dadb08841d77bf1)
* [Gateway Routing pattern](https://docs.microsoft.com/en-us/azure/architecture/patterns/gateway-routing)

View File

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

View File

@ -23,11 +23,16 @@
package com.iluwatar.api.gateway;
import static org.slf4j.LoggerFactory.getLogger;
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.net.http.HttpResponse.BodyHandlers;
import org.slf4j.Logger;
import org.springframework.stereotype.Component;
/**
@ -35,6 +40,8 @@ import org.springframework.stereotype.Component;
*/
@Component
public class ImageClientImpl implements ImageClient {
private static final Logger LOGGER = getLogger(ImageClientImpl.class);
/**
* Makes a simple HTTP Get request to the Image microservice.
*
@ -49,12 +56,26 @@ public class ImageClientImpl implements ImageClient {
.build();
try {
LOGGER.info("Sending request to fetch image path");
var httpResponse = httpClient.send(httpGet, BodyHandlers.ofString());
logResponse(httpResponse);
return httpResponse.body();
} catch (IOException | InterruptedException e) {
e.printStackTrace();
LOGGER.error("Failure occurred while getting image path", e);
}
return null;
}
private void logResponse(HttpResponse<String> httpResponse) {
if (isSuccessResponse(httpResponse.statusCode())) {
LOGGER.info("Image path received successfully");
} else {
LOGGER.warn("Image path request failed");
}
}
private boolean isSuccessResponse(int responseCode) {
return responseCode >= 200 && responseCode <= 299;
}
}

View File

@ -23,18 +23,26 @@
package com.iluwatar.api.gateway;
import static org.slf4j.LoggerFactory.getLogger;
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.net.http.HttpResponse.BodyHandlers;
import org.slf4j.Logger;
import org.springframework.stereotype.Component;
/**
* An adapter to communicate with the Price microservice.
*/
@Component
public class PriceClientImpl implements PriceClient {
private static final Logger LOGGER = getLogger(PriceClientImpl.class);
/**
* Makes a simple HTTP Get request to the Price microservice.
*
@ -49,12 +57,26 @@ public class PriceClientImpl implements PriceClient {
.build();
try {
LOGGER.info("Sending request to fetch price info");
var httpResponse = httpClient.send(httpGet, BodyHandlers.ofString());
logResponse(httpResponse);
return httpResponse.body();
} catch (IOException | InterruptedException e) {
e.printStackTrace();
LOGGER.error("Failure occurred while getting price info", e);
}
return null;
}
private void logResponse(HttpResponse<String> httpResponse) {
if (isSuccessResponse(httpResponse.statusCode())) {
LOGGER.info("Price info received successfully");
} else {
LOGGER.warn("Price info request failed");
}
}
private boolean isSuccessResponse(int responseCode) {
return responseCode >= 200 && responseCode <= 299;
}
}

View File

@ -29,7 +29,7 @@
<parent>
<artifactId>api-gateway</artifactId>
<groupId>com.iluwatar</groupId>
<version>1.23.0-SNAPSHOT</version>
<version>1.24.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>image-microservice</artifactId>

View File

@ -23,15 +23,20 @@
package com.iluwatar.image.microservice;
import static org.slf4j.LoggerFactory.getLogger;
import org.slf4j.Logger;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
/**
* Exposes the Image microservice's endpoints.
*/
@RestController
public class ImageController {
private static final Logger LOGGER = getLogger(ImageController.class);
/**
* An endpoint for a user to retrieve an image path.
@ -40,6 +45,7 @@ public class ImageController {
*/
@RequestMapping(value = "/image-path", method = RequestMethod.GET)
public String getImagePath() {
LOGGER.info("Successfully found image path");
return "/product-image.png";
}
}

View File

@ -29,7 +29,7 @@
<parent>
<artifactId>java-design-patterns</artifactId>
<groupId>com.iluwatar</groupId>
<version>1.23.0-SNAPSHOT</version>
<version>1.24.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.23.0-SNAPSHOT</version>
<version>1.24.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -23,15 +23,20 @@
package com.iluwatar.price.microservice;
import static org.slf4j.LoggerFactory.getLogger;
import org.slf4j.Logger;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
/**
* Exposes the Price microservice's endpoints.
*/
@RestController
public class PriceController {
private static final Logger LOGGER = getLogger(PriceController.class);
/**
* An endpoint for a user to retrieve a product's price.
@ -40,6 +45,7 @@ public class PriceController {
*/
@RequestMapping(value = "/price", method = RequestMethod.GET)
public String getPrice() {
LOGGER.info("Successfully found price info");
return "20";
}
}

View File

@ -9,22 +9,137 @@ tags:
---
## Also known as
Given/When/Then
## Intent
The Arrange/Act/Assert (AAA) is a pattern for organizing unit tests.
Arrange/Act/Assert (AAA) is a pattern for organizing unit tests.
It breaks tests down into three clear and distinct steps:
1. Arrange: Perform the setup and initialization required for the test.
2. Act: Take action(s) required for the test.
3. Assert: Verify the outcome(s) of the test.
## Explanation
This pattern has several significant benefits. It creates a clear separation between a test's
setup, operations, and results. This structure makes the code easier to read and understand. If
you place the steps in order and format your code to separate them, you can scan a test and
quickly comprehend what it does.
It also enforces a certain degree of discipline when you write your tests. You have to think
clearly about the three steps your test will perform. It makes tests more natural to write at
the same time since you already have an outline.
Real world example
> We need to write comprehensive and clear unit test suite for a class.
In plain words
> Arrange/Act/Assert is a testing pattern that organizes tests into three clear steps for easy
> maintenance.
WikiWikiWeb says
> Arrange/Act/Assert is a pattern for arranging and formatting code in UnitTest methods.
**Programmatic Example**
Let's first introduce our `Cash` class to be unit tested.
```java
public class Cash {
private int amount;
Cash(int amount) {
this.amount = amount;
}
void plus(int addend) {
amount += addend;
}
boolean minus(int subtrahend) {
if (amount >= subtrahend) {
amount -= subtrahend;
return true;
} else {
return false;
}
}
int count() {
return amount;
}
}
```
Then we write our unit tests according to Arrange/Act/Assert pattern. Notice the clearly
separated steps for each unit test.
```java
public class CashAAATest {
@Test
public void testPlus() {
//Arrange
var cash = new Cash(3);
//Act
cash.plus(4);
//Assert
assertEquals(7, cash.count());
}
@Test
public void testMinus() {
//Arrange
var cash = new Cash(8);
//Act
var result = cash.minus(5);
//Assert
assertTrue(result);
assertEquals(3, cash.count());
}
@Test
public void testInsufficientMinus() {
//Arrange
var cash = new Cash(1);
//Act
var result = cash.minus(6);
//Assert
assertFalse(result);
assertEquals(1, cash.count());
}
@Test
public void testUpdate() {
//Arrange
var cash = new Cash(5);
//Act
cash.plus(6);
var result = cash.minus(3);
//Assert
assertTrue(result);
assertEquals(8, cash.count());
}
}
```
## Applicability
Use Arrange/Act/Assert pattern when
* you need to structure your unit tests so they're easier to read, maintain, and enhance.
* You need to structure your unit tests so that they're easier to read, maintain, and enhance.
## Credits
* [Arrange, Act, Assert: What is AAA Testing?](https://blog.ncrunch.net/post/arrange-act-assert-aaa-testing.aspx)
* [Bill Wake: 3A Arrange, Act, Assert](https://xp123.com/articles/3a-arrange-act-assert/)
* [Martin Fowler: GivenWhenThen](https://martinfowler.com/bliki/GivenWhenThen.html)
* [xUnit Test Patterns: Refactoring Test Code](https://www.amazon.com/gp/product/0131495054/ref=as_li_qf_asin_il_tl?ie=UTF8&tag=javadesignpat-20&creative=9325&linkCode=as2&creativeASIN=0131495054&linkId=99701e8f4af2f7e8dd50d720c9b63dbf)
* [Unit Testing Principles, Practices, and Patterns](https://www.amazon.com/gp/product/1617296279/ref=as_li_qf_asin_il_tl?ie=UTF8&tag=javadesignpat-20&creative=9325&linkCode=as2&creativeASIN=1617296279&linkId=74c75cf22a63c3e4758ae08aa0a0cc35)
* [Test Driven Development: By Example](https://www.amazon.com/gp/product/0321146530/ref=as_li_qf_asin_il_tl?ie=UTF8&tag=javadesignpat-20&creative=9325&linkCode=as2&creativeASIN=0321146530&linkId=5c63a93d8c1175b84ca5087472ef0e05)

View File

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

View File

@ -60,7 +60,7 @@ public class CashAAATest {
//Act
cash.plus(4);
//Assert
assertEquals(cash.count(), 7);
assertEquals(7, cash.count());
}
@Test
@ -71,7 +71,7 @@ public class CashAAATest {
var result = cash.minus(5);
//Assert
assertTrue(result);
assertEquals(cash.count(), 3);
assertEquals(3, cash.count());
}
@Test
@ -82,7 +82,7 @@ public class CashAAATest {
var result = cash.minus(6);
//Assert
assertFalse(result);
assertEquals(cash.count(), 1);
assertEquals(1, cash.count());
}
@Test
@ -94,6 +94,6 @@ public class CashAAATest {
var result = cash.minus(3);
//Assert
assertTrue(result);
assertEquals(cash.count(), 8);
assertEquals(8, cash.count());
}
}

View File

@ -44,16 +44,16 @@ public class CashAntiAAATest {
var cash = new Cash(3);
//test plus
cash.plus(4);
assertEquals(cash.count(), 7);
assertEquals(7, cash.count());
//test minus
cash = new Cash(8);
assertTrue(cash.minus(5));
assertEquals(cash.count(), 3);
assertEquals(3, cash.count());
assertFalse(cash.minus(6));
assertEquals(cash.count(), 3);
assertEquals(3, cash.count());
//test update
cash.plus(5);
assertTrue(cash.minus(5));
assertEquals(cash.count(), 3);
assertEquals(3, cash.count());
}
}

View File

@ -22,7 +22,7 @@
<parent>
<groupId>com.iluwatar</groupId>
<artifactId>java-design-patterns</artifactId>
<version>1.23.0-SNAPSHOT</version>
<version>1.24.0-SNAPSHOT</version>
</parent>
<artifactId>async-method-invocation</artifactId>
<dependencies>

View File

@ -25,12 +25,24 @@ package com.iluwatar.async.method.invocation;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
/**
* Application test
*/
class AppTest {
/**
* Issue: Add at least one assertion to this test case.
*
* Solution: Inserted assertion to check whether the execution of the main method in {@link App}
* throws an exception.
*/
@Test
void test() throws Exception {
App.main(new String[]{});
void shouldExecuteApplicationWithoutException() {
assertDoesNotThrow(() -> App.main(new String[]{}));
}
}

View File

@ -20,7 +20,7 @@
<parent>
<artifactId>java-design-patterns</artifactId>
<groupId>com.iluwatar</groupId>
<version>1.23.0-SNAPSHOT</version>
<version>1.24.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -24,15 +24,26 @@
package com.iluwatar.balking;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.function.Executable;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
/**
* Application test
*/
class AppTest {
/**
* Issue: Add at least one assertion to this test case.
*
* Solution: Inserted assertion to check whether the execution of the main method in {@link App}
* throws an exception.
*/
@Test
void main() {
App.main();
void shouldExecuteApplicationWithoutException() {
assertDoesNotThrow((Executable) App::main);
}
}

View File

@ -9,20 +9,26 @@ tags:
---
## Also known as
Handle/Body
## Intent
Decouple an abstraction from its implementation so that the two can vary independently.
## Explanation
Real world example
> Consider you have a weapon with different enchantments and you are supposed to allow mixing different weapons with different enchantments. What would you do? Create multiple copies of each of the weapons for each of the enchantments or would you just create separate enchantment and set it for the weapon as needed? Bridge pattern allows you to do the second.
> Consider you have a weapon with different enchantments, and you are supposed to allow mixing
> different weapons with different enchantments. What would you do? Create multiple copies of each
> of the weapons for each of the enchantments or would you just create separate enchantment and set
> it for the weapon as needed? Bridge pattern allows you to do the second.
In Plain Words
> Bridge pattern is about preferring composition over inheritance. Implementation details are pushed from a hierarchy to another object with a separate hierarchy.
> Bridge pattern is about preferring composition over inheritance. Implementation details are pushed
> from a hierarchy to another object with a separate hierarchy.
Wikipedia says
@ -30,7 +36,7 @@ Wikipedia says
**Programmatic Example**
Translating our weapon example from above. Here we have the `Weapon` hierarchy
Translating our weapon example from above. Here we have the `Weapon` hierarchy:
```java
public interface Weapon {
@ -105,7 +111,7 @@ public class Hammer implements Weapon {
}
```
And the separate enchantment hierarchy
Here's the separate enchantment hierarchy:
```java
public interface Enchantment {
@ -151,7 +157,7 @@ public class SoulEatingEnchantment implements Enchantment {
}
```
And both the hierarchies in action
Here are both hierarchies in action:
```java
var enchantedSword = new Sword(new SoulEatingEnchantment());
@ -178,18 +184,21 @@ hammer.unwield();
```
## Class diagram
![alt text](./etc/bridge.urm.png "Bridge class diagram")
## Applicability
Use the Bridge pattern when
* you want to avoid a permanent binding between an abstraction and its implementation. This might be the case, for example, when the implementation must be selected or switched at run-time.
* both the abstractions and their implementations should be extensible by subclassing. In this case, the Bridge pattern lets you combine the different abstractions and implementations and extend them independently
* changes in the implementation of an abstraction should have no impact on clients; that is, their code should not have to be recompiled.
* you have a proliferation of classes. Such a class hierarchy indicates the need for splitting an object into two parts. Rumbaugh uses the term "nested generalizations" to refer to such class hierarchies
* you want to share an implementation among multiple objects (perhaps using reference counting), and this fact should be hidden from the client. A simple example is Coplien's String class, in which multiple objects can share the same string representation.
* You want to avoid a permanent binding between an abstraction and its implementation. This might be the case, for example, when the implementation must be selected or switched at run-time.
* Both the abstractions and their implementations should be extensible by subclassing. In this case, the Bridge pattern lets you combine the different abstractions and implementations and extend them independently.
* Changes in the implementation of an abstraction should have no impact on clients; that is, their code should not have to be recompiled.
* You have a proliferation of classes. Such a class hierarchy indicates the need for splitting an object into two parts. Rumbaugh uses the term "nested generalizations" to refer to such class hierarchies.
* You want to share an implementation among multiple objects (perhaps using reference counting), and this fact should be hidden from the client. A simple example is Coplien's String class, in which multiple objects can share the same string representation.
## Tutorial
* [Bridge Pattern Tutorial](https://www.journaldev.com/1491/bridge-design-pattern-java)
## Credits

View File

@ -22,7 +22,7 @@
<parent>
<groupId>com.iluwatar</groupId>
<artifactId>java-design-patterns</artifactId>
<version>1.23.0-SNAPSHOT</version>
<version>1.24.0-SNAPSHOT</version>
</parent>
<artifactId>bridge</artifactId>
<dependencies>

View File

@ -25,12 +25,22 @@ package com.iluwatar.bridge;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
/**
* Application test
*/
class AppTest {
/**
* Issue: Add at least one assertion to this test case.
*
* Solution: Inserted assertion to check whether the execution of the main method in {@link App}
* throws an exception.
*/
@Test
void test() {
App.main(new String[]{});
void shouldExecuteApplicationWithoutException() {
assertDoesNotThrow(() -> App.main(new String[]{}));
}
}

View File

@ -9,36 +9,47 @@ tags:
---
## Intent
Separate the construction of a complex object from its
representation so that the same construction process can create different
representations.
Separate the construction of a complex object from its representation so that the same construction
process can create different representations.
## Explanation
Real world example
> Imagine a character generator for a role playing game. The easiest option is to let computer create the character for you. But if you want to select the character details like profession, gender, hair color etc. the character generation becomes a step-by-step process that completes when all the selections are ready.
> Imagine a character generator for a role-playing game. The easiest option is to let the computer
> create the character for you. If you want to manually select the character details like
> profession, gender, hair color etc. the character generation becomes a step-by-step process that
> completes when all the selections are ready.
In plain words
> Allows you to create different flavors of an object while avoiding constructor pollution. Useful when there could be several flavors of an object. Or when there are a lot of steps involved in creation of an object.
> Allows you to create different flavors of an object while avoiding constructor pollution. Useful
> when there could be several flavors of an object. Or when there are a lot of steps involved in
> creation of an object.
Wikipedia says
> The builder pattern is an object creation software design pattern with the intentions of finding a solution to the telescoping constructor anti-pattern.
> The builder pattern is an object creation software design pattern with the intentions of finding
> a solution to the telescoping constructor anti-pattern.
Having said that let me add a bit about what telescoping constructor anti-pattern is. At one point or the other we have all seen a constructor like below:
Having said that let me add a bit about what telescoping constructor anti-pattern is. At one point
or the other, we have all seen a constructor like below:
```java
public Hero(Profession profession, String name, HairType hairType, HairColor hairColor, Armor armor, Weapon weapon) {
}
```
As you can see the number of constructor parameters can quickly get out of hand and it might become difficult to understand the arrangement of parameters. Plus this parameter list could keep on growing if you would want to add more options in future. This is called telescoping constructor anti-pattern.
As you can see the number of constructor parameters can quickly get out of hand, and it may become
difficult to understand the arrangement of parameters. Plus this parameter list could keep on
growing if you would want to add more options in the future. This is called telescoping constructor
anti-pattern.
**Programmatic Example**
The sane alternative is to use the Builder pattern. First of all we have our hero that we want to create
The sane alternative is to use the Builder pattern. First of all we have our hero that we want to
create:
```java
public final class Hero {
@ -60,7 +71,7 @@ public final class Hero {
}
```
And then we have the builder
Then we have the builder:
```java
public static class Builder {
@ -105,20 +116,22 @@ And then we have the builder
}
```
And then it can be used as:
Then it can be used as:
```java
var mage = new Hero.Builder(Profession.MAGE, "Riobard").withHairColor(HairColor.BLACK).withWeapon(Weapon.DAGGER).build();
```
## Class diagram
![alt text](./etc/builder.urm.png "Builder class diagram")
## Applicability
Use the Builder pattern when
* the algorithm for creating a complex object should be independent of the parts that make up the object and how they're assembled
* the construction process must allow different representations for the object that's constructed
* The algorithm for creating a complex object should be independent of the parts that make up the object and how they're assembled
* The construction process must allow different representations for the object that's constructed
## Real world examples

View File

@ -22,7 +22,7 @@
<parent>
<groupId>com.iluwatar</groupId>
<artifactId>java-design-patterns</artifactId>
<version>1.23.0-SNAPSHOT</version>
<version>1.24.0-SNAPSHOT</version>
</parent>
<artifactId>builder</artifactId>
<dependencies>

View File

@ -25,12 +25,23 @@ package com.iluwatar.builder;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
/**
* Application test
*/
class AppTest {
/**
* Issue: Add at least one assertion to this test case.
*
* Solution: Inserted assertion to check whether the execution of the main method in {@link App}
* throws an exception.
*/
@Test
void test() {
App.main(new String[]{});
void shouldExecuteApplicationWithoutException() {
assertDoesNotThrow(() -> App.main(new String[]{}));
}
}

View File

@ -22,7 +22,7 @@
<parent>
<groupId>com.iluwatar</groupId>
<artifactId>java-design-patterns</artifactId>
<version>1.23.0-SNAPSHOT</version>
<version>1.24.0-SNAPSHOT</version>
</parent>
<artifactId>business-delegate</artifactId>
<dependencies>

View File

@ -27,13 +27,23 @@ import org.junit.jupiter.api.Test;
import java.io.IOException;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
/**
* Tests that Business Delegate example runs without errors.
*/
public class AppTest {
class AppTest {
/**
* Issue: Add at least one assertion to this test case.
*
* Solution: Inserted assertion to check whether the execution of the main method in {@link App}
* throws an exception.
*/
@Test
public void test() throws IOException {
String[] args = {};
App.main(args);
void shouldExecuteApplicationWithoutException() {
assertDoesNotThrow(() -> App.main(new String[]{}));
}
}

View File

@ -20,7 +20,7 @@
<parent>
<artifactId>java-design-patterns</artifactId>
<groupId>com.iluwatar</groupId>
<version>1.23.0-SNAPSHOT</version>
<version>1.24.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -58,12 +58,14 @@ public class App {
var vm = new VirtualMachine();
vm.getWizards()[0] = wizard;
interpretInstruction("LITERAL 0", vm);
interpretInstruction("LITERAL 0", vm);
String literal = "LITERAL 0";
interpretInstruction(literal, vm);
interpretInstruction(literal, vm);
interpretInstruction("GET_HEALTH", vm);
interpretInstruction("LITERAL 0", vm);
interpretInstruction(literal, vm);
interpretInstruction("GET_AGILITY", vm);
interpretInstruction("LITERAL 0", vm);
interpretInstruction(literal, vm);
interpretInstruction("GET_WISDOM ", vm);
interpretInstruction("ADD", vm);
interpretInstruction("LITERAL 2", vm);

View File

@ -25,13 +25,22 @@ package com.iluwatar.bytecode;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
/**
* Application test
*/
public class AppTest {
class AppTest {
/**
* Issue: Add at least one assertion to this test case.
*
* Solution: Inserted assertion to check whether the execution of the main method in {@link App}
* throws an exception.
*/
@Test
public void test() {
App.main(new String[]{});
void shouldExecuteApplicationWithoutException() {
assertDoesNotThrow(() -> App.main(new String[]{}));
}
}

View File

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

View File

@ -27,12 +27,23 @@ import org.junit.jupiter.api.Test;
import java.io.IOException;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
/**
* Tests that Caching example runs without errors.
*/
public class AppTest {
class AppTest {
/**
* Issue: Add at least one assertion to this test case.
*
* Solution: Inserted assertion to check whether the execution of the main method in {@link App}
* throws an exception.
*/
@Test
public void test() {
App.main(new String[]{});
void shouldExecuteApplicationWithoutException() {
assertDoesNotThrow(() -> App.main(new String[]{}));
}
}

View File

@ -9,14 +9,16 @@ tags:
---
## Intent
Callback is a piece of executable code that is passed as an argument to other code, which is expected to call back
(execute) the argument at some convenient time.
Callback is a piece of executable code that is passed as an argument to other code, which is
expected to call back (execute) the argument at some convenient time.
## Explanation
Real world example
> We need to be notified after executing task has finished. We pass a callback method for the executor and wait for it to call back on us.
> We need to be notified after executing task has finished. We pass a callback method for
> the executor and wait for it to call back on us.
In plain words
@ -24,7 +26,9 @@ In plain words
Wikipedia says
> In computer programming, a callback, also known as a "call-after" function, is any executable code that is passed as an argument to other code; that other code is expected to call back (execute) the argument at a given time.
> In computer programming, a callback, also known as a "call-after" function, is any executable
> code that is passed as an argument to other code; that other code is expected to call
> back (execute) the argument at a given time.
**Programmatic Example**
@ -61,7 +65,7 @@ public final class SimpleTask extends Task {
}
```
Finally here's how we execute a task and receive a callback when it's finished.
Finally, here's how we execute a task and receive a callback when it's finished.
```java
var task = new SimpleTask();
@ -69,13 +73,15 @@ Finally here's how we execute a task and receive a callback when it's finished.
```
## Class diagram
![alt text](./etc/callback.png "Callback")
## Applicability
Use the Callback pattern when
* when some arbitrary synchronous or asynchronous action must be performed after execution of some defined activity.
## Real world examples
* [CyclicBarrier](http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/CyclicBarrier.html#CyclicBarrier%28int,%20java.lang.Runnable%29) constructor can accept callback that will be triggered every time when barrier is tripped.
* [CyclicBarrier](http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/CyclicBarrier.html#CyclicBarrier%28int,%20java.lang.Runnable%29) constructor can accept a callback that will be triggered every time a barrier is tripped.

Binary file not shown.

Before

Width:  |  Height:  |  Size: 8.4 KiB

After

Width:  |  Height:  |  Size: 18 KiB

View File

@ -8,11 +8,6 @@ package com.iluwatar.callback {
interface Callback {
+ call() {abstract}
}
class LambdasApp {
- LOGGER : Logger {static}
- LambdasApp()
+ main(args : String[]) {static}
}
class SimpleTask {
- LOGGER : Logger {static}
+ SimpleTask()

View File

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

View File

@ -1,26 +0,0 @@
/*
* The MIT License
* Copyright © 2014-2019 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.
*/
module com.iluwatar.callback {
requires org.slf4j;
}

View File

@ -25,12 +25,23 @@ package com.iluwatar.callback;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
/**
* Tests that Callback example runs without errors.
*/
public class AppTest {
class AppTest {
/**
* Issue: Add at least one assertion to this test case.
*
* Solution: Inserted assertion to check whether the execution of the main method in {@link App}
* throws an exception.
*/
@Test
public void test() {
App.main(new String[]{});
void shouldExecuteApplicationWithoutException() {
assertDoesNotThrow(() -> App.main(new String[]{}));
}
}

View File

@ -9,27 +9,32 @@ tags:
---
## Intent
Avoid coupling the sender of a request to its receiver by giving
more than one object a chance to handle the request. Chain the receiving
objects and pass the request along the chain until an object handles it.
Avoid coupling the sender of a request to its receiver by giving more than one object a chance to
handle the request. Chain the receiving objects and pass the request along the chain until an object
handles it.
## Explanation
Real world example
> The Orc King gives loud orders to his army. The closest one to react is the commander, then officer and then soldier. The commander, officer and soldier here form a chain of responsibility.
> The Orc King gives loud orders to his army. The closest one to react is the commander, then
> officer and then soldier. The commander, officer and soldier here form a chain of responsibility.
In plain words
> It helps building a chain of objects. Request enters from one end and keeps going from object to object till it finds the suitable handler.
> It helps to build a chain of objects. A request enters from one end and keeps going from an object
> to another until it finds a suitable handler.
Wikipedia says
> In object-oriented design, the chain-of-responsibility pattern is a design pattern consisting of a source of command objects and a series of processing objects. Each processing object contains logic that defines the types of command objects that it can handle; the rest are passed to the next processing object in the chain.
> In object-oriented design, the chain-of-responsibility pattern is a design pattern consisting of
> a source of command objects and a series of processing objects. Each processing object contains
> logic that defines the types of command objects that it can handle; the rest are passed to the
> next processing object in the chain.
**Programmatic Example**
Translating our example with orcs from above. First we have the request class
Translating our example with the orcs from above. First we have the `Request` class:
```java
public class Request {
@ -140,14 +145,16 @@ king.makeRequest(new Request(RequestType.COLLECT_TAX, "collect tax")); // Orc so
```
## Class diagram
![alt text](./etc/chain.urm.png "Chain of Responsibility class diagram")
## Applicability
Use Chain of Responsibility when
* more than one object may handle a request, and the handler isn't known a priori. The handler should be ascertained automatically
* you want to issue a request to one of several objects without specifying the receiver explicitly
* the set of objects that can handle a request should be specified dynamically
* More than one object may handle a request, and the handler isn't known a priori. The handler should be ascertained automatically.
* You want to issue a request to one of several objects without specifying the receiver explicitly.
* The set of objects that can handle a request should be specified dynamically.
## Real world examples

View File

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

View File

@ -1,26 +0,0 @@
/*
* The MIT License
* Copyright © 2014-2019 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.
*/
module com.iluwatar.chain {
requires org.slf4j;
}

View File

@ -25,13 +25,23 @@ package com.iluwatar.chain;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
/**
* Application test
*/
public class AppTest {
class AppTest {
/**
* Issue: Add at least one assertion to this test case.
*
* Solution: Inserted assertion to check whether the execution of the main method in {@link App}
* throws an exception.
*/
@Test
public void test() {
App.main(new String[]{});
void shouldExecuteApplicationWithoutException() {
assertDoesNotThrow(() -> App.main(new String[]{}));
}
}

View File

@ -12,32 +12,43 @@ tags:
## Intent
Handle costly remote *procedure/service* calls in such a way that the failure of a **single** service/component cannot bring the whole application down, and we can reconnect to the service as soon as possible.
Handle costly remote service calls in such a way that the failure of a single service/component
cannot bring the whole application down, and we can reconnect to the service as soon as possible.
## Explanation
Real world example
> Imagine a Web App that has both local (example: files and images) and remote (example: database entries) to serve. The database might not be responding due to a variety of reasons, so if the application keeps trying to read from the database using multiple threads/processes, soon all of them will hang and our entire web application will crash. We should be able to detect this situation and show the user an appropriate message so that he/she can explore other parts of the app unaffected by the database failure without any problem.
> Imagine a web application that has both local files/images and remote database entries to serve.
> The database might not be responding due to a variety of reasons, so if the application keeps
> trying to read from the database using multiple threads/processes, soon all of them will hang
> causing our entire web application will crash. We should be able to detect this situation and show
> the user an appropriate message so that he/she can explore other parts of the app unaffected by
> the database failure.
In plain words
> Allows us to save resources when we know a remote service failed. Useful when all parts of our application are highly decoupled from each other, and failure of one component doesn't mean the other parts will stop working.
> Circuit Breaker allows graceful handling of failed remote services. It's especially useful when
> all parts of our application are highly decoupled from each other, and failure of one component
> doesn't mean the other parts will stop working.
Wikipedia says
> **Circuit breaker** is a design pattern used in modern software development. It is used to detect failures and encapsulates the logic of preventing a failure from constantly recurring, during maintenance, temporary external system failure or unexpected system difficulties.
So, how does this all come together?
> Circuit breaker is a design pattern used in modern software development. It is used to detect
> failures and encapsulates the logic of preventing a failure from constantly recurring, during
> maintenance, temporary external system failure or unexpected system difficulties.
## Programmatic Example
With the above example in mind we will imitate the functionality in a simple manner. We have two services: A *monitoring service* which will mimic the web app and will make both **local** and **remote** calls.
So, how does this all come together? With the above example in mind we will imitate the
functionality in a simple example. A monitoring service mimics the web app and makes both local and
remote calls.
The service architecture is as follows:
![alt text](./etc/ServiceDiagram.PNG "Service Diagram")
In terms of code, the End user application is:
In terms of code, the end user application is:
```java
public class App {
@ -62,7 +73,7 @@ public class App {
}
```
The monitoring service is:
The monitoring service:
``` java
public class MonitoringService {
@ -80,7 +91,8 @@ public class MonitoringService {
}
}
```
As it can be seen, it does the call to get local resources directly, but it wraps the call to remote (costly) service in a circuit breaker object, which prevents faults as follows:
As it can be seen, it does the call to get local resources directly, but it wraps the call to
remote (costly) service in a circuit breaker object, which prevents faults as follows:
```java
public class CircuitBreaker {
@ -155,24 +167,27 @@ public class CircuitBreaker {
}
```
How does the above pattern prevent failures? Let's understand via this finite state machine implemented by it.
How does the above pattern prevent failures? Let's understand via this finite state machine
implemented by it.
![alt text](./etc/StateDiagram.PNG "State Diagram")
- We initialize the Circuit Breaker object with certain parameters: **timeout**, **failureThreshold** and **retryTimePeriod** which help determine how resilient the API is.
- Initially, we are in the **closed** state and the remote call to API happens.
- We initialize the Circuit Breaker object with certain parameters: `timeout`, `failureThreshold` and `retryTimePeriod` which help determine how resilient the API is.
- Initially, we are in the `closed` state and nos remote calls to the API have occurred.
- Every time the call succeeds, we reset the state to as it was in the beginning.
- If the number of failures cross a certain threshold, we move to the **open** state, which acts just like an open circuit and prevents remote service calls from being made, thus saving resources. (Here, we return the response called ```stale response from API```)
- Once we exceed the retry timeout period, we move to the **half-open** state and make another call to the remote service again to check if the service is working so that we can serve fresh content. A *failure* sets it back to **open** state and another attempt is made after retry timeout period, while a *success* sets it to **closed** state so that everything starts working normally again.
- If the number of failures cross a certain threshold, we move to the `open` state, which acts just like an open circuit and prevents remote service calls from being made, thus saving resources. (Here, we return the response called ```stale response from API```)
- Once we exceed the retry timeout period, we move to the `half-open` state and make another call to the remote service again to check if the service is working so that we can serve fresh content. A failure sets it back to `open` state and another attempt is made after retry timeout period, while a success sets it to `closed` state so that everything starts working normally again.
## Class diagram
![alt text](./etc/circuit-breaker.urm.png "Circuit Breaker class diagram")
## Applicability
Use the Circuit Breaker pattern when
- Building a fault-tolerant application where failure of some services shouldn't bring the entire application down.
- Building an continuously incremental/continuous delivery application, as some of it's components can be upgraded without shutting it down entirely.
- Building a continuously running (always-on) application, so that its components can be upgraded without shutting it down entirely.
## Related Patterns

View File

@ -27,7 +27,7 @@
<parent>
<groupId>com.iluwatar</groupId>
<artifactId>java-design-patterns</artifactId>
<version>1.23.0-SNAPSHOT</version>
<version>1.24.0-SNAPSHOT</version>
</parent>
<artifactId>circuit-breaker</artifactId>
<dependencies>

View File

@ -27,7 +27,7 @@
<parent>
<groupId>com.iluwatar</groupId>
<artifactId>java-design-patterns</artifactId>
<version>1.23.0-SNAPSHOT</version>
<version>1.24.0-SNAPSHOT</version>
</parent>
<artifactId>collection-pipeline</artifactId>
<dependencies>

View File

@ -1,26 +0,0 @@
/*
* The MIT License
* Copyright © 2014-2019 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.
*/
module com.iluwatar.collectionpipeline {
requires org.slf4j;
}

View File

@ -29,7 +29,7 @@
<parent>
<groupId>com.iluwatar</groupId>
<artifactId>java-design-patterns</artifactId>
<version>1.23.0-SNAPSHOT</version>
<version>1.24.0-SNAPSHOT</version>
</parent>
<artifactId>combinator</artifactId>
@ -39,6 +39,12 @@
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@ -25,12 +25,19 @@ package com.iluwatar.combinator;
import org.junit.Test;
import static org.junit.Assert.*;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
public class CombinatorAppTest {
/**
* Issue: Add at least one assertion to this test case.
*
* Solution: Inserted assertion to check whether the execution of the main method in {@link CombinatorApp#main(String[])}
* throws an exception.
*/
@Test
public void main() {
CombinatorApp.main(new String[]{});
public void shouldExecuteApplicationWithoutException() {
assertDoesNotThrow(() -> CombinatorApp.main(new String[]{}));
}
}

View File

@ -9,15 +9,20 @@ tags:
---
## Also known as
Action, Transaction
## Intent
Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.
Encapsulate a request as an object, thereby letting you parameterize clients with different
requests, queue or log requests, and support undoable operations.
## Explanation
Real world example
> There is a wizard casting spells on a goblin. The spells are executed on the goblin one by one. The first spell shrinks the goblin and the second makes him invisible. Then the wizard reverses the spells one by one. Each spell here is a command object that can be undone.
> There is a wizard casting spells on a goblin. The spells are executed on the goblin one by one.
> The first spell shrinks the goblin and the second makes him invisible. Then the wizard reverses
> the spells one by one. Each spell here is a command object that can be undone.
In plain words
@ -25,11 +30,13 @@ In plain words
Wikipedia says
> In object-oriented programming, the command pattern is a behavioral design pattern in which an object is used to encapsulate all information needed to perform an action or trigger an event at a later time.
> In object-oriented programming, the command pattern is a behavioral design pattern in which an
> object is used to encapsulate all information needed to perform an action or trigger an event at
> a later time.
**Programmatic Example**
Here's the sample code with wizard and goblin. Let's start from the wizard class.
Here's the sample code with wizard and goblin. Let's start from the `Wizard` class.
```java
public class Wizard {
@ -75,19 +82,18 @@ public class Wizard {
Next we present the spell hierarchy.
```java
public abstract class Command {
public interface Command {
public abstract void execute(Target target);
void execute(Target target);
public abstract void undo();
void undo();
public abstract void redo();
void redo();
@Override
public abstract String toString();
String toString();
}
public class InvisibilitySpell extends Command {
public class InvisibilitySpell implements Command {
private Target target;
@ -117,7 +123,7 @@ public class InvisibilitySpell extends Command {
}
}
public class ShrinkSpell extends Command {
public class ShrinkSpell implements Command {
private Size oldSize;
private Target target;
@ -150,7 +156,7 @@ public class ShrinkSpell extends Command {
}
```
And last we have the goblin who's the target of the spells.
Finally, we have the goblin who's the target of the spells.
```java
public abstract class Target {
@ -200,44 +206,67 @@ public class Goblin extends Target {
}
```
Finally here's the whole example in action.
Here's the whole example in action.
```java
var wizard = new Wizard();
var goblin = new Goblin();
goblin.printStatus();
// Goblin, [size=normal] [visibility=visible]
wizard.castSpell(new ShrinkSpell(), goblin);
// Wizard casts Shrink spell at Goblin
goblin.printStatus();
// Goblin, [size=small] [visibility=visible]
wizard.castSpell(new InvisibilitySpell(), goblin);
// Wizard casts Invisibility spell at Goblin
goblin.printStatus();
// Goblin, [size=small] [visibility=invisible]
wizard.undoLastSpell();
// Wizard undoes Invisibility spell
goblin.printStatus();
```
Here's the program output:
```java
// Goblin, [size=normal] [visibility=visible]
// Wizard casts Shrink spell at Goblin
// Goblin, [size=small] [visibility=visible]
// Wizard casts Invisibility spell at Goblin
// Goblin, [size=small] [visibility=invisible]
// Wizard undoes Invisibility spell
// Goblin, [size=small] [visibility=visible]
```
## Class diagram
![alt text](./etc/command.png "Command")
## Applicability
Use the Command pattern when you want to
* parameterize objects by an action to perform. You can express such parameterization in a procedural language with a callback function, that is, a function that's registered somewhere to be called at a later point. Commands are an object-oriented replacement for callbacks.
* specify, queue, and execute requests at different times. A Command object can have a lifetime independent of the original request. If the receiver of a request can be represented in an address space-independent way, then you can transfer a command object for the request to a different process and fulfill the request there
* support undo. The Command's execute operation can store state for reversing its effects in the command itself. The Command interface must have an added Unexecute operation that reverses the effects of a previous call to execute. Executed commands are stored in a history list. Unlimited-level undo and redo is achieved by traversing this list backwards and forwards calling unexecute and execute, respectively
* support logging changes so that they can be reapplied in case of a system crash. By augmenting the Command interface with load and store operations, you can keep a persistent log of changes. Recovering from a crash involves reloading logged commands from disk and re-executing them with the execute operation
* structure a system around high-level operations build on primitive operations. Such a structure is common in information systems that support transactions. A transaction encapsulates a set of changes to data. The Command pattern offers a way to model transactions. Commands have a common interface, letting you invoke all transactions the same way. The pattern also makes it easy to extend the system with new transactions
Use the Command pattern when you want to:
* Parameterize objects by an action to perform. You can express such parameterization in a
procedural language with a callback function, that is, a function that's registered somewhere to be
called at a later point. Commands are an object-oriented replacement for callbacks.
* Specify, queue, and execute requests at different times. A Command object can have a lifetime
independent of the original request. If the receiver of a request can be represented in an address
space-independent way, then you can transfer a command object for the request to a different process
and fulfill the request there.
* Support undo. The Command's execute operation can store state for reversing its effects in the
command itself. The Command interface must have an added un-execute operation that reverses the
effects of a previous call to execute. The executed commands are stored in a history list.
Unlimited-level undo and redo is achieved by traversing this list backwards and forwards calling
un-execute and execute, respectively.
* Support logging changes so that they can be reapplied in case of a system crash. By augmenting the
Command interface with load and store operations, you can keep a persistent log of changes.
Recovering from a crash involves reloading logged commands from disk and re-executing them with
the execute operation.
* Structure a system around high-level operations build on primitive operations. Such a structure is
common in information systems that support transactions. A transaction encapsulates a set of changes
to data. The Command pattern offers a way to model transactions. Commands have a common interface,
letting you invoke all transactions the same way. The pattern also makes it easy to extend the
system with new transactions.
## Typical Use Case
* to keep a history of requests
* implement callback functionality
* implement the undo functionality
* To keep a history of requests
* Implement callback functionality
* Implement the undo functionality
## Real world examples

Binary file not shown.

Before

Width:  |  Height:  |  Size: 27 KiB

After

Width:  |  Height:  |  Size: 75 KiB

View File

@ -4,7 +4,7 @@ package com.iluwatar.command {
+ App()
+ main(args : String[]) {static}
}
abstract class Command {
interface Command {
+ Command()
+ execute(Target) {abstract}
+ redo() {abstract}
@ -77,7 +77,7 @@ ShrinkSpell --> "-oldSize" Size
InvisibilitySpell --> "-target" Target
ShrinkSpell --> "-target" Target
Target --> "-visibility" Visibility
Goblin --|> Target
InvisibilitySpell --|> Command
ShrinkSpell --|> Command
@enduml
Goblin --|> Target
InvisibilitySpell ..|> Command
ShrinkSpell ..|> Command
@enduml

View File

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

View File

@ -26,15 +26,12 @@ package com.iluwatar.command;
/**
* Interface for Commands.
*/
public abstract class Command {
public interface Command {
void execute(Target target);
public abstract void execute(Target target);
void undo();
public abstract void undo();
public abstract void redo();
@Override
public abstract String toString();
void redo();
String toString();
}

View File

@ -26,7 +26,7 @@ package com.iluwatar.command;
/**
* InvisibilitySpell is a concrete command.
*/
public class InvisibilitySpell extends Command {
public class InvisibilitySpell implements Command {
private Target target;

View File

@ -26,7 +26,7 @@ package com.iluwatar.command;
/**
* ShrinkSpell is a concrete command.
*/
public class ShrinkSpell extends Command {
public class ShrinkSpell implements Command {
private Size oldSize;
private Target target;

View File

@ -1,26 +0,0 @@
/*
* The MIT License
* Copyright © 2014-2019 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.
*/
module com.iluwatar.command {
requires org.slf4j;
}

View File

@ -25,12 +25,21 @@ package com.iluwatar.command;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
/**
* Tests that Command example runs without errors.
*/
public class AppTest {
class AppTest {
/**
* Issue: Add at least one assertion to this test case.
*
* Solution: Inserted assertion to check whether the execution of the main method in {@link App#main(String[])}
* throws an exception.
*/
@Test
public void test() {
App.main(new String[]{});
void shouldExecuteApplicationWithoutException() {
assertDoesNotThrow(() -> App.main(new String[]{}));
}
}

View File

@ -27,7 +27,7 @@
<parent>
<groupId>com.iluwatar</groupId>
<artifactId>java-design-patterns</artifactId>
<version>1.23.0-SNAPSHOT</version>
<version>1.24.0-SNAPSHOT</version>
</parent>
<artifactId>commander</artifactId>
<dependencies>

View File

@ -36,9 +36,11 @@ import com.iluwatar.commander.queue.QueueDatabase;
import com.iluwatar.commander.queue.QueueTask;
import com.iluwatar.commander.queue.QueueTask.TaskType;
import com.iluwatar.commander.shippingservice.ShippingService;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* <p>Commander pattern is used to handle all issues that can come up while making a
* distributed transaction. The idea is to have a commander, which coordinates the execution of all
@ -159,8 +161,8 @@ public class Commander {
private void sendPaymentRequest(Order order) {
if (System.currentTimeMillis() - order.createdTime >= this.paymentTime) {
if (order.paid.equals(PaymentStatus.Trying)) {
order.paid = PaymentStatus.NotDone;
if (order.paid.equals(PaymentStatus.TRYING)) {
order.paid = PaymentStatus.NOT_DONE;
sendPaymentFailureMessage(order);
LOG.error("Order " + order.id + ": Payment time for order over, failed and returning..");
} //if succeeded or failed, would have been dequeued, no attempt to make payment
@ -172,15 +174,15 @@ public class Commander {
if (!l.isEmpty()) {
if (DatabaseUnavailableException.class.isAssignableFrom(l.get(0).getClass())) {
LOG.debug("Order " + order.id + ": Error in connecting to payment service,"
+ " trying again..");
+ " trying again..");
} else {
LOG.debug("Order " + order.id + ": Error in creating payment request..");
}
throw l.remove(0);
}
if (order.paid.equals(PaymentStatus.Trying)) {
if (order.paid.equals(PaymentStatus.TRYING)) {
var transactionId = paymentService.receiveRequest(order.price);
order.paid = PaymentStatus.Done;
order.paid = PaymentStatus.DONE;
LOG.info("Order " + order.id + ": Payment successful, transaction Id: " + transactionId);
if (!finalSiteMsgShown) {
LOG.info("Payment made successfully, thank you for shopping with us!!");
@ -193,26 +195,26 @@ public class Commander {
if (PaymentDetailsErrorException.class.isAssignableFrom(err.getClass())) {
if (!finalSiteMsgShown) {
LOG.info("There was an error in payment. Your account/card details "
+ "may have been incorrect. "
+ "Meanwhile, your order has been converted to COD and will be shipped.");
+ "may have been incorrect. "
+ "Meanwhile, your order has been converted to COD and will be shipped.");
finalSiteMsgShown = true;
}
LOG.error("Order " + order.id + ": Payment details incorrect, failed..");
o.paid = PaymentStatus.NotDone;
o.paid = PaymentStatus.NOT_DONE;
sendPaymentFailureMessage(o);
} else {
if (o.messageSent.equals(MessageSent.NoneSent)) {
if (o.messageSent.equals(MessageSent.NONE_SENT)) {
if (!finalSiteMsgShown) {
LOG.info("There was an error in payment. We are on it, and will get back to you "
+ "asap. Don't worry, your order has been placed and will be shipped.");
+ "asap. Don't worry, your order has been placed and will be shipped.");
finalSiteMsgShown = true;
}
LOG.warn("Order " + order.id + ": Payment error, going to queue..");
sendPaymentPossibleErrorMsg(o);
}
if (o.paid.equals(PaymentStatus.Trying) && System
.currentTimeMillis() - o.createdTime < paymentTime) {
var qt = new QueueTask(o, TaskType.Payment, -1);
if (o.paid.equals(PaymentStatus.TRYING) && System
.currentTimeMillis() - o.createdTime < paymentTime) {
var qt = new QueueTask(o, TaskType.PAYMENT, -1);
updateQueue(qt);
}
}
@ -234,12 +236,12 @@ public class Commander {
// additional check not needed
LOG.trace("Order " + qt.order.id + ": Queue time for order over, failed..");
return;
} else if (qt.taskType.equals(TaskType.Payment) && !qt.order.paid.equals(PaymentStatus.Trying)
|| qt.taskType.equals(TaskType.Messaging) && (qt.messageType == 1
&& !qt.order.messageSent.equals(MessageSent.NoneSent)
|| qt.order.messageSent.equals(MessageSent.PaymentFail)
|| qt.order.messageSent.equals(MessageSent.PaymentSuccessful))
|| qt.taskType.equals(TaskType.EmployeeDb) && qt.order.addedToEmployeeHandle) {
} else if (qt.taskType.equals(TaskType.PAYMENT) && !qt.order.paid.equals(PaymentStatus.TRYING)
|| qt.taskType.equals(TaskType.MESSAGING) && (qt.messageType == 1
&& !qt.order.messageSent.equals(MessageSent.NONE_SENT)
|| qt.order.messageSent.equals(MessageSent.PAYMENT_FAIL)
|| qt.order.messageSent.equals(MessageSent.PAYMENT_SUCCESSFUL))
|| qt.taskType.equals(TaskType.EMPLOYEE_DB) && qt.order.addedToEmployeeHandle) {
LOG.trace("Order " + qt.order.id + ": Not queueing task since task already done..");
return;
}
@ -256,8 +258,8 @@ public class Commander {
tryDoingTasksInQueue();
};
Retry.HandleErrorIssue<QueueTask> handleError = (qt1, err) -> {
if (qt1.taskType.equals(TaskType.Payment)) {
qt1.order.paid = PaymentStatus.NotDone;
if (qt1.taskType.equals(TaskType.PAYMENT)) {
qt1.order.paid = PaymentStatus.NOT_DONE;
sendPaymentFailureMessage(qt1.order);
LOG.error("Order " + qt1.order.id + ": Unable to enqueue payment task,"
+ " payment failed..");
@ -331,35 +333,9 @@ public class Commander {
}
var list = messagingService.exceptionsList;
Thread t = new Thread(() -> {
Retry.Operation op = (l) -> {
if (!l.isEmpty()) {
if (DatabaseUnavailableException.class.isAssignableFrom(l.get(0).getClass())) {
LOG.debug("Order " + order.id + ": Error in connecting to messaging service "
+ "(Payment Success msg), trying again..");
} else {
LOG.debug("Order " + order.id + ": Error in creating Payment Success"
+ " messaging request..");
}
throw l.remove(0);
}
if (!order.messageSent.equals(MessageSent.PaymentFail)
&& !order.messageSent.equals(MessageSent.PaymentSuccessful)) {
var requestId = messagingService.receiveRequest(2);
order.messageSent = MessageSent.PaymentSuccessful;
LOG.info("Order " + order.id + ": Payment Success message sent,"
+ " request Id: " + requestId);
}
};
Retry.Operation op = handleSuccessMessageRetryOperation(order);
Retry.HandleErrorIssue<Order> handleError = (o, err) -> {
if ((o.messageSent.equals(MessageSent.NoneSent) || o.messageSent
.equals(MessageSent.PaymentTrying))
&& System.currentTimeMillis() - o.createdTime < messageTime) {
var qt = new QueueTask(order, TaskType.Messaging, 2);
updateQueue(qt);
LOG.info("Order " + order.id + ": Error in sending Payment Success message, trying to"
+ " queue task and add to employee handle..");
employeeHandleIssue(order);
}
handleSuccessMessageErrorIssue(order, o);
};
var r = new Retry<>(op, handleError, numOfRetries, retryDuration,
e -> DatabaseUnavailableException.class.isAssignableFrom(e.getClass()));
@ -372,6 +348,40 @@ public class Commander {
t.start();
}
private void handleSuccessMessageErrorIssue(Order order, Order o) {
if ((o.messageSent.equals(MessageSent.NONE_SENT) || o.messageSent
.equals(MessageSent.PAYMENT_TRYING))
&& System.currentTimeMillis() - o.createdTime < messageTime) {
var qt = new QueueTask(order, TaskType.MESSAGING, 2);
updateQueue(qt);
LOG.info("Order " + order.id + ": Error in sending Payment Success message, trying to"
+ " queue task and add to employee handle..");
employeeHandleIssue(order);
}
}
private Retry.Operation handleSuccessMessageRetryOperation(Order order) {
return (l) -> {
if (!l.isEmpty()) {
if (DatabaseUnavailableException.class.isAssignableFrom(l.get(0).getClass())) {
LOG.debug("Order " + order.id + ": Error in connecting to messaging service "
+ "(Payment Success msg), trying again..");
} else {
LOG.debug("Order " + order.id + ": Error in creating Payment Success"
+ " messaging request..");
}
throw l.remove(0);
}
if (!order.messageSent.equals(MessageSent.PAYMENT_FAIL)
&& !order.messageSent.equals(MessageSent.PAYMENT_SUCCESSFUL)) {
var requestId = messagingService.receiveRequest(2);
order.messageSent = MessageSent.PAYMENT_SUCCESSFUL;
LOG.info("Order " + order.id + ": Payment Success message sent,"
+ " request Id: " + requestId);
}
};
}
private void sendPaymentFailureMessage(Order order) {
if (System.currentTimeMillis() - order.createdTime >= this.messageTime) {
LOG.trace("Order " + order.id + ": Message time for order over, returning..");
@ -380,34 +390,10 @@ public class Commander {
var list = messagingService.exceptionsList;
var t = new Thread(() -> {
Retry.Operation op = (l) -> {
if (!l.isEmpty()) {
if (DatabaseUnavailableException.class.isAssignableFrom(l.get(0).getClass())) {
LOG.debug("Order " + order.id + ": Error in connecting to messaging service "
+ "(Payment Failure msg), trying again..");
} else {
LOG.debug("Order " + order.id + ": Error in creating Payment Failure"
+ " message request..");
}
throw l.remove(0);
}
if (!order.messageSent.equals(MessageSent.PaymentFail)
&& !order.messageSent.equals(MessageSent.PaymentSuccessful)) {
var requestId = messagingService.receiveRequest(0);
order.messageSent = MessageSent.PaymentFail;
LOG.info("Order " + order.id + ": Payment Failure message sent successfully,"
+ " request Id: " + requestId);
}
handlePaymentFailureRetryOperation(order, l);
};
Retry.HandleErrorIssue<Order> handleError = (o, err) -> {
if ((o.messageSent.equals(MessageSent.NoneSent) || o.messageSent
.equals(MessageSent.PaymentTrying))
&& System.currentTimeMillis() - o.createdTime < messageTime) {
var qt = new QueueTask(order, TaskType.Messaging, 0);
updateQueue(qt);
LOG.warn("Order " + order.id + ": Error in sending Payment Failure message, "
+ "trying to queue task and add to employee handle..");
employeeHandleIssue(o);
}
handlePaymentErrorIssue(order, o);
};
var r = new Retry<>(op, handleError, numOfRetries, retryDuration,
e -> DatabaseUnavailableException.class.isAssignableFrom(e.getClass()));
@ -420,6 +406,38 @@ public class Commander {
t.start();
}
private void handlePaymentErrorIssue(Order order, Order o) {
if ((o.messageSent.equals(MessageSent.NONE_SENT) || o.messageSent
.equals(MessageSent.PAYMENT_TRYING))
&& System.currentTimeMillis() - o.createdTime < messageTime) {
var qt = new QueueTask(order, TaskType.MESSAGING, 0);
updateQueue(qt);
LOG.warn("Order " + order.id + ": Error in sending Payment Failure message, "
+ "trying to queue task and add to employee handle..");
employeeHandleIssue(o);
}
}
private void handlePaymentFailureRetryOperation(Order order, List<Exception> l) throws Exception {
if (!l.isEmpty()) {
if (DatabaseUnavailableException.class.isAssignableFrom(l.get(0).getClass())) {
LOG.debug("Order " + order.id + ": Error in connecting to messaging service "
+ "(Payment Failure msg), trying again..");
} else {
LOG.debug("Order " + order.id + ": Error in creating Payment Failure"
+ " message request..");
}
throw l.remove(0);
}
if (!order.messageSent.equals(MessageSent.PAYMENT_FAIL)
&& !order.messageSent.equals(MessageSent.PAYMENT_SUCCESSFUL)) {
var requestId = messagingService.receiveRequest(0);
order.messageSent = MessageSent.PAYMENT_FAIL;
LOG.info("Order " + order.id + ": Payment Failure message sent successfully,"
+ " request Id: " + requestId);
}
}
private void sendPaymentPossibleErrorMsg(Order order) {
if (System.currentTimeMillis() - order.createdTime >= this.messageTime) {
LOG.trace("Message time for order over, returning..");
@ -428,34 +446,10 @@ public class Commander {
var list = messagingService.exceptionsList;
var t = new Thread(() -> {
Retry.Operation op = (l) -> {
if (!l.isEmpty()) {
if (DatabaseUnavailableException.class.isAssignableFrom(l.get(0).getClass())) {
LOG.debug("Order " + order.id + ": Error in connecting to messaging service "
+ "(Payment Error msg), trying again..");
} else {
LOG.debug("Order " + order.id + ": Error in creating Payment Error"
+ " messaging request..");
}
throw l.remove(0);
}
if (order.paid.equals(PaymentStatus.Trying) && order.messageSent
.equals(MessageSent.NoneSent)) {
var requestId = messagingService.receiveRequest(1);
order.messageSent = MessageSent.PaymentTrying;
LOG.info("Order " + order.id + ": Payment Error message sent successfully,"
+ " request Id: " + requestId);
}
handlePaymentPossibleErrorMsgRetryOperation(order, l);
};
Retry.HandleErrorIssue<Order> handleError = (o, err) -> {
if (o.messageSent.equals(MessageSent.NoneSent) && order.paid
.equals(PaymentStatus.Trying)
&& System.currentTimeMillis() - o.createdTime < messageTime) {
var qt = new QueueTask(order, TaskType.Messaging, 1);
updateQueue(qt);
LOG.warn("Order " + order.id + ": Error in sending Payment Error message, "
+ "trying to queue task and add to employee handle..");
employeeHandleIssue(o);
}
handlePaymentPossibleErrorMsgErrorIssue(order, o);
};
var r = new Retry<>(op, handleError, numOfRetries, retryDuration,
e -> DatabaseUnavailableException.class.isAssignableFrom(e.getClass()));
@ -468,6 +462,39 @@ public class Commander {
t.start();
}
private void handlePaymentPossibleErrorMsgErrorIssue(Order order, Order o) {
if (o.messageSent.equals(MessageSent.NONE_SENT) && order.paid
.equals(PaymentStatus.TRYING)
&& System.currentTimeMillis() - o.createdTime < messageTime) {
var qt = new QueueTask(order, TaskType.MESSAGING, 1);
updateQueue(qt);
LOG.warn("Order " + order.id + ": Error in sending Payment Error message, "
+ "trying to queue task and add to employee handle..");
employeeHandleIssue(o);
}
}
private void handlePaymentPossibleErrorMsgRetryOperation(Order order, List<Exception> l)
throws Exception {
if (!l.isEmpty()) {
if (DatabaseUnavailableException.class.isAssignableFrom(l.get(0).getClass())) {
LOG.debug("Order " + order.id + ": Error in connecting to messaging service "
+ "(Payment Error msg), trying again..");
} else {
LOG.debug("Order " + order.id + ": Error in creating Payment Error"
+ " messaging request..");
}
throw l.remove(0);
}
if (order.paid.equals(PaymentStatus.TRYING) && order.messageSent
.equals(MessageSent.NONE_SENT)) {
var requestId = messagingService.receiveRequest(1);
order.messageSent = MessageSent.PAYMENT_TRYING;
LOG.info("Order " + order.id + ": Payment Error message sent successfully,"
+ " request Id: " + requestId);
}
}
private void employeeHandleIssue(Order order) {
if (System.currentTimeMillis() - order.createdTime >= this.employeeTime) {
LOG.trace("Order " + order.id + ": Employee handle time for order over, returning..");
@ -490,7 +517,7 @@ public class Commander {
Retry.HandleErrorIssue<Order> handleError = (o, err) -> {
if (!o.addedToEmployeeHandle && System
.currentTimeMillis() - order.createdTime < employeeTime) {
var qt = new QueueTask(order, TaskType.EmployeeDb, -1);
var qt = new QueueTask(order, TaskType.EMPLOYEE_DB, -1);
updateQueue(qt);
LOG.warn("Order " + order.id + ": Error in adding to employee db,"
+ " trying to queue task..");
@ -520,21 +547,21 @@ public class Commander {
LOG.trace("Order " + qt.order.id + ": This queue task of type " + qt.getType()
+ " does not need to be done anymore (timeout), dequeue..");
} else {
if (qt.taskType.equals(TaskType.Payment)) {
if (!qt.order.paid.equals(PaymentStatus.Trying)) {
if (qt.taskType.equals(TaskType.PAYMENT)) {
if (!qt.order.paid.equals(PaymentStatus.TRYING)) {
tryDequeue();
LOG.trace("Order " + qt.order.id + ": This payment task already done, dequeueing..");
} else {
sendPaymentRequest(qt.order);
LOG.debug("Order " + qt.order.id + ": Trying to connect to payment service..");
}
} else if (qt.taskType.equals(TaskType.Messaging)) {
if (qt.order.messageSent.equals(MessageSent.PaymentFail)
|| qt.order.messageSent.equals(MessageSent.PaymentSuccessful)) {
} else if (qt.taskType.equals(TaskType.MESSAGING)) {
if (qt.order.messageSent.equals(MessageSent.PAYMENT_FAIL)
|| qt.order.messageSent.equals(MessageSent.PAYMENT_SUCCESSFUL)) {
tryDequeue();
LOG.trace("Order " + qt.order.id + ": This messaging task already done, dequeue..");
} else if (qt.messageType == 1 && (!qt.order.messageSent.equals(MessageSent.NoneSent)
|| !qt.order.paid.equals(PaymentStatus.Trying))) {
} else if (qt.messageType == 1 && (!qt.order.messageSent.equals(MessageSent.NONE_SENT)
|| !qt.order.paid.equals(PaymentStatus.TRYING))) {
tryDequeue();
LOG.trace("Order " + qt.order.id + ": This messaging task does not need to be done,"
+ " dequeue..");
@ -548,7 +575,7 @@ public class Commander {
sendSuccessMessage(qt.order);
LOG.debug("Order " + qt.order.id + ": Trying to connect to messaging service..");
}
} else if (qt.taskType.equals(TaskType.EmployeeDb)) {
} else if (qt.taskType.equals(TaskType.EMPLOYEE_DB)) {
if (qt.order.addedToEmployeeHandle) {
tryDequeue();
LOG.trace("Order " + qt.order.id + ": This employee handle task already done,"

View File

@ -33,11 +33,11 @@ import java.util.Random;
public class Order { //can store all transactions ids also
enum PaymentStatus {
NotDone, Trying, Done
NOT_DONE, TRYING, DONE
}
enum MessageSent {
NoneSent, PaymentFail, PaymentTrying, PaymentSuccessful
NONE_SENT, PAYMENT_FAIL, PAYMENT_TRYING, PAYMENT_SUCCESSFUL
}
final User user;
@ -65,8 +65,8 @@ public class Order { //can store all transactions ids also
}
this.id = id;
USED_IDS.put(this.id, true);
this.paid = PaymentStatus.Trying;
this.messageSent = MessageSent.NoneSent;
this.paid = PaymentStatus.TRYING;
this.messageSent = MessageSent.NONE_SENT;
this.addedToEmployeeHandle = false;
}

View File

@ -38,7 +38,7 @@ public class MessagingService extends Service {
private static final Logger LOGGER = LoggerFactory.getLogger(MessagingService.class);
enum MessageToSend {
PaymentFail, PaymentTrying, PaymentSuccessful
PAYMENT_FAIL, PAYMENT_TRYING, PAYMENT_SUCCESSFUL
}
class MessageRequest {
@ -63,11 +63,11 @@ public class MessagingService extends Service {
var id = generateId();
MessageToSend msg;
if (messageToSend == 0) {
msg = MessageToSend.PaymentFail;
msg = MessageToSend.PAYMENT_FAIL;
} else if (messageToSend == 1) {
msg = MessageToSend.PaymentTrying;
msg = MessageToSend.PAYMENT_TRYING;
} else { //messageToSend == 2
msg = MessageToSend.PaymentSuccessful;
msg = MessageToSend.PAYMENT_SUCCESSFUL;
}
var req = new MessageRequest(id, msg);
return updateDb(req);
@ -84,10 +84,10 @@ public class MessagingService extends Service {
}
String sendMessage(MessageToSend m) {
if (m.equals(MessageToSend.PaymentSuccessful)) {
if (m.equals(MessageToSend.PAYMENT_SUCCESSFUL)) {
return "Msg: Your order has been placed and paid for successfully!"
+ " Thank you for shopping with us!";
} else if (m.equals(MessageToSend.PaymentTrying)) {
} else if (m.equals(MessageToSend.PAYMENT_TRYING)) {
return "Msg: There was an error in your payment process,"
+ " we are working on it and will return back to you shortly."
+ " Meanwhile, your order has been placed and will be shipped.";

View File

@ -36,7 +36,7 @@ public class QueueTask {
*/
public enum TaskType {
Messaging, Payment, EmployeeDb
MESSAGING, PAYMENT, EMPLOYEE_DB
}
public Order order;
@ -68,7 +68,7 @@ public class QueueTask {
* @return String representing type of task
*/
public String getType() {
if (!this.taskType.equals(TaskType.Messaging)) {
if (!this.taskType.equals(TaskType.MESSAGING)) {
return this.taskType.toString();
} else {
if (this.messageType == 0) {

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