Compare commits
40 Commits
caching-pa
...
all-contri
Author | SHA1 | Date | |
---|---|---|---|
5bcb010931 | |||
fb45e5c731 | |||
6cf025447a | |||
dafe02f1be | |||
784cdee819 | |||
5e434b783e | |||
f597fc1b07 | |||
1b880c1818 | |||
e498c25675 | |||
122e6edb38 | |||
ea3c9d955e | |||
f1feb3f6a0 | |||
1388e38744 | |||
1dd26289e5 | |||
241a7ad9a2 | |||
32b33480dd | |||
068fa0371e | |||
b5aaa94794 | |||
825b5a9a29 | |||
31890f67e6 | |||
41b1462eed | |||
276549d156 | |||
d67b625a74 | |||
1b14ebcbb1 | |||
a471ce25da | |||
74caa0c4e5 | |||
53a294fee5 | |||
a5062908c0 | |||
076310bb79 | |||
470d29e715 | |||
74802e83b5 | |||
09b577f634 | |||
2fce2e44e2 | |||
b3a1749bd0 | |||
af0ccdc6e1 | |||
323dd63e66 | |||
be3250bd0d | |||
1222f12b99 | |||
965d38f139 | |||
eb8f9db575 |
@ -1132,7 +1132,8 @@
|
||||
"profile": "http://subho.xyz",
|
||||
"contributions": [
|
||||
"code",
|
||||
"review"
|
||||
"review",
|
||||
"maintenance"
|
||||
]
|
||||
},
|
||||
{
|
||||
@ -1450,6 +1451,96 @@
|
||||
"review",
|
||||
"code"
|
||||
]
|
||||
},
|
||||
{
|
||||
"login": "eas5",
|
||||
"name": "Elvys Soares",
|
||||
"avatar_url": "https://avatars.githubusercontent.com/u/50836521?v=4",
|
||||
"profile": "https://github.com/eas5",
|
||||
"contributions": [
|
||||
"code"
|
||||
]
|
||||
},
|
||||
{
|
||||
"login": "zWeBrain",
|
||||
"name": "zWeBrain",
|
||||
"avatar_url": "https://avatars.githubusercontent.com/u/46642512?v=4",
|
||||
"profile": "https://github.com/zWeBrain",
|
||||
"contributions": [
|
||||
"code"
|
||||
]
|
||||
},
|
||||
{
|
||||
"login": "Al-assad",
|
||||
"name": "余林颖",
|
||||
"avatar_url": "https://avatars.githubusercontent.com/u/22493821?v=4",
|
||||
"profile": "https://al-assad.github.io/notion/",
|
||||
"contributions": [
|
||||
"translation"
|
||||
]
|
||||
},
|
||||
{
|
||||
"login": "STudio26",
|
||||
"name": "Alain",
|
||||
"avatar_url": "https://avatars.githubusercontent.com/u/6988911?v=4",
|
||||
"profile": "https://github.com/STudio26",
|
||||
"contributions": [
|
||||
"translation"
|
||||
]
|
||||
},
|
||||
{
|
||||
"login": "DEV-VRUPER",
|
||||
"name": "VR",
|
||||
"avatar_url": "https://avatars.githubusercontent.com/u/30525467?v=4",
|
||||
"profile": "https://github.com/DEV-VRUPER",
|
||||
"contributions": [
|
||||
"doc"
|
||||
]
|
||||
},
|
||||
{
|
||||
"login": "JackieNim",
|
||||
"name": "JackieNim",
|
||||
"avatar_url": "https://avatars.githubusercontent.com/u/4138836?v=4",
|
||||
"profile": "https://github.com/JackieNim",
|
||||
"contributions": [
|
||||
"code"
|
||||
]
|
||||
},
|
||||
{
|
||||
"login": "EdisonE3",
|
||||
"name": "EdisonE3",
|
||||
"avatar_url": "https://avatars.githubusercontent.com/u/52118917?v=4",
|
||||
"profile": "https://github.com/EdisonE3",
|
||||
"contributions": [
|
||||
"code"
|
||||
]
|
||||
},
|
||||
{
|
||||
"login": "tao-sun2",
|
||||
"name": "Tao",
|
||||
"avatar_url": "https://avatars.githubusercontent.com/u/66189688?v=4",
|
||||
"profile": "https://github.com/tao-sun2",
|
||||
"contributions": [
|
||||
"code"
|
||||
]
|
||||
},
|
||||
{
|
||||
"login": "JuanManuelAbate",
|
||||
"name": "Juan Manuel Abate",
|
||||
"avatar_url": "https://avatars.githubusercontent.com/u/16357060?v=4",
|
||||
"profile": "https://github.com/JuanManuelAbate",
|
||||
"contributions": [
|
||||
"translation"
|
||||
]
|
||||
},
|
||||
{
|
||||
"login": "Xenilo137",
|
||||
"name": "Xenilo137",
|
||||
"avatar_url": "https://avatars.githubusercontent.com/u/24865069?v=4",
|
||||
"profile": "https://github.com/Xenilo137",
|
||||
"contributions": [
|
||||
"code"
|
||||
]
|
||||
}
|
||||
],
|
||||
"contributorsPerLine": 4,
|
||||
|
9
.github/workflows/maven-ci.yml
vendored
9
.github/workflows/maven-ci.yml
vendored
@ -40,25 +40,26 @@ jobs:
|
||||
steps:
|
||||
|
||||
- name: Checkout Code
|
||||
uses: actions/checkout@master
|
||||
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@master
|
||||
uses: actions/setup-java@v2
|
||||
with:
|
||||
java-version: 11
|
||||
distribution: 'adopt'
|
||||
|
||||
- name: Cache SonarCloud packages
|
||||
uses: actions/cache@master
|
||||
uses: actions/cache@v2
|
||||
with:
|
||||
path: ~/.sonar/cache
|
||||
key: ${{ runner.os }}-sonar
|
||||
restore-keys: ${{ runner.os }}-sonar
|
||||
|
||||
- name: Cache Maven dependencies
|
||||
uses: actions/cache@master
|
||||
uses: actions/cache@v2
|
||||
with:
|
||||
path: ~/.m2/repository
|
||||
key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }}
|
||||
|
7
.github/workflows/maven-pr-builder.yml
vendored
7
.github/workflows/maven-pr-builder.yml
vendored
@ -38,15 +38,16 @@ jobs:
|
||||
|
||||
steps:
|
||||
- name: Checkout Code
|
||||
uses: actions/checkout@master
|
||||
uses: actions/checkout@v2
|
||||
|
||||
- name: Set up JDK 11
|
||||
uses: actions/setup-java@master
|
||||
uses: actions/setup-java@v2
|
||||
with:
|
||||
java-version: 11
|
||||
distribution: 'adopt'
|
||||
|
||||
- name: Cache Maven Dependecies
|
||||
uses: actions/cache@master
|
||||
uses: actions/cache@v2
|
||||
with:
|
||||
path: ~/.m2/repository
|
||||
key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }}
|
||||
|
32
README.md
32
README.md
@ -6,16 +6,16 @@
|
||||
|
||||

|
||||
[](https://raw.githubusercontent.com/iluwatar/java-design-patterns/master/LICENSE.md)
|
||||
[](https://sonarcloud.io/dashboard?id=iluwatar_java-design-patterns)
|
||||
[](https://sonarcloud.io/dashboard?id=iluwatar_java-design-patterns)
|
||||
[](https://sonarcloud.io/dashboard?id=iluwatar_java-design-patterns)
|
||||
[](https://gitter.im/iluwatar/java-design-patterns?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
|
||||
<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
|
||||
[](#contributors-)
|
||||
[](#contributors-)
|
||||
<!-- ALL-CONTRIBUTORS-BADGE:END -->
|
||||
|
||||
<br/>
|
||||
|
||||
Read in different language : [**CN**](/zh/README.md), [**KR**](/ko/README.md), [**FR**](/fr/README.md), [**TR**](/tr/README.md), [**AR**](/ar/README.md)
|
||||
Read in different language : [**zh**](/localization/zh/README.md), [**ko**](/localization/ko/README.md), [**fr**](/localization/fr/README.md), [**tr**](/localization/tr/README.md), [**ar**](/localization/ar/README.md), [**es**](/localization/es/README.md)
|
||||
|
||||
<br/>
|
||||
|
||||
@ -34,7 +34,7 @@ are familiar with the patterns.
|
||||
# Getting started
|
||||
|
||||
This site showcases Java Design Patterns. The solutions have been developed by
|
||||
experienced programmers and architects from the open source community. The
|
||||
experienced programmers and architects from the open source community. The
|
||||
patterns can be browsed by their high level descriptions or by looking at their
|
||||
source code. The source code examples are well commented and can be thought as
|
||||
programming tutorials on how to implement a specific pattern. We use the most
|
||||
@ -49,7 +49,7 @@ patterns should only be introduced when they are needed for practical
|
||||
extensibility.
|
||||
|
||||
Once you are familiar with these concepts you can start drilling down into the
|
||||
[available design patterns](https://java-design-patterns.com/patterns/) by any
|
||||
[available design patterns](https://java-design-patterns.com/patterns/) by any
|
||||
of the following approaches
|
||||
|
||||
- Search for a specific pattern by name. Can't find one? Please report a new pattern [here](https://github.com/iluwatar/java-design-patterns/issues).
|
||||
@ -61,8 +61,8 @@ in your architectures and have as much fun learning them as we had developing th
|
||||
|
||||
# How to contribute
|
||||
|
||||
If you are willing to contribute to the project you will find the relevant information in
|
||||
our [developer wiki](https://github.com/iluwatar/java-design-patterns/wiki). We will help
|
||||
If you are willing to contribute to the project you will find the relevant information in
|
||||
our [developer wiki](https://github.com/iluwatar/java-design-patterns/wiki). We will help
|
||||
you and answer your questions in the [Gitter chatroom](https://gitter.im/iluwatar/java-design-patterns).
|
||||
|
||||
# License
|
||||
@ -259,7 +259,7 @@ This project is licensed under the terms of the MIT license.
|
||||
<td align="center"><a href="https://www.linkedin.com/in/ashish-trivedi-218379135/"><img src="https://avatars3.githubusercontent.com/u/23194128?v=4?s=100" 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?s=100" 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?s=100" 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?s=100" 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>
|
||||
<td align="center"><a href="http://subho.xyz"><img src="https://avatars0.githubusercontent.com/u/13291222?v=4?s=100" 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> <a href="#maintenance-ohbus" title="Maintenance">🚧</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td align="center"><a href="https://github.com/nahteb"><img src="https://avatars3.githubusercontent.com/u/13121570?v=4?s=100" 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>
|
||||
@ -313,6 +313,22 @@ This project is licensed under the terms of the MIT license.
|
||||
<td align="center"><a href="https://github.com/noamgrinch"><img src="https://avatars.githubusercontent.com/u/31648669?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Noam Greenshtain</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/commits?author=noamgrinch" title="Code">💻</a></td>
|
||||
<td align="center"><a href="https://xuyonghong.cn/"><img src="https://avatars.githubusercontent.com/u/14086462?v=4?s=100" width="100px;" alt=""/><br /><sub><b>yonghong Xu</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/commits?author=qfxl" title="Documentation">📖</a></td>
|
||||
<td align="center"><a href="https://www.linkedin.com/in/jinisha-vora"><img src="https://avatars.githubusercontent.com/u/40777762?v=4?s=100" width="100px;" alt=""/><br /><sub><b>jinishavora</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/pulls?q=is%3Apr+reviewed-by%3Ajinishavora" title="Reviewed Pull Requests">👀</a> <a href="https://github.com/iluwatar/java-design-patterns/commits?author=jinishavora" title="Code">💻</a></td>
|
||||
<td align="center"><a href="https://github.com/eas5"><img src="https://avatars.githubusercontent.com/u/50836521?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Elvys Soares</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/commits?author=eas5" title="Code">💻</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td align="center"><a href="https://github.com/zWeBrain"><img src="https://avatars.githubusercontent.com/u/46642512?v=4?s=100" width="100px;" alt=""/><br /><sub><b>zWeBrain</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/commits?author=zWeBrain" title="Code">💻</a></td>
|
||||
<td align="center"><a href="https://al-assad.github.io/notion/"><img src="https://avatars.githubusercontent.com/u/22493821?v=4?s=100" width="100px;" alt=""/><br /><sub><b>余林颖</b></sub></a><br /><a href="#translation-Al-assad" title="Translation">🌍</a></td>
|
||||
<td align="center"><a href="https://github.com/STudio26"><img src="https://avatars.githubusercontent.com/u/6988911?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Alain</b></sub></a><br /><a href="#translation-STudio26" title="Translation">🌍</a></td>
|
||||
<td align="center"><a href="https://github.com/DEV-VRUPER"><img src="https://avatars.githubusercontent.com/u/30525467?v=4?s=100" width="100px;" alt=""/><br /><sub><b>VR</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/commits?author=DEV-VRUPER" title="Documentation">📖</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td align="center"><a href="https://github.com/JackieNim"><img src="https://avatars.githubusercontent.com/u/4138836?v=4?s=100" width="100px;" alt=""/><br /><sub><b>JackieNim</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/commits?author=JackieNim" title="Code">💻</a></td>
|
||||
<td align="center"><a href="https://github.com/EdisonE3"><img src="https://avatars.githubusercontent.com/u/52118917?v=4?s=100" width="100px;" alt=""/><br /><sub><b>EdisonE3</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/commits?author=EdisonE3" title="Code">💻</a></td>
|
||||
<td align="center"><a href="https://github.com/tao-sun2"><img src="https://avatars.githubusercontent.com/u/66189688?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Tao</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/commits?author=tao-sun2" title="Code">💻</a></td>
|
||||
<td align="center"><a href="https://github.com/JuanManuelAbate"><img src="https://avatars.githubusercontent.com/u/16357060?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Juan Manuel Abate</b></sub></a><br /><a href="#translation-JuanManuelAbate" title="Translation">🌍</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td align="center"><a href="https://github.com/Xenilo137"><img src="https://avatars.githubusercontent.com/u/24865069?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Xenilo137</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/commits?author=Xenilo137" title="Code">💻</a></td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
|
@ -4,6 +4,7 @@ title: Abstract Document
|
||||
folder: abstract-document
|
||||
permalink: /patterns/abstract-document/
|
||||
categories: Structural
|
||||
language: en
|
||||
tags:
|
||||
- Extensibility
|
||||
---
|
||||
|
@ -30,7 +30,7 @@
|
||||
<parent>
|
||||
<artifactId>java-design-patterns</artifactId>
|
||||
<groupId>com.iluwatar</groupId>
|
||||
<version>1.24.0-SNAPSHOT</version>
|
||||
<version>1.25.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<artifactId>abstract-document</artifactId>
|
||||
<dependencies>
|
||||
|
@ -4,6 +4,7 @@ title: Abstract Factory
|
||||
folder: abstract-factory
|
||||
permalink: /patterns/abstract-factory/
|
||||
categories: Creational
|
||||
language: en
|
||||
tags:
|
||||
- Gang of Four
|
||||
---
|
||||
|
@ -31,7 +31,7 @@
|
||||
<parent>
|
||||
<groupId>com.iluwatar</groupId>
|
||||
<artifactId>java-design-patterns</artifactId>
|
||||
<version>1.24.0-SNAPSHOT</version>
|
||||
<version>1.25.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<artifactId>abstract-factory</artifactId>
|
||||
<dependencies>
|
||||
|
@ -4,6 +4,7 @@ title: Active Object
|
||||
folder: active-object
|
||||
permalink: /patterns/active-object/
|
||||
categories: Concurrency
|
||||
language: en
|
||||
tags:
|
||||
- Performance
|
||||
---
|
||||
|
@ -31,7 +31,7 @@
|
||||
<parent>
|
||||
<groupId>com.iluwatar</groupId>
|
||||
<artifactId>java-design-patterns</artifactId>
|
||||
<version>1.24.0-SNAPSHOT</version>
|
||||
<version>1.25.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<artifactId>active-object</artifactId>
|
||||
<dependencies>
|
||||
|
@ -4,6 +4,7 @@ title: Acyclic Visitor
|
||||
folder: acyclic-visitor
|
||||
permalink: /patterns/acyclic-visitor/
|
||||
categories: Behavioral
|
||||
language: en
|
||||
tags:
|
||||
- Extensibility
|
||||
---
|
||||
|
@ -30,7 +30,7 @@
|
||||
<parent>
|
||||
<groupId>com.iluwatar</groupId>
|
||||
<artifactId>java-design-patterns</artifactId>
|
||||
<version>1.24.0-SNAPSHOT</version>
|
||||
<version>1.25.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>acyclic-visitor</artifactId>
|
||||
|
@ -4,6 +4,7 @@ title: Adapter
|
||||
folder: adapter
|
||||
permalink: /patterns/adapter/
|
||||
categories: Structural
|
||||
language: en
|
||||
tags:
|
||||
- Gang of Four
|
||||
---
|
||||
|
@ -31,7 +31,7 @@
|
||||
<parent>
|
||||
<groupId>com.iluwatar</groupId>
|
||||
<artifactId>java-design-patterns</artifactId>
|
||||
<version>1.24.0-SNAPSHOT</version>
|
||||
<version>1.25.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<artifactId>adapter</artifactId>
|
||||
<dependencies>
|
||||
|
@ -4,6 +4,7 @@ title: Aggregator Microservices
|
||||
folder: aggregator-microservices
|
||||
permalink: /patterns/aggregator-microservices/
|
||||
categories: Architectural
|
||||
language: en
|
||||
tags:
|
||||
- Cloud distributed
|
||||
- Decoupling
|
||||
|
@ -29,7 +29,7 @@
|
||||
<parent>
|
||||
<artifactId>aggregator-microservices</artifactId>
|
||||
<groupId>com.iluwatar</groupId>
|
||||
<version>1.24.0-SNAPSHOT</version>
|
||||
<version>1.25.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>aggregator-service</artifactId>
|
||||
|
@ -29,7 +29,7 @@
|
||||
<parent>
|
||||
<artifactId>aggregator-microservices</artifactId>
|
||||
<groupId>com.iluwatar</groupId>
|
||||
<version>1.24.0-SNAPSHOT</version>
|
||||
<version>1.25.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
|
@ -29,7 +29,7 @@
|
||||
<parent>
|
||||
<artifactId>aggregator-microservices</artifactId>
|
||||
<groupId>com.iluwatar</groupId>
|
||||
<version>1.24.0-SNAPSHOT</version>
|
||||
<version>1.25.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>inventory-microservice</artifactId>
|
||||
|
@ -29,7 +29,7 @@
|
||||
<parent>
|
||||
<artifactId>java-design-patterns</artifactId>
|
||||
<groupId>com.iluwatar</groupId>
|
||||
<version>1.24.0-SNAPSHOT</version>
|
||||
<version>1.25.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>aggregator-microservices</artifactId>
|
||||
|
@ -4,6 +4,7 @@ title: Ambassador
|
||||
folder: ambassador
|
||||
permalink: /patterns/ambassador/
|
||||
categories: Structural
|
||||
language: en
|
||||
tags:
|
||||
- Decoupling
|
||||
- Cloud distributed
|
||||
|
@ -29,7 +29,7 @@
|
||||
<parent>
|
||||
<artifactId>java-design-patterns</artifactId>
|
||||
<groupId>com.iluwatar</groupId>
|
||||
<version>1.24.0-SNAPSHOT</version>
|
||||
<version>1.25.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>ambassador</artifactId>
|
||||
|
@ -4,6 +4,7 @@ title: API Gateway
|
||||
folder: api-gateway
|
||||
permalink: /patterns/api-gateway/
|
||||
categories: Architectural
|
||||
language: en
|
||||
tags:
|
||||
- Cloud distributed
|
||||
- Decoupling
|
||||
|
@ -29,7 +29,7 @@
|
||||
<parent>
|
||||
<artifactId>api-gateway</artifactId>
|
||||
<groupId>com.iluwatar</groupId>
|
||||
<version>1.24.0-SNAPSHOT</version>
|
||||
<version>1.25.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>api-gateway-service</artifactId>
|
||||
|
@ -29,7 +29,7 @@
|
||||
<parent>
|
||||
<artifactId>api-gateway</artifactId>
|
||||
<groupId>com.iluwatar</groupId>
|
||||
<version>1.24.0-SNAPSHOT</version>
|
||||
<version>1.25.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>image-microservice</artifactId>
|
||||
|
@ -29,7 +29,7 @@
|
||||
<parent>
|
||||
<artifactId>java-design-patterns</artifactId>
|
||||
<groupId>com.iluwatar</groupId>
|
||||
<version>1.24.0-SNAPSHOT</version>
|
||||
<version>1.25.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>api-gateway</artifactId>
|
||||
|
@ -29,7 +29,7 @@
|
||||
<parent>
|
||||
<artifactId>api-gateway</artifactId>
|
||||
<groupId>com.iluwatar</groupId>
|
||||
<version>1.24.0-SNAPSHOT</version>
|
||||
<version>1.25.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
@ -4,6 +4,7 @@ title: Arrange/Act/Assert
|
||||
folder: arrange-act-assert
|
||||
permalink: /patterns/arrange-act-assert/
|
||||
categories: Idiom
|
||||
language: en
|
||||
tags:
|
||||
- Testing
|
||||
---
|
||||
|
@ -29,7 +29,7 @@
|
||||
<parent>
|
||||
<artifactId>java-design-patterns</artifactId>
|
||||
<groupId>com.iluwatar</groupId>
|
||||
<version>1.24.0-SNAPSHOT</version>
|
||||
<version>1.25.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
|
Binary file not shown.
Before Width: | Height: | Size: 252 B |
Binary file not shown.
Before Width: | Height: | Size: 310 B |
Binary file not shown.
Before Width: | Height: | Size: 100 B |
Binary file not shown.
Before Width: | Height: | Size: 413 B |
Binary file not shown.
Before Width: | Height: | Size: 802 B |
@ -4,6 +4,7 @@ title: Async Method Invocation
|
||||
folder: async-method-invocation
|
||||
permalink: /patterns/async-method-invocation/
|
||||
categories: Concurrency
|
||||
language: en
|
||||
tags:
|
||||
- Reactive
|
||||
---
|
||||
|
@ -31,7 +31,7 @@
|
||||
<parent>
|
||||
<groupId>com.iluwatar</groupId>
|
||||
<artifactId>java-design-patterns</artifactId>
|
||||
<version>1.24.0-SNAPSHOT</version>
|
||||
<version>1.25.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<artifactId>async-method-invocation</artifactId>
|
||||
<dependencies>
|
||||
|
@ -4,6 +4,7 @@ title: Balking
|
||||
folder: balking
|
||||
permalink: /patterns/balking/
|
||||
categories: Concurrency
|
||||
language: en
|
||||
tags:
|
||||
- Decoupling
|
||||
---
|
||||
|
@ -29,7 +29,7 @@
|
||||
<parent>
|
||||
<artifactId>java-design-patterns</artifactId>
|
||||
<groupId>com.iluwatar</groupId>
|
||||
<version>1.24.0-SNAPSHOT</version>
|
||||
<version>1.25.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
|
@ -4,6 +4,7 @@ title: Bridge
|
||||
folder: bridge
|
||||
permalink: /patterns/bridge/
|
||||
categories: Structural
|
||||
language: en
|
||||
tags:
|
||||
- Gang of Four
|
||||
---
|
||||
|
@ -31,7 +31,7 @@
|
||||
<parent>
|
||||
<groupId>com.iluwatar</groupId>
|
||||
<artifactId>java-design-patterns</artifactId>
|
||||
<version>1.24.0-SNAPSHOT</version>
|
||||
<version>1.25.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<artifactId>bridge</artifactId>
|
||||
<dependencies>
|
||||
|
@ -43,7 +43,7 @@ public class Hammer implements Weapon {
|
||||
|
||||
@Override
|
||||
public void swing() {
|
||||
LOGGER.info("The hammer is swinged.");
|
||||
LOGGER.info("The hammer is swung.");
|
||||
enchantment.apply();
|
||||
}
|
||||
|
||||
|
@ -43,7 +43,7 @@ public class Sword implements Weapon {
|
||||
|
||||
@Override
|
||||
public void swing() {
|
||||
LOGGER.info("The sword is swinged.");
|
||||
LOGGER.info("The sword is swung.");
|
||||
enchantment.apply();
|
||||
}
|
||||
|
||||
|
@ -4,6 +4,7 @@ title: Builder
|
||||
folder: builder
|
||||
permalink: /patterns/builder/
|
||||
categories: Creational
|
||||
language: en
|
||||
tags:
|
||||
- Gang of Four
|
||||
---
|
||||
|
@ -31,7 +31,7 @@
|
||||
<parent>
|
||||
<groupId>com.iluwatar</groupId>
|
||||
<artifactId>java-design-patterns</artifactId>
|
||||
<version>1.24.0-SNAPSHOT</version>
|
||||
<version>1.25.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<artifactId>builder</artifactId>
|
||||
<dependencies>
|
||||
|
@ -4,6 +4,7 @@ title: Business Delegate
|
||||
folder: business-delegate
|
||||
permalink: /patterns/business-delegate/
|
||||
categories: Structural
|
||||
language: en
|
||||
tags:
|
||||
- Decoupling
|
||||
---
|
||||
|
@ -31,7 +31,7 @@
|
||||
<parent>
|
||||
<groupId>com.iluwatar</groupId>
|
||||
<artifactId>java-design-patterns</artifactId>
|
||||
<version>1.24.0-SNAPSHOT</version>
|
||||
<version>1.25.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<artifactId>business-delegate</artifactId>
|
||||
<dependencies>
|
||||
|
@ -4,6 +4,7 @@ title: Bytecode
|
||||
folder: bytecode
|
||||
permalink: /patterns/bytecode/
|
||||
categories: Behavioral
|
||||
language: en
|
||||
tags:
|
||||
- Game programming
|
||||
---
|
||||
|
@ -29,7 +29,7 @@
|
||||
<parent>
|
||||
<artifactId>java-design-patterns</artifactId>
|
||||
<groupId>com.iluwatar</groupId>
|
||||
<version>1.24.0-SNAPSHOT</version>
|
||||
<version>1.25.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
|
@ -4,26 +4,333 @@ title: Caching
|
||||
folder: caching
|
||||
permalink: /patterns/caching/
|
||||
categories: Behavioral
|
||||
language: en
|
||||
tags:
|
||||
- Performance
|
||||
- Cloud distributed
|
||||
---
|
||||
|
||||
## Intent
|
||||
To avoid expensive re-acquisition of resources by not releasing
|
||||
the resources immediately after their use. The resources retain their identity, are kept in some
|
||||
fast-access storage, and are re-used to avoid having to acquire them again.
|
||||
|
||||
The caching pattern avoids expensive re-acquisition of resources by not releasing them immediately
|
||||
after use. The resources retain their identity, are kept in some fast-access storage, and are
|
||||
re-used to avoid having to acquire them again.
|
||||
|
||||
## Explanation
|
||||
|
||||
Real world example
|
||||
|
||||
> A team is working on a website that provides new homes for abandoned cats. People can post their
|
||||
> cats on the website after registering, but all the new posts require approval from one of the
|
||||
> site moderators. The user accounts of the site moderators contain a specific flag and the data
|
||||
> is stored in a MongoDB database. Checking for the moderator flag each time a post is viewed
|
||||
> becomes expensive and it's a good idea to utilize caching here.
|
||||
|
||||
In plain words
|
||||
|
||||
> Caching pattern keeps frequently needed data in fast-access storage to improve performance.
|
||||
|
||||
Wikipedia says:
|
||||
|
||||
> In computing, a cache is a hardware or software component that stores data so that future
|
||||
> requests for that data can be served faster; the data stored in a cache might be the result of
|
||||
> an earlier computation or a copy of data stored elsewhere. A cache hit occurs when the requested
|
||||
> data can be found in a cache, while a cache miss occurs when it cannot. Cache hits are served by
|
||||
> reading data from the cache, which is faster than recomputing a result or reading from a slower
|
||||
> data store; thus, the more requests that can be served from the cache, the faster the system
|
||||
> performs.
|
||||
|
||||
**Programmatic Example**
|
||||
|
||||
Let's first look at the data layer of our application. The interesting classes are `UserAccount`
|
||||
which is a simple Java object containing the user account details, and `DbManager` which handles
|
||||
reading and writing of these objects to/from MongoDB database.
|
||||
|
||||
```java
|
||||
@Setter
|
||||
@Getter
|
||||
@AllArgsConstructor
|
||||
@ToString
|
||||
public class UserAccount {
|
||||
private String userId;
|
||||
private String userName;
|
||||
private String additionalInfo;
|
||||
}
|
||||
|
||||
@Slf4j
|
||||
public final class DbManager {
|
||||
|
||||
private static MongoClient mongoClient;
|
||||
private static MongoDatabase db;
|
||||
|
||||
private DbManager() { /*...*/ }
|
||||
|
||||
public static void createVirtualDb() { /*...*/ }
|
||||
|
||||
public static void connect() throws ParseException { /*...*/ }
|
||||
|
||||
public static UserAccount readFromDb(String userId) { /*...*/ }
|
||||
|
||||
public static void writeToDb(UserAccount userAccount) { /*...*/ }
|
||||
|
||||
public static void updateDb(UserAccount userAccount) { /*...*/ }
|
||||
|
||||
public static void upsertDb(UserAccount userAccount) { /*...*/ }
|
||||
}
|
||||
```
|
||||
|
||||
In the example, we are demonstrating various different caching policies
|
||||
|
||||
* Write-through writes data to the cache and DB in a single transaction
|
||||
* Write-around writes data immediately into the DB instead of the cache
|
||||
* Write-behind writes data into the cache initially whilst the data is only written into the DB
|
||||
when the cache is full
|
||||
* Cache-aside pushes the responsibility of keeping the data synchronized in both data sources to
|
||||
the application itself
|
||||
* Read-through strategy is also included in the aforementioned strategies and it returns data from
|
||||
the cache to the caller if it exists, otherwise queries from DB and stores it into the cache for
|
||||
future use.
|
||||
|
||||
The cache implementation in `LruCache` is a hash table accompanied by a doubly
|
||||
linked-list. The linked-list helps in capturing and maintaining the LRU data in the cache. When
|
||||
data is queried (from the cache), added (to the cache), or updated, the data is moved to the front
|
||||
of the list to depict itself as the most-recently-used data. The LRU data is always at the end of
|
||||
the list.
|
||||
|
||||
```java
|
||||
@Slf4j
|
||||
public class LruCache {
|
||||
|
||||
static class Node {
|
||||
String userId;
|
||||
UserAccount userAccount;
|
||||
Node previous;
|
||||
Node next;
|
||||
|
||||
public Node(String userId, UserAccount userAccount) {
|
||||
this.userId = userId;
|
||||
this.userAccount = userAccount;
|
||||
}
|
||||
}
|
||||
|
||||
/* ... omitted details ... */
|
||||
|
||||
public LruCache(int capacity) {
|
||||
this.capacity = capacity;
|
||||
}
|
||||
|
||||
public UserAccount get(String userId) {
|
||||
if (cache.containsKey(userId)) {
|
||||
var node = cache.get(userId);
|
||||
remove(node);
|
||||
setHead(node);
|
||||
return node.userAccount;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void set(String userId, UserAccount userAccount) {
|
||||
if (cache.containsKey(userId)) {
|
||||
var old = cache.get(userId);
|
||||
old.userAccount = userAccount;
|
||||
remove(old);
|
||||
setHead(old);
|
||||
} else {
|
||||
var newNode = new Node(userId, userAccount);
|
||||
if (cache.size() >= capacity) {
|
||||
LOGGER.info("# Cache is FULL! Removing {} from cache...", end.userId);
|
||||
cache.remove(end.userId); // remove LRU data from cache.
|
||||
remove(end);
|
||||
setHead(newNode);
|
||||
} else {
|
||||
setHead(newNode);
|
||||
}
|
||||
cache.put(userId, newNode);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean contains(String userId) {
|
||||
return cache.containsKey(userId);
|
||||
}
|
||||
|
||||
public void remove(Node node) { /* ... */ }
|
||||
public void setHead(Node node) { /* ... */ }
|
||||
public void invalidate(String userId) { /* ... */ }
|
||||
public boolean isFull() { /* ... */ }
|
||||
public UserAccount getLruData() { /* ... */ }
|
||||
public void clear() { /* ... */ }
|
||||
public List<UserAccount> getCacheDataInListForm() { /* ... */ }
|
||||
public void setCapacity(int newCapacity) { /* ... */ }
|
||||
}
|
||||
```
|
||||
|
||||
The next layer we are going to look at is `CacheStore` which implements the different caching
|
||||
strategies.
|
||||
|
||||
```java
|
||||
@Slf4j
|
||||
public class CacheStore {
|
||||
|
||||
private static LruCache cache;
|
||||
|
||||
/* ... details omitted ... */
|
||||
|
||||
public static UserAccount readThrough(String userId) {
|
||||
if (cache.contains(userId)) {
|
||||
LOGGER.info("# Cache Hit!");
|
||||
return cache.get(userId);
|
||||
}
|
||||
LOGGER.info("# Cache Miss!");
|
||||
UserAccount userAccount = DbManager.readFromDb(userId);
|
||||
cache.set(userId, userAccount);
|
||||
return userAccount;
|
||||
}
|
||||
|
||||
public static void writeThrough(UserAccount userAccount) {
|
||||
if (cache.contains(userAccount.getUserId())) {
|
||||
DbManager.updateDb(userAccount);
|
||||
} else {
|
||||
DbManager.writeToDb(userAccount);
|
||||
}
|
||||
cache.set(userAccount.getUserId(), userAccount);
|
||||
}
|
||||
|
||||
public static void clearCache() {
|
||||
if (cache != null) {
|
||||
cache.clear();
|
||||
}
|
||||
}
|
||||
|
||||
public static void flushCache() {
|
||||
LOGGER.info("# flushCache...");
|
||||
Optional.ofNullable(cache)
|
||||
.map(LruCache::getCacheDataInListForm)
|
||||
.orElse(List.of())
|
||||
.forEach(DbManager::updateDb);
|
||||
}
|
||||
|
||||
/* ... omitted the implementation of other caching strategies ... */
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
`AppManager` helps to bridge the gap in communication between the main class and the application's
|
||||
back-end. DB connection is initialized through this class. The chosen caching strategy/policy is
|
||||
also initialized here. Before the cache can be used, the size of the cache has to be set. Depending
|
||||
on the chosen caching policy, `AppManager` will call the appropriate function in the `CacheStore`
|
||||
class.
|
||||
|
||||
```java
|
||||
@Slf4j
|
||||
public final class AppManager {
|
||||
|
||||
private static CachingPolicy cachingPolicy;
|
||||
|
||||
private AppManager() {
|
||||
}
|
||||
|
||||
public static void initDb(boolean useMongoDb) { /* ... */ }
|
||||
|
||||
public static void initCachingPolicy(CachingPolicy policy) { /* ... */ }
|
||||
|
||||
public static void initCacheCapacity(int capacity) { /* ... */ }
|
||||
|
||||
public static UserAccount find(String userId) {
|
||||
if (cachingPolicy == CachingPolicy.THROUGH || cachingPolicy == CachingPolicy.AROUND) {
|
||||
return CacheStore.readThrough(userId);
|
||||
} else if (cachingPolicy == CachingPolicy.BEHIND) {
|
||||
return CacheStore.readThroughWithWriteBackPolicy(userId);
|
||||
} else if (cachingPolicy == CachingPolicy.ASIDE) {
|
||||
return findAside(userId);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static void save(UserAccount userAccount) {
|
||||
if (cachingPolicy == CachingPolicy.THROUGH) {
|
||||
CacheStore.writeThrough(userAccount);
|
||||
} else if (cachingPolicy == CachingPolicy.AROUND) {
|
||||
CacheStore.writeAround(userAccount);
|
||||
} else if (cachingPolicy == CachingPolicy.BEHIND) {
|
||||
CacheStore.writeBehind(userAccount);
|
||||
} else if (cachingPolicy == CachingPolicy.ASIDE) {
|
||||
saveAside(userAccount);
|
||||
}
|
||||
}
|
||||
|
||||
public static String printCacheContent() {
|
||||
return CacheStore.print();
|
||||
}
|
||||
|
||||
/* ... details omitted ... */
|
||||
}
|
||||
```
|
||||
|
||||
Here is what we do in the main class of the application.
|
||||
|
||||
```java
|
||||
@Slf4j
|
||||
public class App {
|
||||
|
||||
public static void main(String[] args) {
|
||||
AppManager.initDb(false);
|
||||
AppManager.initCacheCapacity(3);
|
||||
var app = new App();
|
||||
app.useReadAndWriteThroughStrategy();
|
||||
app.useReadThroughAndWriteAroundStrategy();
|
||||
app.useReadThroughAndWriteBehindStrategy();
|
||||
app.useCacheAsideStategy();
|
||||
}
|
||||
|
||||
public void useReadAndWriteThroughStrategy() {
|
||||
LOGGER.info("# CachingPolicy.THROUGH");
|
||||
AppManager.initCachingPolicy(CachingPolicy.THROUGH);
|
||||
var userAccount1 = new UserAccount("001", "John", "He is a boy.");
|
||||
AppManager.save(userAccount1);
|
||||
LOGGER.info(AppManager.printCacheContent());
|
||||
AppManager.find("001");
|
||||
AppManager.find("001");
|
||||
}
|
||||
|
||||
public void useReadThroughAndWriteAroundStrategy() { /* ... */ }
|
||||
|
||||
public void useReadThroughAndWriteBehindStrategy() { /* ... */ }
|
||||
|
||||
public void useCacheAsideStategy() { /* ... */ }
|
||||
}
|
||||
```
|
||||
|
||||
Finally, here is some of the console output from the program.
|
||||
|
||||
```
|
||||
12:32:53.845 [main] INFO com.iluwatar.caching.App - # CachingPolicy.THROUGH
|
||||
12:32:53.900 [main] INFO com.iluwatar.caching.App -
|
||||
--CACHE CONTENT--
|
||||
UserAccount(userId=001, userName=John, additionalInfo=He is a boy.)
|
||||
----
|
||||
```
|
||||
|
||||
## Class diagram
|
||||
|
||||

|
||||
|
||||
## Applicability
|
||||
|
||||
Use the Caching pattern(s) when
|
||||
|
||||
* Repetitious acquisition, initialization, and release of the same resource causes unnecessary performance overhead.
|
||||
* Repetitious acquisition, initialization, and release of the same resource cause unnecessary
|
||||
performance overhead.
|
||||
|
||||
## Related patterns
|
||||
|
||||
* [Proxy](https://java-design-patterns.com/patterns/proxy/)
|
||||
|
||||
## Credits
|
||||
|
||||
* [Write-through, write-around, write-back: Cache explained](http://www.computerweekly.com/feature/Write-through-write-around-write-back-Cache-explained)
|
||||
* [Read-Through, Write-Through, Write-Behind, and Refresh-Ahead Caching](https://docs.oracle.com/cd/E15357_01/coh.360/e15723/cache_rtwtwbra.htm#COHDG5177)
|
||||
* [Cache-Aside pattern](https://docs.microsoft.com/en-us/azure/architecture/patterns/cache-aside)
|
||||
* [Java EE 8 High Performance: Master techniques such as memory optimization, caching, concurrency, and multithreading to achieve maximum performance from your enterprise applications](https://www.amazon.com/gp/product/178847306X/ref=as_li_qf_asin_il_tl?ie=UTF8&tag=javadesignpat-20&creative=9325&linkCode=as2&creativeASIN=178847306X&linkId=e948720055599f248cdac47da9125ff4)
|
||||
* [Java Performance: In-Depth Advice for Tuning and Programming Java 8, 11, and Beyond](https://www.amazon.com/gp/product/1492056111/ref=as_li_qf_asin_il_tl?ie=UTF8&tag=javadesignpat-20&creative=9325&linkCode=as2&creativeASIN=1492056111&linkId=7e553581559b9ec04221259e52004b08)
|
||||
* [Effective Java](https://www.amazon.com/gp/product/B078H61SCH/ref=as_li_qf_asin_il_tl?ie=UTF8&tag=javadesignpat-20&creative=9325&linkCode=as2&creativeASIN=B078H61SCH&linkId=f06607a0b48c76541ef19c5b8b9e7882)
|
||||
* [Java Performance: The Definitive Guide: Getting the Most Out of Your Code](https://www.amazon.com/gp/product/1449358454/ref=as_li_qf_asin_il_tl?ie=UTF8&tag=javadesignpat-20&creative=9325&linkCode=as2&creativeASIN=1449358454&linkId=475c18363e350630cc0b39ab681b2687)
|
||||
|
@ -29,7 +29,7 @@
|
||||
<parent>
|
||||
<groupId>com.iluwatar</groupId>
|
||||
<artifactId>java-design-patterns</artifactId>
|
||||
<version>1.24.0-SNAPSHOT</version>
|
||||
<version>1.25.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<artifactId>caching</artifactId>
|
||||
<dependencies>
|
||||
|
@ -25,6 +25,7 @@ package com.iluwatar.caching;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.util.Optional;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* AppManager helps to bridge the gap in communication between the main class and the application's
|
||||
@ -33,6 +34,7 @@ import java.util.Optional;
|
||||
* Depending on the chosen caching policy, AppManager will call the appropriate function in the
|
||||
* CacheStore class.
|
||||
*/
|
||||
@Slf4j
|
||||
public final class AppManager {
|
||||
|
||||
private static CachingPolicy cachingPolicy;
|
||||
@ -50,7 +52,7 @@ public final class AppManager {
|
||||
try {
|
||||
DbManager.connect();
|
||||
} catch (ParseException e) {
|
||||
e.printStackTrace();
|
||||
LOGGER.error("Error connecting to MongoDB", e);
|
||||
}
|
||||
} else {
|
||||
DbManager.createVirtualDb();
|
||||
|
@ -30,6 +30,7 @@ import com.mongodb.client.model.UpdateOptions;
|
||||
import java.text.ParseException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.bson.Document;
|
||||
|
||||
/**
|
||||
@ -41,6 +42,7 @@ import org.bson.Document;
|
||||
* underlying data storage (connect()) or a simple Java data structure to (temporarily) store the
|
||||
* data/objects during runtime (createVirtualDB()).</p>
|
||||
*/
|
||||
@Slf4j
|
||||
public final class DbManager {
|
||||
|
||||
private static MongoClient mongoClient;
|
||||
@ -83,7 +85,7 @@ public final class DbManager {
|
||||
try {
|
||||
connect();
|
||||
} catch (ParseException e) {
|
||||
e.printStackTrace();
|
||||
LOGGER.error("Error connecting to MongoDB", e);
|
||||
}
|
||||
}
|
||||
var iterable = db
|
||||
@ -110,7 +112,7 @@ public final class DbManager {
|
||||
try {
|
||||
connect();
|
||||
} catch (ParseException e) {
|
||||
e.printStackTrace();
|
||||
LOGGER.error("Error connecting to MongoDB", e);
|
||||
}
|
||||
}
|
||||
db.getCollection(CachingConstants.USER_ACCOUNT).insertOne(
|
||||
@ -132,7 +134,7 @@ public final class DbManager {
|
||||
try {
|
||||
connect();
|
||||
} catch (ParseException e) {
|
||||
e.printStackTrace();
|
||||
LOGGER.error("Error connecting to MongoDB", e);
|
||||
}
|
||||
}
|
||||
db.getCollection(CachingConstants.USER_ACCOUNT).updateOne(
|
||||
@ -153,7 +155,7 @@ public final class DbManager {
|
||||
try {
|
||||
connect();
|
||||
} catch (ParseException e) {
|
||||
e.printStackTrace();
|
||||
LOGGER.error("Error connecting to MongoDB", e);
|
||||
}
|
||||
}
|
||||
db.getCollection(CachingConstants.USER_ACCOUNT).updateOne(
|
||||
|
@ -4,6 +4,7 @@ title: Callback
|
||||
folder: callback
|
||||
permalink: /patterns/callback/
|
||||
categories: Idiom
|
||||
language: en
|
||||
tags:
|
||||
- Reactive
|
||||
---
|
||||
|
@ -29,7 +29,7 @@
|
||||
<parent>
|
||||
<groupId>com.iluwatar</groupId>
|
||||
<artifactId>java-design-patterns</artifactId>
|
||||
<version>1.24.0-SNAPSHOT</version>
|
||||
<version>1.25.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<artifactId>callback</artifactId>
|
||||
<dependencies>
|
||||
|
@ -4,6 +4,7 @@ title: Chain of responsibility
|
||||
folder: chain
|
||||
permalink: /patterns/chain/
|
||||
categories: Behavioral
|
||||
language: en
|
||||
tags:
|
||||
- Gang of Four
|
||||
---
|
||||
|
@ -29,7 +29,7 @@
|
||||
<parent>
|
||||
<groupId>com.iluwatar</groupId>
|
||||
<artifactId>java-design-patterns</artifactId>
|
||||
<version>1.24.0-SNAPSHOT</version>
|
||||
<version>1.25.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<artifactId>chain</artifactId>
|
||||
<dependencies>
|
||||
|
@ -4,6 +4,7 @@ title: Circuit Breaker
|
||||
folder: circuit-breaker
|
||||
permalink: /patterns/circuit-breaker/
|
||||
categories: Behavioral
|
||||
language: en
|
||||
tags:
|
||||
- Performance
|
||||
- Decoupling
|
||||
|
@ -27,7 +27,7 @@
|
||||
<parent>
|
||||
<groupId>com.iluwatar</groupId>
|
||||
<artifactId>java-design-patterns</artifactId>
|
||||
<version>1.24.0-SNAPSHOT</version>
|
||||
<version>1.25.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<artifactId>circuit-breaker</artifactId>
|
||||
<dependencies>
|
||||
|
@ -4,6 +4,7 @@ title: Static Content Hosting
|
||||
folder: cloud-static-content-hosting
|
||||
permalink: /patterns/cloud-static-content-hosting/
|
||||
categories: Cloud
|
||||
language: en
|
||||
tags:
|
||||
- Cloud distributed
|
||||
---
|
||||
|
@ -4,6 +4,7 @@ title: Collection Pipeline
|
||||
folder: collection-pipeline
|
||||
permalink: /patterns/collection-pipeline/
|
||||
categories: Functional
|
||||
language: en
|
||||
tags:
|
||||
- Reactive
|
||||
---
|
||||
|
@ -27,7 +27,7 @@
|
||||
<parent>
|
||||
<groupId>com.iluwatar</groupId>
|
||||
<artifactId>java-design-patterns</artifactId>
|
||||
<version>1.24.0-SNAPSHOT</version>
|
||||
<version>1.25.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<artifactId>collection-pipeline</artifactId>
|
||||
<dependencies>
|
||||
|
@ -4,6 +4,7 @@ title: Combinator
|
||||
folder: combinator
|
||||
permalink: /patterns/combinator/
|
||||
categories: Idiom
|
||||
language: en
|
||||
tags:
|
||||
- Reactive
|
||||
---
|
||||
|
@ -29,7 +29,7 @@
|
||||
<parent>
|
||||
<groupId>com.iluwatar</groupId>
|
||||
<artifactId>java-design-patterns</artifactId>
|
||||
<version>1.24.0-SNAPSHOT</version>
|
||||
<version>1.25.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>combinator</artifactId>
|
||||
|
@ -4,6 +4,7 @@ title: Command
|
||||
folder: command
|
||||
permalink: /patterns/command/
|
||||
categories: Behavioral
|
||||
language: en
|
||||
tags:
|
||||
- Gang of Four
|
||||
---
|
||||
|
@ -29,7 +29,7 @@
|
||||
<parent>
|
||||
<groupId>com.iluwatar</groupId>
|
||||
<artifactId>java-design-patterns</artifactId>
|
||||
<version>1.24.0-SNAPSHOT</version>
|
||||
<version>1.25.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<artifactId>command</artifactId>
|
||||
<dependencies>
|
||||
|
@ -4,6 +4,7 @@ title: Commander
|
||||
folder: commander
|
||||
permalink: /patterns/commander/
|
||||
categories: Concurrency
|
||||
language: en
|
||||
tags:
|
||||
- Cloud distributed
|
||||
---
|
||||
|
@ -27,7 +27,7 @@
|
||||
<parent>
|
||||
<groupId>com.iluwatar</groupId>
|
||||
<artifactId>java-design-patterns</artifactId>
|
||||
<version>1.24.0-SNAPSHOT</version>
|
||||
<version>1.25.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<artifactId>commander</artifactId>
|
||||
<dependencies>
|
||||
|
123
composite-entity/README.md
Normal file
123
composite-entity/README.md
Normal file
@ -0,0 +1,123 @@
|
||||
---
|
||||
layout: pattern
|
||||
title: Composite Entity
|
||||
folder: composite-entity
|
||||
permalink: /patterns/composite-entity/
|
||||
categories: Structural
|
||||
language: en
|
||||
tags:
|
||||
- Enterprise Integration Pattern
|
||||
---
|
||||
|
||||
## Intent
|
||||
|
||||
It is used to model, represent, and manage a set of persistent objects that are interrelated, rather than representing them as individual fine-grained entities.
|
||||
|
||||
## Explanation
|
||||
|
||||
Real world example
|
||||
|
||||
> For a console, there may be many interfaces that need to be managed and controlled. Using the composite entity pattern, dependent objects such as messages and signals can be combined together and controlled using a single object.
|
||||
|
||||
In plain words
|
||||
|
||||
> Composite entity pattern allows a set of related objects to be represented and managed by a unified object.
|
||||
|
||||
**Programmatic Example**
|
||||
|
||||
We need a generic solution for the problem. To achieve this, let's introduce a generic
|
||||
Composite Entity Pattern.
|
||||
|
||||
```java
|
||||
public abstract class DependentObject<T> {
|
||||
|
||||
T data;
|
||||
|
||||
public void setData(T message) {
|
||||
this.data = message;
|
||||
}
|
||||
|
||||
public T getData() {
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
||||
public abstract class CoarseGrainedObject<T> {
|
||||
|
||||
DependentObject<T>[] dependentObjects;
|
||||
|
||||
public void setData(T... data) {
|
||||
IntStream.range(0, data.length).forEach(i -> dependentObjects[i].setData(data[i]));
|
||||
}
|
||||
|
||||
public T[] getData() {
|
||||
return (T[]) Arrays.stream(dependentObjects).map(DependentObject::getData).toArray();
|
||||
}
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
The specialized composite entity `console` inherit from this base class as follows.
|
||||
|
||||
```java
|
||||
public class MessageDependentObject extends DependentObject<String> {
|
||||
|
||||
}
|
||||
|
||||
public class SignalDependentObject extends DependentObject<String> {
|
||||
|
||||
}
|
||||
|
||||
public class ConsoleCoarseGrainedObject extends CoarseGrainedObject<String> {
|
||||
|
||||
@Override
|
||||
public String[] getData() {
|
||||
super.getData();
|
||||
return new String[]{
|
||||
dependentObjects[0].getData(), dependentObjects[1].getData()
|
||||
};
|
||||
}
|
||||
|
||||
public void init() {
|
||||
dependentObjects = new DependentObject[]{
|
||||
new MessageDependentObject(), new SignalDependentObject()};
|
||||
}
|
||||
}
|
||||
|
||||
public class CompositeEntity {
|
||||
|
||||
private final ConsoleCoarseGrainedObject console = new ConsoleCoarseGrainedObject();
|
||||
|
||||
public void setData(String message, String signal) {
|
||||
console.setData(message, signal);
|
||||
}
|
||||
|
||||
public String[] getData() {
|
||||
return console.getData();
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Now managing the assignment of message and signal objects with the composite entity `console`.
|
||||
|
||||
```java
|
||||
var console = new CompositeEntity();
|
||||
console.init();
|
||||
console.setData("No Danger", "Green Light");
|
||||
Arrays.stream(console.getData()).forEach(LOGGER::info);
|
||||
console.setData("Danger", "Red Light");
|
||||
Arrays.stream(console.getData()).forEach(LOGGER::info);
|
||||
```
|
||||
|
||||
## Class diagram
|
||||
|
||||

|
||||
|
||||
## Applicability
|
||||
|
||||
Use the Composite Entity Pattern in the following situation:
|
||||
|
||||
* You want to manage multiple dependency objects through one object to adjust the degree of granularity between objects. At the same time, the lifetime of dependency objects depends on a coarse-grained object.
|
||||
## Credits
|
||||
|
||||
* [Composite Entity Pattern in wikipedia](https://en.wikipedia.org/wiki/Composite_entity_pattern)
|
45
composite-entity/etc/composite-entity.urm.puml
Normal file
45
composite-entity/etc/composite-entity.urm.puml
Normal file
@ -0,0 +1,45 @@
|
||||
@startuml
|
||||
package com.iluwatar.compositeentity {
|
||||
class App {
|
||||
+ App(message: String, signal: String)
|
||||
+ main(args : String[]) {static}
|
||||
}
|
||||
class CompositeEntity{
|
||||
- console : ConsoleCoarseGrainedObject
|
||||
+ CompositeEntity()
|
||||
+ setData(message: String, signal: String)
|
||||
+ getData()
|
||||
+ init()
|
||||
}
|
||||
abstract CoarseGrainedObject{
|
||||
- dependentObjects : DependentObject[]
|
||||
+ CoarseGrainedObject()
|
||||
+ setData(data: T[])
|
||||
+ getData()
|
||||
}
|
||||
abstract DependentObject{
|
||||
- data : T
|
||||
+ DependentObject()
|
||||
+ setData(data: T)
|
||||
+ getData()
|
||||
}
|
||||
class ConsoleCoarseGrainedObject{
|
||||
+ ConsoleCoarseGrainedObject()
|
||||
+ getData()
|
||||
+ init()
|
||||
}
|
||||
class MessageDependentObject{
|
||||
+ MessageDependentObject()
|
||||
}
|
||||
class SignalDependentObject{
|
||||
+ SignalDependentObject()
|
||||
}
|
||||
|
||||
MessageDependentObject --|> DependentObject
|
||||
SignalDependentObject --|> DependentObject
|
||||
ConsoleCoarseGrainedObject --|> CoarseGrainedObject
|
||||
CompositeEntity -right-> ConsoleCoarseGrainedObject
|
||||
CoarseGrainedObject "1" o--> "0.." DependentObject
|
||||
App .right.> CompositeEntity
|
||||
}
|
||||
@enduml
|
BIN
composite-entity/etc/composite_entity.urm.png
Normal file
BIN
composite-entity/etc/composite_entity.urm.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 90 KiB |
63
composite-entity/pom.xml
Normal file
63
composite-entity/pom.xml
Normal file
@ -0,0 +1,63 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!--
|
||||
|
||||
The MIT License
|
||||
Copyright © 2014-2021 Ilkka Seppälä
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
|
||||
-->
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>java-design-patterns</artifactId>
|
||||
<groupId>com.iluwatar</groupId>
|
||||
<version>1.25.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>composite-entity</artifactId>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-engine</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-assembly-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<configuration>
|
||||
<archive>
|
||||
<manifest>
|
||||
<mainClass>com.iluwatar.composite-entity.com.iluwatar.compositeentity.App</mainClass>
|
||||
</manifest>
|
||||
</archive>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
@ -0,0 +1,61 @@
|
||||
/*
|
||||
* The MIT License
|
||||
* Copyright © 2014-2021 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.compositeentity;
|
||||
|
||||
import java.util.Arrays;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
|
||||
/**
|
||||
* Composite entity is a Java EE Software design pattern and it is used to model, represent, and
|
||||
* manage a set of interrelated persistent objects rather than representing them as individual
|
||||
* fine-grained entity beans, and also a composite entity bean represents a graph of objects.
|
||||
*/
|
||||
@Slf4j
|
||||
public class App {
|
||||
|
||||
|
||||
/**
|
||||
* An instance that a console manages two related objects.
|
||||
*/
|
||||
public App(String message, String signal) {
|
||||
var console = new CompositeEntity();
|
||||
console.init();
|
||||
console.setData(message, signal);
|
||||
Arrays.stream(console.getData()).forEach(LOGGER::info);
|
||||
console.setData("Danger", "Red Light");
|
||||
Arrays.stream(console.getData()).forEach(LOGGER::info);
|
||||
}
|
||||
|
||||
/**
|
||||
* Program entry point.
|
||||
*
|
||||
* @param args command line args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
|
||||
new App("No Danger", "Green Light");
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,46 @@
|
||||
/*
|
||||
* The MIT License
|
||||
* Copyright © 2014-2021 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.compositeentity;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
/**
|
||||
* A coarse-grained object is an object with its own life cycle manages its own relationships to
|
||||
* other objects. It can be an object contained in the composite entity, or, composite entity itself
|
||||
* can be the coarse-grained object which holds dependent objects.
|
||||
*/
|
||||
|
||||
public abstract class CoarseGrainedObject<T> {
|
||||
|
||||
DependentObject<T>[] dependentObjects;
|
||||
|
||||
public void setData(T... data) {
|
||||
IntStream.range(0, data.length).forEach(i -> dependentObjects[i].setData(data[i]));
|
||||
}
|
||||
|
||||
public T[] getData() {
|
||||
return (T[]) Arrays.stream(dependentObjects).map(DependentObject::getData).toArray();
|
||||
}
|
||||
}
|
@ -0,0 +1,46 @@
|
||||
/*
|
||||
* The MIT License
|
||||
* Copyright © 2014-2021 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.compositeentity;
|
||||
|
||||
/**
|
||||
* Composite entity is the coarse-grained entity bean which may be the coarse-grained object, or may
|
||||
* contain a reference to the coarse-grained object.
|
||||
*/
|
||||
|
||||
public class CompositeEntity {
|
||||
|
||||
private final ConsoleCoarseGrainedObject console = new ConsoleCoarseGrainedObject();
|
||||
|
||||
public void setData(String message, String signal) {
|
||||
console.setData(message, signal);
|
||||
}
|
||||
|
||||
public String[] getData() {
|
||||
return console.getData();
|
||||
}
|
||||
|
||||
public void init() {
|
||||
console.init();
|
||||
}
|
||||
}
|
@ -0,0 +1,43 @@
|
||||
/*
|
||||
* The MIT License
|
||||
* Copyright © 2014-2021 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.compositeentity;
|
||||
|
||||
/**
|
||||
* A specific CoarseGrainedObject to implement a console.
|
||||
*/
|
||||
|
||||
public class ConsoleCoarseGrainedObject extends CoarseGrainedObject<String> {
|
||||
|
||||
@Override
|
||||
public String[] getData() {
|
||||
return new String[]{
|
||||
dependentObjects[0].getData(), dependentObjects[1].getData()
|
||||
};
|
||||
}
|
||||
|
||||
public void init() {
|
||||
dependentObjects = new DependentObject[]{
|
||||
new MessageDependentObject(), new SignalDependentObject()};
|
||||
}
|
||||
}
|
@ -0,0 +1,43 @@
|
||||
/*
|
||||
* The MIT License
|
||||
* Copyright © 2014-2021 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.compositeentity;
|
||||
|
||||
/**
|
||||
* It is an object, which can contain other dependent objects (there may be a tree of objects within
|
||||
* the composite entity), that depends on the coarse-grained object and has its life cycle managed
|
||||
* by the coarse-grained object.
|
||||
*/
|
||||
|
||||
public abstract class DependentObject<T> {
|
||||
|
||||
T data;
|
||||
|
||||
public void setData(T message) {
|
||||
this.data = message;
|
||||
}
|
||||
|
||||
public T getData() {
|
||||
return data;
|
||||
}
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
/*
|
||||
* The MIT License
|
||||
* Copyright © 2014-2021 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.compositeentity;
|
||||
|
||||
/**
|
||||
* The first DependentObject to show message.
|
||||
*/
|
||||
|
||||
public class MessageDependentObject extends DependentObject<String> {
|
||||
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
/*
|
||||
* The MIT License
|
||||
* Copyright © 2014-2021 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.compositeentity;
|
||||
|
||||
/**
|
||||
* The second DependentObject to show message.
|
||||
*/
|
||||
|
||||
public class SignalDependentObject extends DependentObject<String> {
|
||||
|
||||
}
|
@ -0,0 +1,48 @@
|
||||
/*
|
||||
* The MIT License
|
||||
* Copyright © 2014-2021 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.compositeentity;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* com.iluwatar.compositeentity.App running test
|
||||
*/
|
||||
class AppTest {
|
||||
|
||||
/**
|
||||
* Issue: Add at least one assertion to this test case.
|
||||
* <p>
|
||||
* Solution: Inserted assertion to check whether the execution of the main method in {@link
|
||||
* App#main(String[])} throws an exception.
|
||||
*/
|
||||
|
||||
@Test
|
||||
void shouldExecuteApplicationWithoutException() {
|
||||
|
||||
assertDoesNotThrow(() -> App.main(new String[]{}));
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,55 @@
|
||||
/*
|
||||
* The MIT License
|
||||
* Copyright © 2014-2021 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.compositeentity;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class PersistenceTest {
|
||||
|
||||
final static ConsoleCoarseGrainedObject console = new ConsoleCoarseGrainedObject();
|
||||
|
||||
@Test
|
||||
void dependentObjectChangedForPersistenceTest() {
|
||||
MessageDependentObject dependentObject = new MessageDependentObject();
|
||||
console.init();
|
||||
console.dependentObjects[0] = dependentObject;
|
||||
String message = "Danger";
|
||||
assertNull(console.dependentObjects[0].getData());
|
||||
dependentObject.setData(message);
|
||||
assertEquals(message, console.dependentObjects[0].getData());
|
||||
}
|
||||
|
||||
@Test
|
||||
void coarseGrainedObjectChangedForPersistenceTest() {
|
||||
MessageDependentObject dependentObject = new MessageDependentObject();
|
||||
console.init();
|
||||
console.dependentObjects[0] = dependentObject;
|
||||
String message = "Danger";
|
||||
assertNull(console.dependentObjects[0].getData());
|
||||
console.setData(message);
|
||||
assertEquals(message, dependentObject.getData());
|
||||
}
|
||||
}
|
@ -4,6 +4,7 @@ title: Composite
|
||||
folder: composite
|
||||
permalink: /patterns/composite/
|
||||
categories: Structural
|
||||
language: en
|
||||
tags:
|
||||
- Gang of Four
|
||||
---
|
||||
|
@ -29,7 +29,7 @@
|
||||
<parent>
|
||||
<groupId>com.iluwatar</groupId>
|
||||
<artifactId>java-design-patterns</artifactId>
|
||||
<version>1.24.0-SNAPSHOT</version>
|
||||
<version>1.25.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<artifactId>composite</artifactId>
|
||||
<dependencies>
|
||||
|
@ -4,6 +4,7 @@ title: Converter
|
||||
folder: converter
|
||||
permalink: /patterns/converter/
|
||||
categories: Creational
|
||||
language: en
|
||||
tags:
|
||||
- Decoupling
|
||||
---
|
||||
|
@ -29,7 +29,7 @@
|
||||
<parent>
|
||||
<artifactId>java-design-patterns</artifactId>
|
||||
<groupId>com.iluwatar</groupId>
|
||||
<version>1.24.0-SNAPSHOT</version>
|
||||
<version>1.25.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<artifactId>converter</artifactId>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
@ -4,6 +4,7 @@ title: CQRS
|
||||
folder: cqrs
|
||||
permalink: /patterns/cqrs/
|
||||
categories: Architectural
|
||||
language: en
|
||||
tags:
|
||||
- Performance
|
||||
- Cloud distributed
|
||||
|
@ -30,7 +30,7 @@
|
||||
<parent>
|
||||
<groupId>com.iluwatar</groupId>
|
||||
<artifactId>java-design-patterns</artifactId>
|
||||
<version>1.24.0-SNAPSHOT</version>
|
||||
<version>1.25.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<artifactId>cqrs</artifactId>
|
||||
<dependencies>
|
||||
|
@ -4,6 +4,7 @@ title: Data Access Object
|
||||
folder: dao
|
||||
permalink: /patterns/dao/
|
||||
categories: Architectural
|
||||
language: en
|
||||
tags:
|
||||
- Data access
|
||||
---
|
||||
|
@ -30,7 +30,7 @@
|
||||
<parent>
|
||||
<groupId>com.iluwatar</groupId>
|
||||
<artifactId>java-design-patterns</artifactId>
|
||||
<version>1.24.0-SNAPSHOT</version>
|
||||
<version>1.25.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<artifactId>dao</artifactId>
|
||||
|
||||
|
@ -5,6 +5,7 @@ folder: data-bus
|
||||
permalink: /patterns/data-bus/
|
||||
|
||||
categories: Architectural
|
||||
language: en
|
||||
tags:
|
||||
- Decoupling
|
||||
---
|
||||
|
@ -30,7 +30,7 @@
|
||||
<parent>
|
||||
<groupId>com.iluwatar</groupId>
|
||||
<artifactId>java-design-patterns</artifactId>
|
||||
<version>1.24.0-SNAPSHOT</version>
|
||||
<version>1.25.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<artifactId>data-bus</artifactId>
|
||||
<dependencies>
|
||||
|
@ -4,6 +4,7 @@ title: Data Locality
|
||||
folder: data-locality
|
||||
permalink: /patterns/data-locality/
|
||||
categories: Behavioral
|
||||
language: en
|
||||
tags:
|
||||
- Game programming
|
||||
- Performance
|
||||
|
@ -30,7 +30,7 @@
|
||||
<parent>
|
||||
<groupId>com.iluwatar</groupId>
|
||||
<artifactId>java-design-patterns</artifactId>
|
||||
<version>1.24.0-SNAPSHOT</version>
|
||||
<version>1.25.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<artifactId>data-locality</artifactId>
|
||||
|
||||
|
@ -4,6 +4,7 @@ title: Data Mapper
|
||||
folder: data-mapper
|
||||
permalink: /patterns/data-mapper/
|
||||
categories: Architectural
|
||||
language: en
|
||||
tags:
|
||||
- Decoupling
|
||||
---
|
||||
|
@ -28,7 +28,7 @@
|
||||
<parent>
|
||||
<groupId>com.iluwatar</groupId>
|
||||
<artifactId>java-design-patterns</artifactId>
|
||||
<version>1.24.0-SNAPSHOT</version>
|
||||
<version>1.25.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<artifactId>data-mapper</artifactId>
|
||||
<dependencies>
|
||||
|
@ -4,6 +4,7 @@ title: Data Transfer Object
|
||||
folder: data-transfer-object
|
||||
permalink: /patterns/data-transfer-object/
|
||||
categories: Architectural
|
||||
language: en
|
||||
tags:
|
||||
- Performance
|
||||
---
|
||||
|
@ -28,7 +28,7 @@
|
||||
<parent>
|
||||
<groupId>com.iluwatar</groupId>
|
||||
<artifactId>java-design-patterns</artifactId>
|
||||
<version>1.24.0-SNAPSHOT</version>
|
||||
<version>1.25.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<artifactId>data-transfer-object</artifactId>
|
||||
<dependencies>
|
||||
|
@ -4,6 +4,7 @@ title: Decorator
|
||||
folder: decorator
|
||||
permalink: /patterns/decorator/
|
||||
categories: Structural
|
||||
language: en
|
||||
tags:
|
||||
- Gang Of Four
|
||||
- Extensibility
|
||||
|
@ -29,7 +29,7 @@
|
||||
<parent>
|
||||
<groupId>com.iluwatar</groupId>
|
||||
<artifactId>java-design-patterns</artifactId>
|
||||
<version>1.24.0-SNAPSHOT</version>
|
||||
<version>1.25.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<artifactId>decorator</artifactId>
|
||||
<dependencies>
|
||||
|
@ -4,6 +4,7 @@ title: Delegation
|
||||
folder: delegation
|
||||
permalink: /patterns/delegation/
|
||||
categories: Structural
|
||||
language: en
|
||||
tags:
|
||||
- Decoupling
|
||||
---
|
||||
|
@ -29,7 +29,7 @@
|
||||
<parent>
|
||||
<artifactId>java-design-patterns</artifactId>
|
||||
<groupId>com.iluwatar</groupId>
|
||||
<version>1.24.0-SNAPSHOT</version>
|
||||
<version>1.25.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
|
@ -4,6 +4,7 @@ title: Dependency Injection
|
||||
folder: dependency-injection
|
||||
permalink: /patterns/dependency-injection/
|
||||
categories: Creational
|
||||
language: en
|
||||
tags:
|
||||
- Decoupling
|
||||
---
|
||||
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user