Compare commits
51 Commits
all-contri
...
vuepress
Author | SHA1 | Date | |
---|---|---|---|
e05595194d | |||
f4d67c77f7 | |||
4d54781f79 | |||
fd915161ee | |||
097ddb385f | |||
e4ab8c8066 | |||
c3ef3dbe8c | |||
c6c4e6ebb6 | |||
801be2520b | |||
d247b6ed69 | |||
0a73ead12d | |||
ce4a6df5c5 | |||
ddb9b14eed | |||
0255111b4e | |||
9513d2be58 | |||
119abf3ee4 | |||
95699c5de3 | |||
c459b92933 | |||
87cc4df14b | |||
57f9c2e968 | |||
42eb7950ae | |||
be72a96cd6 | |||
be25c0b433 | |||
be59e50205 | |||
223b779c83 | |||
9472f10e18 | |||
b6b2079a10 | |||
81f9291e76 | |||
58c0008b75 | |||
bc7040566e | |||
bb49e3c4e9 | |||
842b400fee | |||
ec90320eda | |||
3126ad3106 | |||
2a01563d21 | |||
45af6987c1 | |||
6adb27ca3d | |||
c0e4bf3d1d | |||
50755b7215 | |||
49039843e5 | |||
dabe4d2022 | |||
aff7ef8782 | |||
c150e5f38b | |||
3c213fcd69 | |||
a437f5b2eb | |||
399ad53b09 | |||
e2ebb59fe7 | |||
d87e8cf10d | |||
c5a4068e84 | |||
d36efdbc7c | |||
3f654ab0c8 |
@ -1568,6 +1568,69 @@
|
||||
"contributions": [
|
||||
"code"
|
||||
]
|
||||
},
|
||||
{
|
||||
"login": "karthikbhat13",
|
||||
"name": "karthikbhat13",
|
||||
"avatar_url": "https://avatars.githubusercontent.com/u/22431014?v=4",
|
||||
"profile": "https://github.com/karthikbhat13",
|
||||
"contributions": [
|
||||
"code"
|
||||
]
|
||||
},
|
||||
{
|
||||
"login": "mortezaadi",
|
||||
"name": "Morteza Adigozalpour",
|
||||
"avatar_url": "https://avatars.githubusercontent.com/u/1329687?v=4",
|
||||
"profile": "https://github.com/mortezaadi",
|
||||
"contributions": [
|
||||
"code"
|
||||
]
|
||||
},
|
||||
{
|
||||
"login": "tan31989",
|
||||
"name": "Nagaraj Tantri",
|
||||
"avatar_url": "https://avatars.githubusercontent.com/u/3784194?v=4",
|
||||
"profile": "https://stackoverflow.com/users/308565/nagaraj-tantri",
|
||||
"contributions": [
|
||||
"code"
|
||||
]
|
||||
},
|
||||
{
|
||||
"login": "frascu",
|
||||
"name": "Francesco Scuccimarri",
|
||||
"avatar_url": "https://avatars.githubusercontent.com/u/7107651?v=4",
|
||||
"profile": "http://scuccimarri.it",
|
||||
"contributions": [
|
||||
"code"
|
||||
]
|
||||
},
|
||||
{
|
||||
"login": "Conhan93",
|
||||
"name": "Conny Hansson",
|
||||
"avatar_url": "https://avatars.githubusercontent.com/u/71334757?v=4",
|
||||
"profile": "https://github.com/Conhan93",
|
||||
"contributions": [
|
||||
"doc"
|
||||
]
|
||||
},
|
||||
{
|
||||
"login": "muklasr",
|
||||
"name": "Muklas Rahmanto",
|
||||
"avatar_url": "https://avatars.githubusercontent.com/u/43443753?v=4",
|
||||
"profile": "http://muklasr.medium.com",
|
||||
"contributions": [
|
||||
"translation"
|
||||
]
|
||||
},
|
||||
{
|
||||
"login": "VxDxK",
|
||||
"name": "Vadim",
|
||||
"avatar_url": "https://avatars.githubusercontent.com/u/38704817?v=4",
|
||||
"profile": "https://github.com/VxDxK",
|
||||
"contributions": [
|
||||
"translation"
|
||||
]
|
||||
}
|
||||
],
|
||||
"contributorsPerLine": 4,
|
||||
|
15
README.md
15
README.md
@ -10,12 +10,12 @@
|
||||
[](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 : [**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), [**pt**](/localization/pt/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), [**pt**](localization/pt/README.md), [**id**](localization/id/README.md), [**ru**](localization/ru/README.md)
|
||||
|
||||
<br/>
|
||||
|
||||
@ -333,6 +333,17 @@ This project is licensed under the terms of the MIT license.
|
||||
<td align="center"><a href="https://github.com/marlo2222"><img src="https://avatars.githubusercontent.com/u/40809563?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Marlo Henrique</b></sub></a><br /><a href="#translation-marlo2222" title="Translation">🌍</a></td>
|
||||
<td align="center"><a href="https://github.com/AndriyPyzh"><img src="https://avatars.githubusercontent.com/u/57706635?v=4?s=100" width="100px;" alt=""/><br /><sub><b>AndriyPyzh</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/commits?author=AndriyPyzh" title="Code">💻</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td align="center"><a href="https://github.com/karthikbhat13"><img src="https://avatars.githubusercontent.com/u/22431014?v=4?s=100" width="100px;" alt=""/><br /><sub><b>karthikbhat13</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/commits?author=karthikbhat13" title="Code">💻</a></td>
|
||||
<td align="center"><a href="https://github.com/mortezaadi"><img src="https://avatars.githubusercontent.com/u/1329687?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Morteza Adigozalpour</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/commits?author=mortezaadi" title="Code">💻</a></td>
|
||||
<td align="center"><a href="https://stackoverflow.com/users/308565/nagaraj-tantri"><img src="https://avatars.githubusercontent.com/u/3784194?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Nagaraj Tantri</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/commits?author=tan31989" title="Code">💻</a></td>
|
||||
<td align="center"><a href="http://scuccimarri.it"><img src="https://avatars.githubusercontent.com/u/7107651?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Francesco Scuccimarri</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/commits?author=frascu" title="Code">💻</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td align="center"><a href="https://github.com/Conhan93"><img src="https://avatars.githubusercontent.com/u/71334757?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Conny Hansson</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/commits?author=Conhan93" title="Documentation">📖</a></td>
|
||||
<td align="center"><a href="http://muklasr.medium.com"><img src="https://avatars.githubusercontent.com/u/43443753?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Muklas Rahmanto</b></sub></a><br /><a href="#translation-muklasr" title="Translation">🌍</a></td>
|
||||
<td align="center"><a href="https://github.com/VxDxK"><img src="https://avatars.githubusercontent.com/u/38704817?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Vadim</b></sub></a><br /><a href="#translation-VxDxK" title="Translation">🌍</a></td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
<!-- markdownlint-restore -->
|
||||
|
@ -1,9 +1,6 @@
|
||||
---
|
||||
layout: pattern
|
||||
title: Abstract Document
|
||||
folder: abstract-document
|
||||
permalink: /patterns/abstract-document/
|
||||
categories: Structural
|
||||
category: Structural
|
||||
language: en
|
||||
tags:
|
||||
- Extensibility
|
||||
|
@ -1,9 +1,6 @@
|
||||
---
|
||||
layout: pattern
|
||||
title: Abstract Factory
|
||||
folder: abstract-factory
|
||||
permalink: /patterns/abstract-factory/
|
||||
categories: Creational
|
||||
category: Creational
|
||||
language: en
|
||||
tags:
|
||||
- Gang of Four
|
||||
|
@ -1,9 +1,6 @@
|
||||
---
|
||||
layout: pattern
|
||||
title: Active Object
|
||||
folder: active-object
|
||||
permalink: /patterns/active-object/
|
||||
categories: Concurrency
|
||||
category: Concurrency
|
||||
language: en
|
||||
tags:
|
||||
- Performance
|
||||
@ -11,7 +8,7 @@ tags:
|
||||
|
||||
|
||||
## Intent
|
||||
The active object design pattern decouples method execution from method invocation for objects that each reside in their thread of control. The goal is to introduce concurrency, by using asynchronous method invocation and a scheduler for handling requests.
|
||||
The active object design pattern decouples method execution from method invocation for objects that each reside in their thread of control. The goal is to introduce concurrency, by using asynchronous method invocation, and a scheduler for handling requests.
|
||||
|
||||
## Explanation
|
||||
|
||||
@ -70,7 +67,7 @@ public abstract class ActiveCreature{
|
||||
requests.put(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
logger.info("{} has started to roam and the wastelands.",name());
|
||||
logger.info("{} has started to roam the wastelands.",name());
|
||||
}
|
||||
}
|
||||
);
|
||||
@ -82,7 +79,7 @@ public abstract class ActiveCreature{
|
||||
}
|
||||
```
|
||||
|
||||
We can see that any class that will extend the ActiveCreature class will have its own thread of control to execute and invocate methods.
|
||||
We can see that any class that will extend the ActiveCreature class will have its own thread of control to invoke and execute methods.
|
||||
|
||||
For example, the Orc class:
|
||||
|
||||
@ -96,7 +93,7 @@ public class Orc extends ActiveCreature {
|
||||
}
|
||||
```
|
||||
|
||||
Now, we can create multiple creatures such as Orcs, tell them to eat and roam and they will execute it on their own thread of control:
|
||||
Now, we can create multiple creatures such as Orcs, tell them to eat and roam, and they will execute it on their own thread of control:
|
||||
|
||||
```java
|
||||
public static void main(String[] args) {
|
||||
@ -123,4 +120,4 @@ Now, we can create multiple creatures such as Orcs, tell them to eat and roam an
|
||||
|
||||
## Class diagram
|
||||
|
||||

|
||||

|
||||
|
Before Width: | Height: | Size: 19 KiB After Width: | Height: | Size: 19 KiB |
@ -82,7 +82,7 @@ public abstract class ActiveCreature {
|
||||
}
|
||||
|
||||
/**
|
||||
* Roam in the wastelands.
|
||||
* Roam the wastelands.
|
||||
* @throws InterruptedException due to firing a new Runnable.
|
||||
*/
|
||||
public void roam() throws InterruptedException {
|
||||
|
@ -1,9 +1,6 @@
|
||||
---
|
||||
layout: pattern
|
||||
title: Acyclic Visitor
|
||||
folder: acyclic-visitor
|
||||
permalink: /patterns/acyclic-visitor/
|
||||
categories: Behavioral
|
||||
category: Behavioral
|
||||
language: en
|
||||
tags:
|
||||
- Extensibility
|
||||
|
@ -1,9 +1,6 @@
|
||||
---
|
||||
layout: pattern
|
||||
title: Adapter
|
||||
folder: adapter
|
||||
permalink: /patterns/adapter/
|
||||
categories: Structural
|
||||
category: Structural
|
||||
language: en
|
||||
tags:
|
||||
- Gang of Four
|
||||
|
@ -1,9 +1,6 @@
|
||||
---
|
||||
layout: pattern
|
||||
title: Aggregator Microservices
|
||||
folder: aggregator-microservices
|
||||
permalink: /patterns/aggregator-microservices/
|
||||
categories: Architectural
|
||||
category: Architectural
|
||||
language: en
|
||||
tags:
|
||||
- Cloud distributed
|
||||
|
@ -48,7 +48,7 @@ class AggregatorTest {
|
||||
|
||||
@BeforeEach
|
||||
public void setup() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
MockitoAnnotations.openMocks(this);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1,9 +1,6 @@
|
||||
---
|
||||
layout: pattern
|
||||
title: Ambassador
|
||||
folder: ambassador
|
||||
permalink: /patterns/ambassador/
|
||||
categories: Structural
|
||||
category: Structural
|
||||
language: en
|
||||
tags:
|
||||
- Decoupling
|
||||
|
@ -1,9 +1,6 @@
|
||||
---
|
||||
layout: pattern
|
||||
title: API Gateway
|
||||
folder: api-gateway
|
||||
permalink: /patterns/api-gateway/
|
||||
categories: Architectural
|
||||
category: Architectural
|
||||
language: en
|
||||
tags:
|
||||
- Cloud distributed
|
||||
|
@ -48,7 +48,7 @@ class ApiGatewayTest {
|
||||
|
||||
@BeforeEach
|
||||
public void setup() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
MockitoAnnotations.openMocks(this);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1,9 +1,6 @@
|
||||
---
|
||||
layout: pattern
|
||||
title: Arrange/Act/Assert
|
||||
folder: arrange-act-assert
|
||||
permalink: /patterns/arrange-act-assert/
|
||||
categories: Idiom
|
||||
category: Idiom
|
||||
language: en
|
||||
tags:
|
||||
- Testing
|
||||
|
@ -1,9 +1,6 @@
|
||||
---
|
||||
layout: pattern
|
||||
title: Async Method Invocation
|
||||
folder: async-method-invocation
|
||||
permalink: /patterns/async-method-invocation/
|
||||
categories: Concurrency
|
||||
category: Concurrency
|
||||
language: en
|
||||
tags:
|
||||
- Reactive
|
||||
|
@ -68,7 +68,7 @@ class ThreadAsyncExecutorTest {
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
MockitoAnnotations.openMocks(this);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1,9 +1,6 @@
|
||||
---
|
||||
layout: pattern
|
||||
title: Balking
|
||||
folder: balking
|
||||
permalink: /patterns/balking/
|
||||
categories: Concurrency
|
||||
category: Concurrency
|
||||
language: en
|
||||
tags:
|
||||
- Decoupling
|
||||
|
@ -1,9 +1,6 @@
|
||||
---
|
||||
layout: pattern
|
||||
title: Bridge
|
||||
folder: bridge
|
||||
permalink: /patterns/bridge/
|
||||
categories: Structural
|
||||
category: Structural
|
||||
language: en
|
||||
tags:
|
||||
- Gang of Four
|
||||
|
@ -1,9 +1,6 @@
|
||||
---
|
||||
layout: pattern
|
||||
title: Builder
|
||||
folder: builder
|
||||
permalink: /patterns/builder/
|
||||
categories: Creational
|
||||
category: Creational
|
||||
language: en
|
||||
tags:
|
||||
- Gang of Four
|
||||
|
@ -1,9 +1,6 @@
|
||||
---
|
||||
layout: pattern
|
||||
title: Business Delegate
|
||||
folder: business-delegate
|
||||
permalink: /patterns/business-delegate/
|
||||
categories: Structural
|
||||
category: Structural
|
||||
language: en
|
||||
tags:
|
||||
- Decoupling
|
||||
|
@ -1,9 +1,6 @@
|
||||
---
|
||||
layout: pattern
|
||||
title: Bytecode
|
||||
folder: bytecode
|
||||
permalink: /patterns/bytecode/
|
||||
categories: Behavioral
|
||||
category: Behavioral
|
||||
language: en
|
||||
tags:
|
||||
- Game programming
|
||||
|
@ -1,9 +1,6 @@
|
||||
---
|
||||
layout: pattern
|
||||
title: Caching
|
||||
folder: caching
|
||||
permalink: /patterns/caching/
|
||||
categories: Behavioral
|
||||
category: Behavioral
|
||||
language: en
|
||||
tags:
|
||||
- Performance
|
||||
@ -43,39 +40,29 @@ Wikipedia says:
|
||||
**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.
|
||||
which is a simple Java object containing the user account details, and `DbManager` interface which handles
|
||||
reading and writing of these objects to/from database.
|
||||
|
||||
```java
|
||||
@Setter
|
||||
@Getter
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@ToString
|
||||
@EqualsAndHashCode
|
||||
public class UserAccount {
|
||||
private String userId;
|
||||
private String userName;
|
||||
private String additionalInfo;
|
||||
}
|
||||
|
||||
@Slf4j
|
||||
public final class DbManager {
|
||||
public interface 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) { /*...*/ }
|
||||
void connect();
|
||||
void disconnect();
|
||||
|
||||
UserAccount readFromDb(String userId);
|
||||
UserAccount writeToDb(UserAccount userAccount);
|
||||
UserAccount updateDb(UserAccount userAccount);
|
||||
UserAccount upsertDb(UserAccount userAccount);
|
||||
}
|
||||
```
|
||||
|
||||
@ -171,30 +158,43 @@ strategies.
|
||||
@Slf4j
|
||||
public class CacheStore {
|
||||
|
||||
private static final int CAPACITY = 3;
|
||||
private static LruCache cache;
|
||||
private final DbManager dbManager;
|
||||
|
||||
/* ... details omitted ... */
|
||||
|
||||
public static UserAccount readThrough(String userId) {
|
||||
public UserAccount readThrough(final String userId) {
|
||||
if (cache.contains(userId)) {
|
||||
LOGGER.info("# Cache Hit!");
|
||||
LOGGER.info("# Found in Cache!");
|
||||
return cache.get(userId);
|
||||
}
|
||||
LOGGER.info("# Cache Miss!");
|
||||
UserAccount userAccount = DbManager.readFromDb(userId);
|
||||
LOGGER.info("# Not found in cache! Go to DB!!");
|
||||
UserAccount userAccount = dbManager.readFromDb(userId);
|
||||
cache.set(userId, userAccount);
|
||||
return userAccount;
|
||||
}
|
||||
|
||||
public static void writeThrough(UserAccount userAccount) {
|
||||
public void writeThrough(final UserAccount userAccount) {
|
||||
if (cache.contains(userAccount.getUserId())) {
|
||||
DbManager.updateDb(userAccount);
|
||||
dbManager.updateDb(userAccount);
|
||||
} else {
|
||||
DbManager.writeToDb(userAccount);
|
||||
dbManager.writeToDb(userAccount);
|
||||
}
|
||||
cache.set(userAccount.getUserId(), userAccount);
|
||||
}
|
||||
|
||||
public void writeAround(final UserAccount userAccount) {
|
||||
if (cache.contains(userAccount.getUserId())) {
|
||||
dbManager.updateDb(userAccount);
|
||||
// Cache data has been updated -- remove older
|
||||
cache.invalidate(userAccount.getUserId());
|
||||
// version from cache.
|
||||
} else {
|
||||
dbManager.writeToDb(userAccount);
|
||||
}
|
||||
}
|
||||
|
||||
public static void clearCache() {
|
||||
if (cache != null) {
|
||||
cache.clear();
|
||||
@ -225,34 +225,39 @@ class.
|
||||
public final class AppManager {
|
||||
|
||||
private static CachingPolicy cachingPolicy;
|
||||
private final DbManager dbManager;
|
||||
private final CacheStore cacheStore;
|
||||
|
||||
private AppManager() {
|
||||
}
|
||||
|
||||
public static void initDb(boolean useMongoDb) { /* ... */ }
|
||||
public void initDb() { /* ... */ }
|
||||
|
||||
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);
|
||||
public UserAccount find(final String userId) {
|
||||
LOGGER.info("Trying to find {} in cache", userId);
|
||||
if (cachingPolicy == CachingPolicy.THROUGH
|
||||
|| cachingPolicy == CachingPolicy.AROUND) {
|
||||
return cacheStore.readThrough(userId);
|
||||
} else if (cachingPolicy == CachingPolicy.BEHIND) {
|
||||
return CacheStore.readThroughWithWriteBackPolicy(userId);
|
||||
return cacheStore.readThroughWithWriteBackPolicy(userId);
|
||||
} else if (cachingPolicy == CachingPolicy.ASIDE) {
|
||||
return findAside(userId);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static void save(UserAccount userAccount) {
|
||||
public void save(final UserAccount userAccount) {
|
||||
LOGGER.info("Save record!");
|
||||
if (cachingPolicy == CachingPolicy.THROUGH) {
|
||||
CacheStore.writeThrough(userAccount);
|
||||
cacheStore.writeThrough(userAccount);
|
||||
} else if (cachingPolicy == CachingPolicy.AROUND) {
|
||||
CacheStore.writeAround(userAccount);
|
||||
cacheStore.writeAround(userAccount);
|
||||
} else if (cachingPolicy == CachingPolicy.BEHIND) {
|
||||
CacheStore.writeBehind(userAccount);
|
||||
cacheStore.writeBehind(userAccount);
|
||||
} else if (cachingPolicy == CachingPolicy.ASIDE) {
|
||||
saveAside(userAccount);
|
||||
}
|
||||
@ -272,24 +277,35 @@ Here is what we do in the main class of the application.
|
||||
@Slf4j
|
||||
public class App {
|
||||
|
||||
public static void main(String[] args) {
|
||||
AppManager.initDb(false);
|
||||
AppManager.initCacheCapacity(3);
|
||||
var app = new App();
|
||||
public static void main(final String[] args) {
|
||||
boolean isDbMongo = isDbMongo(args);
|
||||
if(isDbMongo){
|
||||
LOGGER.info("Using the Mongo database engine to run the application.");
|
||||
} else {
|
||||
LOGGER.info("Using the 'in Memory' database to run the application.");
|
||||
}
|
||||
App app = new App(isDbMongo);
|
||||
app.useReadAndWriteThroughStrategy();
|
||||
String splitLine = "==============================================";
|
||||
LOGGER.info(splitLine);
|
||||
app.useReadThroughAndWriteAroundStrategy();
|
||||
LOGGER.info(splitLine);
|
||||
app.useReadThroughAndWriteBehindStrategy();
|
||||
LOGGER.info(splitLine);
|
||||
app.useCacheAsideStategy();
|
||||
LOGGER.info(splitLine);
|
||||
}
|
||||
|
||||
public void useReadAndWriteThroughStrategy() {
|
||||
LOGGER.info("# CachingPolicy.THROUGH");
|
||||
AppManager.initCachingPolicy(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");
|
||||
|
||||
appManager.save(userAccount1);
|
||||
LOGGER.info(appManager.printCacheContent());
|
||||
appManager.find("001");
|
||||
appManager.find("001");
|
||||
}
|
||||
|
||||
public void useReadThroughAndWriteAroundStrategy() { /* ... */ }
|
||||
@ -300,16 +316,6 @@ public class App {
|
||||
}
|
||||
```
|
||||
|
||||
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
|
||||
|
||||

|
||||
|
11
caching/docker-compose.yml
Normal file
11
caching/docker-compose.yml
Normal file
@ -0,0 +1,11 @@
|
||||
version: '3.7'
|
||||
services:
|
||||
mongodb_container:
|
||||
image: mongo:latest
|
||||
environment:
|
||||
MONGO_INITDB_ROOT_USERNAME: root
|
||||
MONGO_INITDB_ROOT_PASSWORD: rootpassword
|
||||
ports:
|
||||
- 27017:27017
|
||||
volumes:
|
||||
- ./mongo-data/:/data/db
|
@ -39,19 +39,21 @@
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mongodb</groupId>
|
||||
<artifactId>mongodb-driver</artifactId>
|
||||
<version>3.12.1</version>
|
||||
<groupId>org.mockito</groupId>
|
||||
<artifactId>mockito-junit-jupiter</artifactId>
|
||||
<version>3.12.4</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mockito</groupId>
|
||||
<artifactId>mockito-all</artifactId>
|
||||
<version>1.10.19</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mongodb</groupId>
|
||||
<artifactId>mongodb-driver-core</artifactId>
|
||||
<version>3.0.4</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mongodb</groupId>
|
||||
<artifactId>bson</artifactId>
|
||||
<version>3.0.4</version>
|
||||
<artifactId>mongo-java-driver</artifactId>
|
||||
<version>3.4.1</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<!--
|
||||
|
@ -1,58 +1,62 @@
|
||||
/*
|
||||
* 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.caching;
|
||||
|
||||
import com.iluwatar.caching.database.DbManager;
|
||||
import com.iluwatar.caching.database.DbManagerFactory;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* The Caching pattern describes how 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. There are four main
|
||||
* caching strategies/techniques in this pattern; each with their own pros and cons. They are;
|
||||
* <code>write-through</code> which writes data to the cache and DB in a single transaction,
|
||||
* <code>write-around</code> which writes data immediately into the DB instead of the cache,
|
||||
* <code>write-behind</code> which writes data into the cache initially whilst the data is only
|
||||
* written into the DB when the cache is full, and <code>cache-aside</code> which pushes the
|
||||
* responsibility of keeping the data synchronized in both data sources to the application itself.
|
||||
* The <code>read-through</code> strategy is also included in the mentioned four strategies --
|
||||
* returns data from the cache to the caller <b>if</b> it exists <b>else</b> queries from DB and
|
||||
* stores it into the cache for future use. These strategies determine when the data in the cache
|
||||
* should be written back to the backing store (i.e. Database) and help keep both data sources
|
||||
* synchronized/up-to-date. This pattern can improve performance and also helps to maintain
|
||||
* consistency between data held in the cache and the data in the underlying data store.
|
||||
* The Caching pattern describes how 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. There are four main
|
||||
* caching strategies/techniques in this pattern; each with their own pros and
|
||||
* cons. They are <code>write-through</code> which writes data to the cache and
|
||||
* DB in a single transaction, <code>write-around</code> which writes data
|
||||
* immediately into the DB instead of the cache, <code>write-behind</code>
|
||||
* which writes data into the cache initially whilst the data is only
|
||||
* written into the DB when the cache is full, and <code>cache-aside</code>
|
||||
* which pushes the responsibility of keeping the data synchronized in both
|
||||
* data sources to the application itself. The <code>read-through</code>
|
||||
* strategy is also included in the mentioned four strategies --
|
||||
* returns data from the cache to the caller <b>if</b> it exists <b>else</b>
|
||||
* queries from DB and stores it into the cache for future use. These strategies
|
||||
* determine when the data in the cache should be written back to the backing
|
||||
* store (i.e. Database) and help keep both data sources
|
||||
* synchronized/up-to-date. This pattern can improve performance and also helps
|
||||
* to maintainconsistency between data held in the cache and the data in
|
||||
* the underlying data store.
|
||||
*
|
||||
* <p>In this example, the user account ({@link UserAccount}) entity is used as the underlying
|
||||
* application data. The cache itself is implemented as an internal (Java) data structure. It adopts
|
||||
* a Least-Recently-Used (LRU) strategy for evicting data from itself when its full. The four
|
||||
* strategies are individually tested. The testing of the cache is restricted towards saving and
|
||||
* querying of user accounts from the underlying data store ( {@link DbManager}). The main class (
|
||||
* {@link App} is not aware of the underlying mechanics of the application (i.e. save and query) and
|
||||
* whether the data is coming from the cache or the DB (i.e. separation of concern). The AppManager
|
||||
* ({@link AppManager}) handles the transaction of data to-and-from the underlying data store
|
||||
* (depending on the preferred caching policy/strategy).
|
||||
* <p>In this example, the user account ({@link UserAccount}) entity is used
|
||||
* as the underlying application data. The cache itself is implemented as an
|
||||
* internal (Java) data structure. It adopts a Least-Recently-Used (LRU)
|
||||
* strategy for evicting data from itself when its full. The four
|
||||
* strategies are individually tested. The testing of the cache is restricted
|
||||
* towards saving and querying of user accounts from the
|
||||
* underlying data store( {@link DbManager}). The main class ( {@link App}
|
||||
* is not aware of the underlying mechanics of the application
|
||||
* (i.e. save and query) and whether the data is coming from the cache or the
|
||||
* DB (i.e. separation of concern). The AppManager ({@link AppManager}) handles
|
||||
* the transaction of data to-and-from the underlying data store (depending on
|
||||
* the preferred caching policy/strategy).
|
||||
* <p>
|
||||
* <i>{@literal App --> AppManager --> CacheStore/LRUCache/CachingPolicy --> DBManager} </i>
|
||||
* <i>{@literal App --> AppManager --> CacheStore/LRUCache/CachingPolicy -->
|
||||
* DBManager} </i>
|
||||
* </p>
|
||||
*
|
||||
* <p>
|
||||
* There are 2 ways to launch the application.
|
||||
* - to use "in Memory" database.
|
||||
* - to use the MongoDb as a database
|
||||
*
|
||||
* To run the application with "in Memory" database, just launch it without parameters
|
||||
* Example: 'java -jar app.jar'
|
||||
*
|
||||
* To run the application with MongoDb you need to be installed the MongoDb
|
||||
* in your system, or to launch it in the docker container.
|
||||
* You may launch docker container from the root of current module with command:
|
||||
* 'docker-compose up'
|
||||
* Then you can start the application with parameter --mongo
|
||||
* Example: 'java -jar app.jar --mongo'
|
||||
* </p>
|
||||
*
|
||||
* @see CacheStore
|
||||
@ -61,23 +65,67 @@ import lombok.extern.slf4j.Slf4j;
|
||||
*/
|
||||
@Slf4j
|
||||
public class App {
|
||||
/**
|
||||
* Constant parameter name to use mongoDB.
|
||||
*/
|
||||
private static final String USE_MONGO_DB = "--mongo";
|
||||
/**
|
||||
* Application manager.
|
||||
*/
|
||||
private final AppManager appManager;
|
||||
|
||||
/**
|
||||
* Constructor of current App.
|
||||
*
|
||||
* @param isMongo boolean
|
||||
*/
|
||||
public App(final boolean isMongo) {
|
||||
DbManager dbManager = DbManagerFactory.initDb(isMongo);
|
||||
appManager = new AppManager(dbManager);
|
||||
appManager.initDb();
|
||||
}
|
||||
|
||||
/**
|
||||
* Program entry point.
|
||||
*
|
||||
* @param args command line args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
AppManager.initDb(false); // VirtualDB (instead of MongoDB) was used in running the JUnit tests
|
||||
public static void main(final String[] args) {
|
||||
// VirtualDB (instead of MongoDB) was used in running the JUnit tests
|
||||
// and the App class to avoid Maven compilation errors. Set flag to
|
||||
// true to run the tests with MongoDB (provided that MongoDB is
|
||||
// installed and socket connection is open).
|
||||
AppManager.initCacheCapacity(3);
|
||||
var app = new App();
|
||||
boolean isDbMongo = isDbMongo(args);
|
||||
if (isDbMongo) {
|
||||
LOGGER.info("Using the Mongo database engine to run the application.");
|
||||
} else {
|
||||
LOGGER.info("Using the 'in Memory' database to run the application.");
|
||||
}
|
||||
App app = new App(isDbMongo);
|
||||
app.useReadAndWriteThroughStrategy();
|
||||
String splitLine = "==============================================";
|
||||
LOGGER.info(splitLine);
|
||||
app.useReadThroughAndWriteAroundStrategy();
|
||||
LOGGER.info(splitLine);
|
||||
app.useReadThroughAndWriteBehindStrategy();
|
||||
LOGGER.info(splitLine);
|
||||
app.useCacheAsideStategy();
|
||||
LOGGER.info(splitLine);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check the input parameters. if
|
||||
*
|
||||
* @param args input params
|
||||
* @return true if there is "--mongo" parameter in arguments
|
||||
*/
|
||||
private static boolean isDbMongo(final String[] args) {
|
||||
for (String arg : args) {
|
||||
if (arg.equals(USE_MONGO_DB)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -85,14 +133,14 @@ public class App {
|
||||
*/
|
||||
public void useReadAndWriteThroughStrategy() {
|
||||
LOGGER.info("# CachingPolicy.THROUGH");
|
||||
AppManager.initCachingPolicy(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");
|
||||
appManager.save(userAccount1);
|
||||
LOGGER.info(appManager.printCacheContent());
|
||||
appManager.find("001");
|
||||
appManager.find("001");
|
||||
}
|
||||
|
||||
/**
|
||||
@ -100,21 +148,21 @@ public class App {
|
||||
*/
|
||||
public void useReadThroughAndWriteAroundStrategy() {
|
||||
LOGGER.info("# CachingPolicy.AROUND");
|
||||
AppManager.initCachingPolicy(CachingPolicy.AROUND);
|
||||
appManager.initCachingPolicy(CachingPolicy.AROUND);
|
||||
|
||||
var userAccount2 = new UserAccount("002", "Jane", "She is a girl.");
|
||||
|
||||
AppManager.save(userAccount2);
|
||||
LOGGER.info(AppManager.printCacheContent());
|
||||
AppManager.find("002");
|
||||
LOGGER.info(AppManager.printCacheContent());
|
||||
userAccount2 = AppManager.find("002");
|
||||
appManager.save(userAccount2);
|
||||
LOGGER.info(appManager.printCacheContent());
|
||||
appManager.find("002");
|
||||
LOGGER.info(appManager.printCacheContent());
|
||||
userAccount2 = appManager.find("002");
|
||||
userAccount2.setUserName("Jane G.");
|
||||
AppManager.save(userAccount2);
|
||||
LOGGER.info(AppManager.printCacheContent());
|
||||
AppManager.find("002");
|
||||
LOGGER.info(AppManager.printCacheContent());
|
||||
AppManager.find("002");
|
||||
appManager.save(userAccount2);
|
||||
LOGGER.info(appManager.printCacheContent());
|
||||
appManager.find("002");
|
||||
LOGGER.info(appManager.printCacheContent());
|
||||
appManager.find("002");
|
||||
}
|
||||
|
||||
/**
|
||||
@ -122,23 +170,31 @@ public class App {
|
||||
*/
|
||||
public void useReadThroughAndWriteBehindStrategy() {
|
||||
LOGGER.info("# CachingPolicy.BEHIND");
|
||||
AppManager.initCachingPolicy(CachingPolicy.BEHIND);
|
||||
appManager.initCachingPolicy(CachingPolicy.BEHIND);
|
||||
|
||||
var userAccount3 = new UserAccount("003", "Adam", "He likes food.");
|
||||
var userAccount4 = new UserAccount("004", "Rita", "She hates cats.");
|
||||
var userAccount5 = new UserAccount("005", "Isaac", "He is allergic to mustard.");
|
||||
var userAccount3 = new UserAccount("003",
|
||||
"Adam",
|
||||
"He likes food.");
|
||||
var userAccount4 = new UserAccount("004",
|
||||
"Rita",
|
||||
"She hates cats.");
|
||||
var userAccount5 = new UserAccount("005",
|
||||
"Isaac",
|
||||
"He is allergic to mustard.");
|
||||
|
||||
AppManager.save(userAccount3);
|
||||
AppManager.save(userAccount4);
|
||||
AppManager.save(userAccount5);
|
||||
LOGGER.info(AppManager.printCacheContent());
|
||||
AppManager.find("003");
|
||||
LOGGER.info(AppManager.printCacheContent());
|
||||
UserAccount userAccount6 = new UserAccount("006", "Yasha", "She is an only child.");
|
||||
AppManager.save(userAccount6);
|
||||
LOGGER.info(AppManager.printCacheContent());
|
||||
AppManager.find("004");
|
||||
LOGGER.info(AppManager.printCacheContent());
|
||||
appManager.save(userAccount3);
|
||||
appManager.save(userAccount4);
|
||||
appManager.save(userAccount5);
|
||||
LOGGER.info(appManager.printCacheContent());
|
||||
appManager.find("003");
|
||||
LOGGER.info(appManager.printCacheContent());
|
||||
UserAccount userAccount6 = new UserAccount("006",
|
||||
"Yasha",
|
||||
"She is an only child.");
|
||||
appManager.save(userAccount6);
|
||||
LOGGER.info(appManager.printCacheContent());
|
||||
appManager.find("004");
|
||||
LOGGER.info(appManager.printCacheContent());
|
||||
}
|
||||
|
||||
/**
|
||||
@ -146,20 +202,26 @@ public class App {
|
||||
*/
|
||||
public void useCacheAsideStategy() {
|
||||
LOGGER.info("# CachingPolicy.ASIDE");
|
||||
AppManager.initCachingPolicy(CachingPolicy.ASIDE);
|
||||
LOGGER.info(AppManager.printCacheContent());
|
||||
appManager.initCachingPolicy(CachingPolicy.ASIDE);
|
||||
LOGGER.info(appManager.printCacheContent());
|
||||
|
||||
var userAccount3 = new UserAccount("003", "Adam", "He likes food.");
|
||||
var userAccount4 = new UserAccount("004", "Rita", "She hates cats.");
|
||||
var userAccount5 = new UserAccount("005", "Isaac", "He is allergic to mustard.");
|
||||
AppManager.save(userAccount3);
|
||||
AppManager.save(userAccount4);
|
||||
AppManager.save(userAccount5);
|
||||
var userAccount3 = new UserAccount("003",
|
||||
"Adam",
|
||||
"He likes food.");
|
||||
var userAccount4 = new UserAccount("004",
|
||||
"Rita",
|
||||
"She hates cats.");
|
||||
var userAccount5 = new UserAccount("005",
|
||||
"Isaac",
|
||||
"He is allergic to mustard.");
|
||||
appManager.save(userAccount3);
|
||||
appManager.save(userAccount4);
|
||||
appManager.save(userAccount5);
|
||||
|
||||
LOGGER.info(AppManager.printCacheContent());
|
||||
AppManager.find("003");
|
||||
LOGGER.info(AppManager.printCacheContent());
|
||||
AppManager.find("004");
|
||||
LOGGER.info(AppManager.printCacheContent());
|
||||
LOGGER.info(appManager.printCacheContent());
|
||||
appManager.find("003");
|
||||
LOGGER.info(appManager.printCacheContent());
|
||||
appManager.find("004");
|
||||
LOGGER.info(appManager.printCacheContent());
|
||||
}
|
||||
}
|
||||
|
@ -23,65 +23,80 @@
|
||||
|
||||
package com.iluwatar.caching;
|
||||
|
||||
import java.text.ParseException;
|
||||
import com.iluwatar.caching.database.DbManager;
|
||||
|
||||
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
|
||||
* 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.
|
||||
* 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.
|
||||
*/
|
||||
@Slf4j
|
||||
public final class AppManager {
|
||||
public class AppManager {
|
||||
/**
|
||||
* Caching Policy.
|
||||
*/
|
||||
private CachingPolicy cachingPolicy;
|
||||
/**
|
||||
* Database Manager.
|
||||
*/
|
||||
private final DbManager dbManager;
|
||||
/**
|
||||
* Cache Store.
|
||||
*/
|
||||
private final CacheStore cacheStore;
|
||||
|
||||
private static CachingPolicy cachingPolicy;
|
||||
|
||||
private AppManager() {
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param newDbManager database manager
|
||||
*/
|
||||
public AppManager(final DbManager newDbManager) {
|
||||
this.dbManager = newDbManager;
|
||||
this.cacheStore = new CacheStore(newDbManager);
|
||||
}
|
||||
|
||||
/**
|
||||
* Developer/Tester is able to choose whether the application should use MongoDB as its underlying
|
||||
* data storage or a simple Java data structure to (temporarily) store the data/objects during
|
||||
* runtime.
|
||||
* Developer/Tester is able to choose whether the application should use
|
||||
* MongoDB as its underlying data storage or a simple Java data structure
|
||||
* to (temporarily) store the data/objects during runtime.
|
||||
*/
|
||||
public static void initDb(boolean useMongoDb) {
|
||||
if (useMongoDb) {
|
||||
try {
|
||||
DbManager.connect();
|
||||
} catch (ParseException e) {
|
||||
LOGGER.error("Error connecting to MongoDB", e);
|
||||
}
|
||||
} else {
|
||||
DbManager.createVirtualDb();
|
||||
}
|
||||
public void initDb() {
|
||||
dbManager.connect();
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize caching policy.
|
||||
*
|
||||
* @param policy is a {@link CachingPolicy}
|
||||
*/
|
||||
public static void initCachingPolicy(CachingPolicy policy) {
|
||||
public void initCachingPolicy(final CachingPolicy policy) {
|
||||
cachingPolicy = policy;
|
||||
if (cachingPolicy == CachingPolicy.BEHIND) {
|
||||
Runtime.getRuntime().addShutdownHook(new Thread(CacheStore::flushCache));
|
||||
Runtime.getRuntime().addShutdownHook(new Thread(cacheStore::flushCache));
|
||||
}
|
||||
CacheStore.clearCache();
|
||||
}
|
||||
|
||||
public static void initCacheCapacity(int capacity) {
|
||||
CacheStore.initCapacity(capacity);
|
||||
cacheStore.clearCache();
|
||||
}
|
||||
|
||||
/**
|
||||
* Find user account.
|
||||
*
|
||||
* @param userId String
|
||||
* @return {@link UserAccount}
|
||||
*/
|
||||
public static UserAccount find(String userId) {
|
||||
if (cachingPolicy == CachingPolicy.THROUGH || cachingPolicy == CachingPolicy.AROUND) {
|
||||
return CacheStore.readThrough(userId);
|
||||
public UserAccount find(final String userId) {
|
||||
LOGGER.info("Trying to find {} in cache", userId);
|
||||
if (cachingPolicy == CachingPolicy.THROUGH
|
||||
|| cachingPolicy == CachingPolicy.AROUND) {
|
||||
return cacheStore.readThrough(userId);
|
||||
} else if (cachingPolicy == CachingPolicy.BEHIND) {
|
||||
return CacheStore.readThroughWithWriteBackPolicy(userId);
|
||||
return cacheStore.readThroughWithWriteBackPolicy(userId);
|
||||
} else if (cachingPolicy == CachingPolicy.ASIDE) {
|
||||
return findAside(userId);
|
||||
}
|
||||
@ -90,41 +105,55 @@ public final class AppManager {
|
||||
|
||||
/**
|
||||
* Save user account.
|
||||
*
|
||||
* @param userAccount {@link UserAccount}
|
||||
*/
|
||||
public static void save(UserAccount userAccount) {
|
||||
public void save(final UserAccount userAccount) {
|
||||
LOGGER.info("Save record!");
|
||||
if (cachingPolicy == CachingPolicy.THROUGH) {
|
||||
CacheStore.writeThrough(userAccount);
|
||||
cacheStore.writeThrough(userAccount);
|
||||
} else if (cachingPolicy == CachingPolicy.AROUND) {
|
||||
CacheStore.writeAround(userAccount);
|
||||
cacheStore.writeAround(userAccount);
|
||||
} else if (cachingPolicy == CachingPolicy.BEHIND) {
|
||||
CacheStore.writeBehind(userAccount);
|
||||
cacheStore.writeBehind(userAccount);
|
||||
} else if (cachingPolicy == CachingPolicy.ASIDE) {
|
||||
saveAside(userAccount);
|
||||
}
|
||||
}
|
||||
|
||||
public static String printCacheContent() {
|
||||
return CacheStore.print();
|
||||
/**
|
||||
* Returns String.
|
||||
*
|
||||
* @return String
|
||||
*/
|
||||
public String printCacheContent() {
|
||||
return cacheStore.print();
|
||||
}
|
||||
|
||||
/**
|
||||
* Cache-Aside save user account helper.
|
||||
*
|
||||
* @param userAccount {@link UserAccount}
|
||||
*/
|
||||
private static void saveAside(UserAccount userAccount) {
|
||||
DbManager.updateDb(userAccount);
|
||||
CacheStore.invalidate(userAccount.getUserId());
|
||||
private void saveAside(final UserAccount userAccount) {
|
||||
dbManager.updateDb(userAccount);
|
||||
cacheStore.invalidate(userAccount.getUserId());
|
||||
}
|
||||
|
||||
/**
|
||||
* Cache-Aside find user account helper.
|
||||
*
|
||||
* @param userId String
|
||||
* @return {@link UserAccount}
|
||||
*/
|
||||
private static UserAccount findAside(String userId) {
|
||||
return Optional.ofNullable(CacheStore.get(userId))
|
||||
.or(() -> {
|
||||
Optional<UserAccount> userAccount = Optional.ofNullable(DbManager.readFromDb(userId));
|
||||
userAccount.ifPresent(account -> CacheStore.set(userId, account));
|
||||
return userAccount;
|
||||
})
|
||||
.orElse(null);
|
||||
private UserAccount findAside(final String userId) {
|
||||
return Optional.ofNullable(cacheStore.get(userId))
|
||||
.or(() -> {
|
||||
Optional<UserAccount> userAccount =
|
||||
Optional.ofNullable(dbManager.readFromDb(userId));
|
||||
userAccount.ifPresent(account -> cacheStore.set(userId, account));
|
||||
return userAccount;
|
||||
})
|
||||
.orElse(null);
|
||||
}
|
||||
}
|
||||
|
@ -23,9 +23,11 @@
|
||||
|
||||
package com.iluwatar.caching;
|
||||
|
||||
import com.iluwatar.caching.database.DbManager;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
@ -33,16 +35,34 @@ import lombok.extern.slf4j.Slf4j;
|
||||
*/
|
||||
@Slf4j
|
||||
public class CacheStore {
|
||||
/**
|
||||
* Cache capacity.
|
||||
*/
|
||||
private static final int CAPACITY = 3;
|
||||
|
||||
private static LruCache cache;
|
||||
/**
|
||||
* Lru cache see {@link LruCache}.
|
||||
*/
|
||||
private LruCache cache;
|
||||
/**
|
||||
* DbManager.
|
||||
*/
|
||||
private final DbManager dbManager;
|
||||
|
||||
private CacheStore() {
|
||||
/**
|
||||
* Cache Store.
|
||||
* @param dataBaseManager {@link DbManager}
|
||||
*/
|
||||
public CacheStore(final DbManager dataBaseManager) {
|
||||
this.dbManager = dataBaseManager;
|
||||
initCapacity(CAPACITY);
|
||||
}
|
||||
|
||||
/**
|
||||
* Init cache capacity.
|
||||
* @param capacity int
|
||||
*/
|
||||
public static void initCapacity(int capacity) {
|
||||
public void initCapacity(final int capacity) {
|
||||
if (cache == null) {
|
||||
cache = new LruCache(capacity);
|
||||
} else {
|
||||
@ -52,57 +72,64 @@ public class CacheStore {
|
||||
|
||||
/**
|
||||
* Get user account using read-through cache.
|
||||
* @param userId {@link String}
|
||||
* @return {@link UserAccount}
|
||||
*/
|
||||
public static UserAccount readThrough(String userId) {
|
||||
public UserAccount readThrough(final String userId) {
|
||||
if (cache.contains(userId)) {
|
||||
LOGGER.info("# Cache Hit!");
|
||||
LOGGER.info("# Found in Cache!");
|
||||
return cache.get(userId);
|
||||
}
|
||||
LOGGER.info("# Cache Miss!");
|
||||
UserAccount userAccount = DbManager.readFromDb(userId);
|
||||
LOGGER.info("# Not found in cache! Go to DB!!");
|
||||
UserAccount userAccount = dbManager.readFromDb(userId);
|
||||
cache.set(userId, userAccount);
|
||||
return userAccount;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get user account using write-through cache.
|
||||
* @param userAccount {@link UserAccount}
|
||||
*/
|
||||
public static void writeThrough(UserAccount userAccount) {
|
||||
public void writeThrough(final UserAccount userAccount) {
|
||||
if (cache.contains(userAccount.getUserId())) {
|
||||
DbManager.updateDb(userAccount);
|
||||
dbManager.updateDb(userAccount);
|
||||
} else {
|
||||
DbManager.writeToDb(userAccount);
|
||||
dbManager.writeToDb(userAccount);
|
||||
}
|
||||
cache.set(userAccount.getUserId(), userAccount);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get user account using write-around cache.
|
||||
* @param userAccount {@link UserAccount}
|
||||
*/
|
||||
public static void writeAround(UserAccount userAccount) {
|
||||
public void writeAround(final UserAccount userAccount) {
|
||||
if (cache.contains(userAccount.getUserId())) {
|
||||
DbManager.updateDb(userAccount);
|
||||
cache.invalidate(userAccount.getUserId()); // Cache data has been updated -- remove older
|
||||
dbManager.updateDb(userAccount);
|
||||
// Cache data has been updated -- remove older
|
||||
cache.invalidate(userAccount.getUserId());
|
||||
// version from cache.
|
||||
} else {
|
||||
DbManager.writeToDb(userAccount);
|
||||
dbManager.writeToDb(userAccount);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get user account using read-through cache with write-back policy.
|
||||
* @param userId {@link String}
|
||||
* @return {@link UserAccount}
|
||||
*/
|
||||
public static UserAccount readThroughWithWriteBackPolicy(String userId) {
|
||||
public UserAccount readThroughWithWriteBackPolicy(final String userId) {
|
||||
if (cache.contains(userId)) {
|
||||
LOGGER.info("# Cache Hit!");
|
||||
LOGGER.info("# Found in cache!");
|
||||
return cache.get(userId);
|
||||
}
|
||||
LOGGER.info("# Cache Miss!");
|
||||
UserAccount userAccount = DbManager.readFromDb(userId);
|
||||
LOGGER.info("# Not found in Cache!");
|
||||
UserAccount userAccount = dbManager.readFromDb(userId);
|
||||
if (cache.isFull()) {
|
||||
LOGGER.info("# Cache is FULL! Writing LRU data to DB...");
|
||||
UserAccount toBeWrittenToDb = cache.getLruData();
|
||||
DbManager.upsertDb(toBeWrittenToDb);
|
||||
dbManager.upsertDb(toBeWrittenToDb);
|
||||
}
|
||||
cache.set(userId, userAccount);
|
||||
return userAccount;
|
||||
@ -110,12 +137,13 @@ public class CacheStore {
|
||||
|
||||
/**
|
||||
* Set user account.
|
||||
* @param userAccount {@link UserAccount}
|
||||
*/
|
||||
public static void writeBehind(UserAccount userAccount) {
|
||||
public void writeBehind(final UserAccount userAccount) {
|
||||
if (cache.isFull() && !cache.contains(userAccount.getUserId())) {
|
||||
LOGGER.info("# Cache is FULL! Writing LRU data to DB...");
|
||||
UserAccount toBeWrittenToDb = cache.getLruData();
|
||||
DbManager.upsertDb(toBeWrittenToDb);
|
||||
dbManager.upsertDb(toBeWrittenToDb);
|
||||
}
|
||||
cache.set(userAccount.getUserId(), userAccount);
|
||||
}
|
||||
@ -123,7 +151,7 @@ public class CacheStore {
|
||||
/**
|
||||
* Clears cache.
|
||||
*/
|
||||
public static void clearCache() {
|
||||
public void clearCache() {
|
||||
if (cache != null) {
|
||||
cache.clear();
|
||||
}
|
||||
@ -132,44 +160,51 @@ public class CacheStore {
|
||||
/**
|
||||
* Writes remaining content in the cache into the DB.
|
||||
*/
|
||||
public static void flushCache() {
|
||||
public void flushCache() {
|
||||
LOGGER.info("# flushCache...");
|
||||
Optional.ofNullable(cache)
|
||||
.map(LruCache::getCacheDataInListForm)
|
||||
.orElse(List.of())
|
||||
.forEach(DbManager::updateDb);
|
||||
.forEach(dbManager::updateDb);
|
||||
dbManager.disconnect();
|
||||
}
|
||||
|
||||
/**
|
||||
* Print user accounts.
|
||||
* @return {@link String}
|
||||
*/
|
||||
public static String print() {
|
||||
public String print() {
|
||||
return Optional.ofNullable(cache)
|
||||
.map(LruCache::getCacheDataInListForm)
|
||||
.orElse(List.of())
|
||||
.stream()
|
||||
.map(userAccount -> userAccount.toString() + "\n")
|
||||
.collect(Collectors.joining("", "\n--CACHE CONTENT--\n", "----\n"));
|
||||
.collect(Collectors.joining("", "\n--CACHE CONTENT--\n", "----"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Delegate to backing cache store.
|
||||
* @param userId {@link String}
|
||||
* @return {@link UserAccount}
|
||||
*/
|
||||
public static UserAccount get(String userId) {
|
||||
public UserAccount get(final String userId) {
|
||||
return cache.get(userId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delegate to backing cache store.
|
||||
* @param userId {@link String}
|
||||
* @param userAccount {@link UserAccount}
|
||||
*/
|
||||
public static void set(String userId, UserAccount userAccount) {
|
||||
public void set(final String userId, final UserAccount userAccount) {
|
||||
cache.set(userId, userAccount);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delegate to backing cache store.
|
||||
* @param userId {@link String}
|
||||
*/
|
||||
public static void invalidate(String userId) {
|
||||
public void invalidate(final String userId) {
|
||||
cache.invalidate(userId);
|
||||
}
|
||||
}
|
||||
|
@ -32,10 +32,25 @@ import lombok.Getter;
|
||||
@AllArgsConstructor
|
||||
@Getter
|
||||
public enum CachingPolicy {
|
||||
/**
|
||||
* Through.
|
||||
*/
|
||||
THROUGH("through"),
|
||||
/**
|
||||
* AROUND.
|
||||
*/
|
||||
AROUND("around"),
|
||||
/**
|
||||
* BEHIND.
|
||||
*/
|
||||
BEHIND("behind"),
|
||||
/**
|
||||
* ASIDE.
|
||||
*/
|
||||
ASIDE("aside");
|
||||
|
||||
/**
|
||||
* Policy value.
|
||||
*/
|
||||
private final String policy;
|
||||
}
|
||||
|
@ -1,171 +0,0 @@
|
||||
/*
|
||||
* 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.caching;
|
||||
|
||||
import com.iluwatar.caching.constants.CachingConstants;
|
||||
import com.mongodb.MongoClient;
|
||||
import com.mongodb.client.MongoDatabase;
|
||||
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;
|
||||
|
||||
/**
|
||||
* <p>DBManager handles the communication with the underlying data store i.e. Database. It contains
|
||||
* the implemented methods for querying, inserting, and updating data. MongoDB was used as the
|
||||
* database for the application.</p>
|
||||
*
|
||||
* <p>Developer/Tester is able to choose whether the application should use MongoDB as its
|
||||
* 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;
|
||||
private static MongoDatabase db;
|
||||
private static boolean useMongoDB;
|
||||
|
||||
private static Map<String, UserAccount> virtualDB;
|
||||
|
||||
private DbManager() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Create DB.
|
||||
*/
|
||||
public static void createVirtualDb() {
|
||||
useMongoDB = false;
|
||||
virtualDB = new HashMap<>();
|
||||
}
|
||||
|
||||
/**
|
||||
* Connect to DB.
|
||||
*/
|
||||
public static void connect() throws ParseException {
|
||||
useMongoDB = true;
|
||||
mongoClient = new MongoClient();
|
||||
db = mongoClient.getDatabase("test");
|
||||
}
|
||||
|
||||
/**
|
||||
* Read user account from DB.
|
||||
*/
|
||||
public static UserAccount readFromDb(String userId) {
|
||||
if (!useMongoDB) {
|
||||
if (virtualDB.containsKey(userId)) {
|
||||
return virtualDB.get(userId);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
if (db == null) {
|
||||
try {
|
||||
connect();
|
||||
} catch (ParseException e) {
|
||||
LOGGER.error("Error connecting to MongoDB", e);
|
||||
}
|
||||
}
|
||||
var iterable = db
|
||||
.getCollection(CachingConstants.USER_ACCOUNT)
|
||||
.find(new Document(CachingConstants.USER_ID, userId));
|
||||
if (iterable == null) {
|
||||
return null;
|
||||
}
|
||||
Document doc = iterable.first();
|
||||
String userName = doc.getString(CachingConstants.USER_NAME);
|
||||
String appInfo = doc.getString(CachingConstants.ADD_INFO);
|
||||
return new UserAccount(userId, userName, appInfo);
|
||||
}
|
||||
|
||||
/**
|
||||
* Write user account to DB.
|
||||
*/
|
||||
public static void writeToDb(UserAccount userAccount) {
|
||||
if (!useMongoDB) {
|
||||
virtualDB.put(userAccount.getUserId(), userAccount);
|
||||
return;
|
||||
}
|
||||
if (db == null) {
|
||||
try {
|
||||
connect();
|
||||
} catch (ParseException e) {
|
||||
LOGGER.error("Error connecting to MongoDB", e);
|
||||
}
|
||||
}
|
||||
db.getCollection(CachingConstants.USER_ACCOUNT).insertOne(
|
||||
new Document(CachingConstants.USER_ID, userAccount.getUserId())
|
||||
.append(CachingConstants.USER_NAME, userAccount.getUserName())
|
||||
.append(CachingConstants.ADD_INFO, userAccount.getAdditionalInfo())
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update DB.
|
||||
*/
|
||||
public static void updateDb(UserAccount userAccount) {
|
||||
if (!useMongoDB) {
|
||||
virtualDB.put(userAccount.getUserId(), userAccount);
|
||||
return;
|
||||
}
|
||||
if (db == null) {
|
||||
try {
|
||||
connect();
|
||||
} catch (ParseException e) {
|
||||
LOGGER.error("Error connecting to MongoDB", e);
|
||||
}
|
||||
}
|
||||
db.getCollection(CachingConstants.USER_ACCOUNT).updateOne(
|
||||
new Document(CachingConstants.USER_ID, userAccount.getUserId()),
|
||||
new Document("$set", new Document(CachingConstants.USER_NAME, userAccount.getUserName())
|
||||
.append(CachingConstants.ADD_INFO, userAccount.getAdditionalInfo())));
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert data into DB if it does not exist. Else, update it.
|
||||
*/
|
||||
public static void upsertDb(UserAccount userAccount) {
|
||||
if (!useMongoDB) {
|
||||
virtualDB.put(userAccount.getUserId(), userAccount);
|
||||
return;
|
||||
}
|
||||
if (db == null) {
|
||||
try {
|
||||
connect();
|
||||
} catch (ParseException e) {
|
||||
LOGGER.error("Error connecting to MongoDB", e);
|
||||
}
|
||||
}
|
||||
db.getCollection(CachingConstants.USER_ACCOUNT).updateOne(
|
||||
new Document(CachingConstants.USER_ID, userAccount.getUserId()),
|
||||
new Document("$set",
|
||||
new Document(CachingConstants.USER_ID, userAccount.getUserId())
|
||||
.append(CachingConstants.USER_NAME, userAccount.getUserName())
|
||||
.append(CachingConstants.ADD_INFO, userAccount.getAdditionalInfo())
|
||||
),
|
||||
new UpdateOptions().upsert(true)
|
||||
);
|
||||
}
|
||||
}
|
@ -29,41 +29,83 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
|
||||
/**
|
||||
* Data structure/implementation of the application's cache. The data structure consists of a hash
|
||||
* table attached with a doubly linked-list. The linked-list helps in capturing and maintaining the
|
||||
* LRU data in the cache. When a 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.
|
||||
* Data structure/implementation of the application's cache. The data structure
|
||||
* consists of a hash table attached with a doubly linked-list. The linked-list
|
||||
* helps in capturing and maintaining the LRU data in the cache. When a 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.
|
||||
*/
|
||||
@Slf4j
|
||||
public class LruCache {
|
||||
|
||||
/**
|
||||
* Static class Node.
|
||||
*/
|
||||
static class Node {
|
||||
String userId;
|
||||
UserAccount userAccount;
|
||||
Node previous;
|
||||
Node next;
|
||||
/**
|
||||
* user id.
|
||||
*/
|
||||
private final String userId;
|
||||
/**
|
||||
* User Account.
|
||||
*/
|
||||
private UserAccount userAccount;
|
||||
/**
|
||||
* previous.
|
||||
*/
|
||||
private Node previous;
|
||||
/**
|
||||
* next.
|
||||
*/
|
||||
private Node next;
|
||||
|
||||
public Node(String userId, UserAccount userAccount) {
|
||||
this.userId = userId;
|
||||
this.userAccount = userAccount;
|
||||
/**
|
||||
* Node definition.
|
||||
*
|
||||
* @param id String
|
||||
* @param account {@link UserAccount}
|
||||
*/
|
||||
Node(final String id, final UserAccount account) {
|
||||
this.userId = id;
|
||||
this.userAccount = account;
|
||||
}
|
||||
}
|
||||
|
||||
int capacity;
|
||||
Map<String, Node> cache = new HashMap<>();
|
||||
Node head;
|
||||
Node end;
|
||||
/**
|
||||
* Capacity of Cache.
|
||||
*/
|
||||
private int capacity;
|
||||
/**
|
||||
* Cache {@link HashMap}.
|
||||
*/
|
||||
private Map<String, Node> cache = new HashMap<>();
|
||||
/**
|
||||
* Head.
|
||||
*/
|
||||
private Node head;
|
||||
/**
|
||||
* End.
|
||||
*/
|
||||
private Node end;
|
||||
|
||||
public LruCache(int capacity) {
|
||||
this.capacity = capacity;
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param cap Integer.
|
||||
*/
|
||||
public LruCache(final int cap) {
|
||||
this.capacity = cap;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get user account.
|
||||
*
|
||||
* @param userId String
|
||||
* @return {@link UserAccount}
|
||||
*/
|
||||
public UserAccount get(String userId) {
|
||||
public UserAccount get(final String userId) {
|
||||
if (cache.containsKey(userId)) {
|
||||
var node = cache.get(userId);
|
||||
remove(node);
|
||||
@ -75,8 +117,10 @@ public class LruCache {
|
||||
|
||||
/**
|
||||
* Remove node from linked list.
|
||||
*
|
||||
* @param node {@link Node}
|
||||
*/
|
||||
public void remove(Node node) {
|
||||
public void remove(final Node node) {
|
||||
if (node.previous != null) {
|
||||
node.previous.next = node.next;
|
||||
} else {
|
||||
@ -91,8 +135,10 @@ public class LruCache {
|
||||
|
||||
/**
|
||||
* Move node to the front of the list.
|
||||
*
|
||||
* @param node {@link Node}
|
||||
*/
|
||||
public void setHead(Node node) {
|
||||
public void setHead(final Node node) {
|
||||
node.next = head;
|
||||
node.previous = null;
|
||||
if (head != null) {
|
||||
@ -106,8 +152,11 @@ public class LruCache {
|
||||
|
||||
/**
|
||||
* Set user account.
|
||||
*
|
||||
* @param userAccount {@link UserAccount}
|
||||
* @param userId {@link String}
|
||||
*/
|
||||
public void set(String userId, UserAccount userAccount) {
|
||||
public void set(final String userId, final UserAccount userAccount) {
|
||||
if (cache.containsKey(userId)) {
|
||||
var old = cache.get(userId);
|
||||
old.userAccount = userAccount;
|
||||
@ -127,25 +176,43 @@ public class LruCache {
|
||||
}
|
||||
}
|
||||
|
||||
public boolean contains(String userId) {
|
||||
/**
|
||||
* Check if Cache contains the userId.
|
||||
*
|
||||
* @param userId {@link String}
|
||||
* @return boolean
|
||||
*/
|
||||
public boolean contains(final String userId) {
|
||||
return cache.containsKey(userId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Invalidate cache for user.
|
||||
*
|
||||
* @param userId {@link String}
|
||||
*/
|
||||
public void invalidate(String userId) {
|
||||
public void invalidate(final String userId) {
|
||||
var toBeRemoved = cache.remove(userId);
|
||||
if (toBeRemoved != null) {
|
||||
LOGGER.info("# {} has been updated! Removing older version from cache...", userId);
|
||||
LOGGER.info("# {} has been updated! "
|
||||
+ "Removing older version from cache...", userId);
|
||||
remove(toBeRemoved);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the cache is full.
|
||||
* @return boolean
|
||||
*/
|
||||
public boolean isFull() {
|
||||
return cache.size() >= capacity;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get LRU data.
|
||||
*
|
||||
* @return {@link UserAccount}
|
||||
*/
|
||||
public UserAccount getLruData() {
|
||||
return end.userAccount;
|
||||
}
|
||||
@ -161,6 +228,8 @@ public class LruCache {
|
||||
|
||||
/**
|
||||
* Returns cache data in list form.
|
||||
*
|
||||
* @return {@link List}
|
||||
*/
|
||||
public List<UserAccount> getCacheDataInListForm() {
|
||||
var listOfCacheData = new ArrayList<UserAccount>();
|
||||
@ -174,10 +243,14 @@ public class LruCache {
|
||||
|
||||
/**
|
||||
* Set cache capacity.
|
||||
*
|
||||
* @param newCapacity int
|
||||
*/
|
||||
public void setCapacity(int newCapacity) {
|
||||
public void setCapacity(final int newCapacity) {
|
||||
if (capacity > newCapacity) {
|
||||
clear(); // Behavior can be modified to accommodate for decrease in cache size. For now, we'll
|
||||
// Behavior can be modified to accommodate
|
||||
// for decrease in cache size. For now, we'll
|
||||
clear();
|
||||
// just clear the cache.
|
||||
} else {
|
||||
this.capacity = newCapacity;
|
||||
|
@ -24,19 +24,28 @@
|
||||
package com.iluwatar.caching;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.ToString;
|
||||
|
||||
/**
|
||||
* Entity class (stored in cache and DB) used in the application.
|
||||
*/
|
||||
@Setter
|
||||
@Getter
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@ToString
|
||||
@EqualsAndHashCode
|
||||
public class UserAccount {
|
||||
/**
|
||||
* User Id.
|
||||
*/
|
||||
private String userId;
|
||||
/**
|
||||
* User Name.
|
||||
*/
|
||||
private String userName;
|
||||
/**
|
||||
* Additional Info.
|
||||
*/
|
||||
private String additionalInfo;
|
||||
}
|
||||
|
@ -26,11 +26,27 @@ package com.iluwatar.caching.constants;
|
||||
/**
|
||||
* Constant class for defining constants.
|
||||
*/
|
||||
public class CachingConstants {
|
||||
|
||||
public final class CachingConstants {
|
||||
/**
|
||||
* User Account.
|
||||
*/
|
||||
public static final String USER_ACCOUNT = "user_accounts";
|
||||
/**
|
||||
* User ID.
|
||||
*/
|
||||
public static final String USER_ID = "userID";
|
||||
/**
|
||||
* User Name.
|
||||
*/
|
||||
public static final String USER_NAME = "userName";
|
||||
/**
|
||||
* Additional Info.
|
||||
*/
|
||||
public static final String ADD_INFO = "additionalInfo";
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*/
|
||||
private CachingConstants() {
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,4 @@
|
||||
/**
|
||||
* Constants.
|
||||
*/
|
||||
package com.iluwatar.caching.constants;
|
@ -0,0 +1,52 @@
|
||||
package com.iluwatar.caching.database;
|
||||
|
||||
import com.iluwatar.caching.UserAccount;
|
||||
|
||||
/**
|
||||
* <p>DBManager handles the communication with the underlying data store i.e.
|
||||
* Database. It contains the implemented methods for querying, inserting,
|
||||
* and updating data. MongoDB was used as the database for the application.</p>
|
||||
*/
|
||||
public interface DbManager {
|
||||
/**
|
||||
* Connect to DB.
|
||||
*/
|
||||
void connect();
|
||||
|
||||
/**
|
||||
* Disconnect from DB.
|
||||
*/
|
||||
void disconnect();
|
||||
|
||||
/**
|
||||
* Read from DB.
|
||||
*
|
||||
* @param userId {@link String}
|
||||
* @return {@link UserAccount}
|
||||
*/
|
||||
UserAccount readFromDb(String userId);
|
||||
|
||||
/**
|
||||
* Write to DB.
|
||||
*
|
||||
* @param userAccount {@link UserAccount}
|
||||
* @return {@link UserAccount}
|
||||
*/
|
||||
UserAccount writeToDb(UserAccount userAccount);
|
||||
|
||||
/**
|
||||
* Update record.
|
||||
*
|
||||
* @param userAccount {@link UserAccount}
|
||||
* @return {@link UserAccount}
|
||||
*/
|
||||
UserAccount updateDb(UserAccount userAccount);
|
||||
|
||||
/**
|
||||
* Update record or Insert if not exists.
|
||||
*
|
||||
* @param userAccount {@link UserAccount}
|
||||
* @return {@link UserAccount}
|
||||
*/
|
||||
UserAccount upsertDb(UserAccount userAccount);
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
package com.iluwatar.caching.database;
|
||||
|
||||
/**
|
||||
* Creates the database connection accroding the input parameter.
|
||||
*/
|
||||
public final class DbManagerFactory {
|
||||
/**
|
||||
* Private constructor.
|
||||
*/
|
||||
private DbManagerFactory() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Init database.
|
||||
*
|
||||
* @param isMongo boolean
|
||||
* @return {@link DbManager}
|
||||
*/
|
||||
public static DbManager initDb(final boolean isMongo) {
|
||||
if (isMongo) {
|
||||
return new MongoDb();
|
||||
}
|
||||
return new VirtualDb();
|
||||
}
|
||||
}
|
128
caching/src/main/java/com/iluwatar/caching/database/MongoDb.java
Normal file
128
caching/src/main/java/com/iluwatar/caching/database/MongoDb.java
Normal file
@ -0,0 +1,128 @@
|
||||
package com.iluwatar.caching.database;
|
||||
|
||||
import static com.iluwatar.caching.constants.CachingConstants.ADD_INFO;
|
||||
import static com.iluwatar.caching.constants.CachingConstants.USER_ACCOUNT;
|
||||
import static com.iluwatar.caching.constants.CachingConstants.USER_ID;
|
||||
import static com.iluwatar.caching.constants.CachingConstants.USER_NAME;
|
||||
|
||||
import com.iluwatar.caching.UserAccount;
|
||||
import com.iluwatar.caching.constants.CachingConstants;
|
||||
import com.mongodb.MongoClient;
|
||||
import com.mongodb.MongoClientOptions;
|
||||
import com.mongodb.MongoCredential;
|
||||
import com.mongodb.ServerAddress;
|
||||
import com.mongodb.client.MongoDatabase;
|
||||
import com.mongodb.client.model.UpdateOptions;
|
||||
import java.util.List;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.bson.Document;
|
||||
|
||||
/**
|
||||
* Implementation of DatabaseManager.
|
||||
* implements base methods to work with MongoDb.
|
||||
*/
|
||||
@Slf4j
|
||||
public class MongoDb implements DbManager {
|
||||
private static final String DATABASE_NAME = "admin";
|
||||
private static final String MONGO_USER = "root";
|
||||
private static final String MONGO_PASSWORD = "rootpassword";
|
||||
private MongoClient client;
|
||||
private MongoDatabase db;
|
||||
|
||||
/**
|
||||
* Connect to Db. Check th connection
|
||||
*/
|
||||
@Override
|
||||
public void connect() {
|
||||
MongoCredential mongoCredential = MongoCredential.createCredential(MONGO_USER,
|
||||
DATABASE_NAME,
|
||||
MONGO_PASSWORD.toCharArray());
|
||||
MongoClientOptions options = MongoClientOptions.builder().build();
|
||||
client = new MongoClient(new ServerAddress(), List.of(mongoCredential), options);
|
||||
db = client.getDatabase(DATABASE_NAME);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void disconnect() {
|
||||
client.close();
|
||||
}
|
||||
|
||||
/**
|
||||
* Read data from DB.
|
||||
*
|
||||
* @param userId {@link String}
|
||||
* @return {@link UserAccount}
|
||||
*/
|
||||
@Override
|
||||
public UserAccount readFromDb(final String userId) {
|
||||
var iterable = db
|
||||
.getCollection(CachingConstants.USER_ACCOUNT)
|
||||
.find(new Document(USER_ID, userId));
|
||||
if (iterable.first() == null) {
|
||||
return null;
|
||||
}
|
||||
Document doc = iterable.first();
|
||||
if (doc != null) {
|
||||
String userName = doc.getString(USER_NAME);
|
||||
String appInfo = doc.getString(ADD_INFO);
|
||||
return new UserAccount(userId, userName, appInfo);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Write data to DB.
|
||||
*
|
||||
* @param userAccount {@link UserAccount}
|
||||
* @return {@link UserAccount}
|
||||
*/
|
||||
@Override
|
||||
public UserAccount writeToDb(final UserAccount userAccount) {
|
||||
db.getCollection(USER_ACCOUNT).insertOne(
|
||||
new Document(USER_ID, userAccount.getUserId())
|
||||
.append(USER_NAME, userAccount.getUserName())
|
||||
.append(ADD_INFO, userAccount.getAdditionalInfo())
|
||||
);
|
||||
return userAccount;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update DB.
|
||||
*
|
||||
* @param userAccount {@link UserAccount}
|
||||
* @return {@link UserAccount}
|
||||
*/
|
||||
@Override
|
||||
public UserAccount updateDb(final UserAccount userAccount) {
|
||||
Document id = new Document(USER_ID, userAccount.getUserId());
|
||||
Document dataSet = new Document(USER_NAME, userAccount.getUserName())
|
||||
.append(ADD_INFO, userAccount.getAdditionalInfo());
|
||||
db.getCollection(CachingConstants.USER_ACCOUNT)
|
||||
.updateOne(id, new Document("$set", dataSet));
|
||||
return userAccount;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update data if exists.
|
||||
*
|
||||
* @param userAccount {@link UserAccount}
|
||||
* @return {@link UserAccount}
|
||||
*/
|
||||
@Override
|
||||
public UserAccount upsertDb(final UserAccount userAccount) {
|
||||
String userId = userAccount.getUserId();
|
||||
String userName = userAccount.getUserName();
|
||||
String additionalInfo = userAccount.getAdditionalInfo();
|
||||
db.getCollection(CachingConstants.USER_ACCOUNT).updateOne(
|
||||
new Document(USER_ID, userId),
|
||||
new Document("$set",
|
||||
new Document(USER_ID, userId)
|
||||
.append(USER_NAME, userName)
|
||||
.append(ADD_INFO, additionalInfo)
|
||||
),
|
||||
new UpdateOptions().upsert(true)
|
||||
);
|
||||
return userAccount;
|
||||
}
|
||||
}
|
@ -0,0 +1,78 @@
|
||||
package com.iluwatar.caching.database;
|
||||
|
||||
import com.iluwatar.caching.UserAccount;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Implementation of DatabaseManager.
|
||||
* implements base methods to work with hashMap as database.
|
||||
*/
|
||||
public class VirtualDb implements DbManager {
|
||||
/**
|
||||
* Virtual DataBase.
|
||||
*/
|
||||
private Map<String, UserAccount> db;
|
||||
|
||||
/**
|
||||
* Creates new HashMap.
|
||||
*/
|
||||
@Override
|
||||
public void connect() {
|
||||
db = new HashMap<>();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void disconnect() {
|
||||
db = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Read from Db.
|
||||
*
|
||||
* @param userId {@link String}
|
||||
* @return {@link UserAccount}
|
||||
*/
|
||||
@Override
|
||||
public UserAccount readFromDb(final String userId) {
|
||||
if (db.containsKey(userId)) {
|
||||
return db.get(userId);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Write to DB.
|
||||
*
|
||||
* @param userAccount {@link UserAccount}
|
||||
* @return {@link UserAccount}
|
||||
*/
|
||||
@Override
|
||||
public UserAccount writeToDb(final UserAccount userAccount) {
|
||||
db.put(userAccount.getUserId(), userAccount);
|
||||
return userAccount;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update reecord in DB.
|
||||
*
|
||||
* @param userAccount {@link UserAccount}
|
||||
* @return {@link UserAccount}
|
||||
*/
|
||||
@Override
|
||||
public UserAccount updateDb(final UserAccount userAccount) {
|
||||
return writeToDb(userAccount);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update.
|
||||
*
|
||||
* @param userAccount {@link UserAccount}
|
||||
* @return {@link UserAccount}
|
||||
*/
|
||||
@Override
|
||||
public UserAccount upsertDb(final UserAccount userAccount) {
|
||||
return updateDb(userAccount);
|
||||
}
|
||||
}
|
@ -0,0 +1,4 @@
|
||||
/**
|
||||
* Database classes.
|
||||
*/
|
||||
package com.iluwatar.caching.database;
|
20
caching/src/main/java/com/iluwatar/caching/package-info.java
Normal file
20
caching/src/main/java/com/iluwatar/caching/package-info.java
Normal file
@ -0,0 +1,20 @@
|
||||
/**
|
||||
* 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.caching;
|
@ -25,25 +25,21 @@ package com.iluwatar.caching;
|
||||
|
||||
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.
|
||||
*/
|
||||
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}
|
||||
* throws an exception.
|
||||
*/
|
||||
|
||||
@Test
|
||||
void shouldExecuteApplicationWithoutException() {
|
||||
|
||||
assertDoesNotThrow(() -> App.main(new String[]{}));
|
||||
}
|
||||
}
|
||||
|
@ -23,11 +23,11 @@
|
||||
|
||||
package com.iluwatar.caching;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
/**
|
||||
* Application test
|
||||
*/
|
||||
@ -43,32 +43,30 @@ class CachingTest {
|
||||
// to avoid Maven compilation errors. Set flag to true to run the
|
||||
// tests with MongoDB (provided that MongoDB is installed and socket
|
||||
// connection is open).
|
||||
AppManager.initDb(false);
|
||||
AppManager.initCacheCapacity(3);
|
||||
app = new App();
|
||||
app = new App(false);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testReadAndWriteThroughStrategy() {
|
||||
assertNotNull(app);
|
||||
assertNotNull(app);
|
||||
app.useReadAndWriteThroughStrategy();
|
||||
}
|
||||
|
||||
@Test
|
||||
void testReadThroughAndWriteAroundStrategy() {
|
||||
assertNotNull(app);
|
||||
assertNotNull(app);
|
||||
app.useReadThroughAndWriteAroundStrategy();
|
||||
}
|
||||
|
||||
@Test
|
||||
void testReadThroughAndWriteBehindStrategy() {
|
||||
assertNotNull(app);
|
||||
assertNotNull(app);
|
||||
app.useReadThroughAndWriteBehindStrategy();
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCacheAsideStrategy() {
|
||||
assertNotNull(app);
|
||||
assertNotNull(app);
|
||||
app.useCacheAsideStategy();
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,84 @@
|
||||
package com.iluwatar.caching.database;
|
||||
|
||||
import com.iluwatar.caching.UserAccount;
|
||||
import com.iluwatar.caching.constants.CachingConstants;
|
||||
import com.mongodb.client.FindIterable;
|
||||
import com.mongodb.client.MongoCollection;
|
||||
import com.mongodb.client.MongoDatabase;
|
||||
import org.bson.Document;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.internal.util.reflection.Whitebox;
|
||||
|
||||
import static com.iluwatar.caching.constants.CachingConstants.ADD_INFO;
|
||||
import static com.iluwatar.caching.constants.CachingConstants.USER_ID;
|
||||
import static com.iluwatar.caching.constants.CachingConstants.USER_NAME;
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
class MongoDbTest {
|
||||
private static final String ID = "123";
|
||||
private static final String NAME = "Some user";
|
||||
private static final String ADDITIONAL_INFO = "Some app Info";
|
||||
|
||||
@Mock
|
||||
MongoDatabase db;
|
||||
private MongoDb mongoDb = new MongoDb();
|
||||
|
||||
private UserAccount userAccount;
|
||||
|
||||
@BeforeEach
|
||||
void init() {
|
||||
db = mock(MongoDatabase.class);
|
||||
Whitebox.setInternalState(mongoDb, "db", db);
|
||||
userAccount = new UserAccount(ID, NAME, ADDITIONAL_INFO);
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
void connect() {
|
||||
assertDoesNotThrow(() -> mongoDb.connect());
|
||||
}
|
||||
|
||||
@Test
|
||||
void readFromDb() {
|
||||
Document document = new Document(USER_ID, ID)
|
||||
.append(USER_NAME, NAME)
|
||||
.append(ADD_INFO, ADDITIONAL_INFO);
|
||||
MongoCollection<Document> mongoCollection = mock(MongoCollection.class);
|
||||
when(db.getCollection(CachingConstants.USER_ACCOUNT)).thenReturn(mongoCollection);
|
||||
|
||||
FindIterable<Document> findIterable = mock(FindIterable.class);
|
||||
when(mongoCollection.find(any(Document.class))).thenReturn(findIterable);
|
||||
|
||||
when(findIterable.first()).thenReturn(document);
|
||||
|
||||
assertEquals(mongoDb.readFromDb(ID),userAccount);
|
||||
}
|
||||
|
||||
@Test
|
||||
void writeToDb() {
|
||||
MongoCollection<Document> mongoCollection = mock(MongoCollection.class);
|
||||
when(db.getCollection(CachingConstants.USER_ACCOUNT)).thenReturn(mongoCollection);
|
||||
doNothing().when(mongoCollection).insertOne(any(Document.class));
|
||||
assertDoesNotThrow(()-> {mongoDb.writeToDb(userAccount);});
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateDb() {
|
||||
MongoCollection<Document> mongoCollection = mock(MongoCollection.class);
|
||||
when(db.getCollection(CachingConstants.USER_ACCOUNT)).thenReturn(mongoCollection);
|
||||
assertDoesNotThrow(()-> {mongoDb.updateDb(userAccount);});
|
||||
}
|
||||
|
||||
@Test
|
||||
void upsertDb() {
|
||||
MongoCollection<Document> mongoCollection = mock(MongoCollection.class);
|
||||
when(db.getCollection(CachingConstants.USER_ACCOUNT)).thenReturn(mongoCollection);
|
||||
assertDoesNotThrow(()-> {mongoDb.upsertDb(userAccount);});
|
||||
}
|
||||
}
|
@ -1,9 +1,6 @@
|
||||
---
|
||||
layout: pattern
|
||||
title: Callback
|
||||
folder: callback
|
||||
permalink: /patterns/callback/
|
||||
categories: Idiom
|
||||
category: Idiom
|
||||
language: en
|
||||
tags:
|
||||
- Reactive
|
||||
|
@ -1,9 +1,6 @@
|
||||
---
|
||||
layout: pattern
|
||||
title: Chain of responsibility
|
||||
folder: chain-of-responsibility
|
||||
permalink: /patterns/chain-of-responsibility/
|
||||
categories: Behavioral
|
||||
category: Behavioral
|
||||
language: en
|
||||
tags:
|
||||
- Gang of Four
|
||||
|
@ -1,9 +1,6 @@
|
||||
---
|
||||
layout: pattern
|
||||
title: Circuit Breaker
|
||||
folder: circuit-breaker
|
||||
permalink: /patterns/circuit-breaker/
|
||||
categories: Behavioral
|
||||
category: Behavioral
|
||||
language: en
|
||||
tags:
|
||||
- Performance
|
||||
|
@ -1,9 +1,6 @@
|
||||
---
|
||||
layout: pattern
|
||||
title: Static Content Hosting
|
||||
folder: cloud-static-content-hosting
|
||||
permalink: /patterns/cloud-static-content-hosting/
|
||||
categories: Cloud
|
||||
category: Cloud
|
||||
language: en
|
||||
tags:
|
||||
- Cloud distributed
|
||||
|
@ -1,9 +1,6 @@
|
||||
---
|
||||
layout: pattern
|
||||
title: Collection Pipeline
|
||||
folder: collection-pipeline
|
||||
permalink: /patterns/collection-pipeline/
|
||||
categories: Functional
|
||||
category: Functional
|
||||
language: en
|
||||
tags:
|
||||
- Reactive
|
||||
|
@ -1,9 +1,6 @@
|
||||
---
|
||||
layout: pattern
|
||||
title: Combinator
|
||||
folder: combinator
|
||||
permalink: /patterns/combinator/
|
||||
categories: Idiom
|
||||
category: Idiom
|
||||
language: en
|
||||
tags:
|
||||
- Reactive
|
||||
|
@ -1,9 +1,6 @@
|
||||
---
|
||||
layout: pattern
|
||||
---
|
||||
title: Command
|
||||
folder: command
|
||||
permalink: /patterns/command/
|
||||
categories: Behavioral
|
||||
category: Behavioral
|
||||
language: en
|
||||
tags:
|
||||
- Gang of Four
|
||||
|
@ -1,9 +1,6 @@
|
||||
---
|
||||
layout: pattern
|
||||
title: Commander
|
||||
folder: commander
|
||||
permalink: /patterns/commander/
|
||||
categories: Concurrency
|
||||
category: Concurrency
|
||||
language: en
|
||||
tags:
|
||||
- Cloud distributed
|
||||
@ -25,4 +22,4 @@ We need a mechanism in place which can handle these kinds of situations. We have
|
||||
|
||||
## Credits
|
||||
|
||||
* [https://www.grahamlea.com/2016/08/distributed-transactions-microservices-icebergs/]
|
||||
* [Distributed Transactions: The Icebergs of Microservices](https://www.grahamlea.com/2016/08/distributed-transactions-microservices-icebergs/)
|
||||
|
@ -1,9 +1,6 @@
|
||||
---
|
||||
layout: pattern
|
||||
title: Composite Entity
|
||||
folder: composite-entity
|
||||
permalink: /patterns/composite-entity/
|
||||
categories: Structural
|
||||
category: Structural
|
||||
language: en
|
||||
tags:
|
||||
- Enterprise Integration Pattern
|
||||
|
@ -1,9 +1,6 @@
|
||||
---
|
||||
layout: pattern
|
||||
title: Composite
|
||||
folder: composite
|
||||
permalink: /patterns/composite/
|
||||
categories: Structural
|
||||
category: Structural
|
||||
language: en
|
||||
tags:
|
||||
- Gang of Four
|
||||
|
@ -1,9 +1,6 @@
|
||||
---
|
||||
layout: pattern
|
||||
title: Converter
|
||||
folder: converter
|
||||
permalink: /patterns/converter/
|
||||
categories: Creational
|
||||
category: Creational
|
||||
language: en
|
||||
tags:
|
||||
- Decoupling
|
||||
|
@ -1,9 +1,6 @@
|
||||
---
|
||||
layout: pattern
|
||||
title: CQRS
|
||||
folder: cqrs
|
||||
permalink: /patterns/cqrs/
|
||||
categories: Architectural
|
||||
category: Architectural
|
||||
language: en
|
||||
tags:
|
||||
- Performance
|
||||
|
@ -1,9 +1,6 @@
|
||||
---
|
||||
layout: pattern
|
||||
title: Data Access Object
|
||||
folder: dao
|
||||
permalink: /patterns/dao/
|
||||
categories: Architectural
|
||||
category: Architectural
|
||||
language: en
|
||||
tags:
|
||||
- Data access
|
||||
|
@ -1,10 +1,6 @@
|
||||
---
|
||||
layout: pattern
|
||||
title: Data Bus
|
||||
folder: data-bus
|
||||
permalink: /patterns/data-bus/
|
||||
|
||||
categories: Architectural
|
||||
category: Architectural
|
||||
language: en
|
||||
tags:
|
||||
- Decoupling
|
||||
|
@ -46,7 +46,7 @@ class DataBusTest {
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
MockitoAnnotations.openMocks(this);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -1,9 +1,6 @@
|
||||
---
|
||||
layout: pattern
|
||||
title: Data Locality
|
||||
folder: data-locality
|
||||
permalink: /patterns/data-locality/
|
||||
categories: Behavioral
|
||||
category: Behavioral
|
||||
language: en
|
||||
tags:
|
||||
- Game programming
|
||||
|
@ -1,9 +1,6 @@
|
||||
---
|
||||
layout: pattern
|
||||
title: Data Mapper
|
||||
folder: data-mapper
|
||||
permalink: /patterns/data-mapper/
|
||||
categories: Architectural
|
||||
category: Architectural
|
||||
language: en
|
||||
tags:
|
||||
- Decoupling
|
||||
|
@ -1,9 +1,6 @@
|
||||
---
|
||||
layout: pattern
|
||||
title: Data Transfer Object
|
||||
folder: data-transfer-object
|
||||
permalink: /patterns/data-transfer-object/
|
||||
categories: Architectural
|
||||
category: Architectural
|
||||
language: en
|
||||
tags:
|
||||
- Performance
|
||||
|
@ -1,9 +1,6 @@
|
||||
---
|
||||
layout: pattern
|
||||
title: Decorator
|
||||
folder: decorator
|
||||
permalink: /patterns/decorator/
|
||||
categories: Structural
|
||||
category: Structural
|
||||
language: en
|
||||
tags:
|
||||
- Gang of Four
|
||||
|
@ -1,9 +1,6 @@
|
||||
---
|
||||
layout: pattern
|
||||
title: Delegation
|
||||
folder: delegation
|
||||
permalink: /patterns/delegation/
|
||||
categories: Structural
|
||||
category: Structural
|
||||
language: en
|
||||
tags:
|
||||
- Decoupling
|
||||
|
@ -1,9 +1,6 @@
|
||||
---
|
||||
layout: pattern
|
||||
title: Dependency Injection
|
||||
folder: dependency-injection
|
||||
permalink: /patterns/dependency-injection/
|
||||
categories: Creational
|
||||
category: Creational
|
||||
language: en
|
||||
tags:
|
||||
- Decoupling
|
||||
@ -103,4 +100,4 @@ Use the Dependency Injection pattern when:
|
||||
* [Dependency Injection Principles, Practices, and Patterns](https://www.amazon.com/gp/product/161729473X/ref=as_li_qf_asin_il_tl?ie=UTF8&tag=javadesignpat-20&creative=9325&linkCode=as2&creativeASIN=161729473X&linkId=57079257a5c7d33755493802f3b884bd)
|
||||
* [Clean Code: A Handbook of Agile Software Craftsmanship](https://www.amazon.com/gp/product/0132350882/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0132350882&linkCode=as2&tag=javadesignpat-20&linkId=2c390d89cc9e61c01b9e7005c7842871)
|
||||
* [Java 9 Dependency Injection: Write loosely coupled code with Spring 5 and Guice](https://www.amazon.com/gp/product/1788296257/ref=as_li_tl?ie=UTF8&tag=javadesignpat-20&camp=1789&creative=9325&linkCode=as2&creativeASIN=1788296257&linkId=4e9137a3bf722a8b5b156cce1eec0fc1)
|
||||
* [Google Guice Tutorial: Open source Java based dependency injection framework](https://www.amazon.com/gp/product/B083P7DZ8M/ref=as_li_tl?ie=UTF8&tag=javadesignpat-20&camp=1789&creative=9325&linkCode=as2&creativeASIN=B083P7DZ8M&linkId=04f0f902c877921e45215b624a124bfe)
|
||||
* [Google Guice: Agile Lightweight Dependency Injection Framework](https://www.amazon.com/gp/product/1590599977/ref=as_li_qf_asin_il_tl?ie=UTF8&tag=javadesignpat-20&creative=9325&linkCode=as2&creativeASIN=1590599977&linkId=3b10c90b7ba480a1b7777ff38000f956)
|
||||
|
@ -1,9 +1,6 @@
|
||||
---
|
||||
layout: pattern
|
||||
title: Dirty Flag
|
||||
folder: dirty-flag
|
||||
permalink: /patterns/dirty-flag/
|
||||
categories: Behavioral
|
||||
category: Behavioral
|
||||
language: en
|
||||
tags:
|
||||
- Game programming
|
||||
|
@ -1,14 +1,15 @@
|
||||
---
|
||||
layout: pattern
|
||||
title: Domain Model
|
||||
folder: domain-model
|
||||
permalink: /patterns/domain-model/
|
||||
categories: Architectural
|
||||
category: Architectural
|
||||
language: en
|
||||
tags:
|
||||
- Domain
|
||||
---
|
||||
|
||||
## Intent
|
||||
|
||||
Domain model pattern provides an object-oriented way of dealing with complicated logic. Instead of having one procedure that handles all business logic for a user action there are multiple objects and each of them handles a slice of domain logic that is relevant to it.
|
||||
|
||||
## Explanation
|
||||
|
||||
Real world example
|
||||
|
@ -1,9 +1,6 @@
|
||||
---
|
||||
layout: pattern
|
||||
title: Double Buffer
|
||||
folder: double-buffer
|
||||
permalink: /patterns/double-buffer/
|
||||
categories: Behavioral
|
||||
category: Behavioral
|
||||
language: en
|
||||
tags:
|
||||
- Performance
|
||||
|
@ -1,9 +1,6 @@
|
||||
---
|
||||
layout: pattern
|
||||
title: Double Checked Locking
|
||||
folder: double-checked-locking
|
||||
permalink: /patterns/double-checked-locking/
|
||||
categories: Idiom
|
||||
category: Idiom
|
||||
language: en
|
||||
tags:
|
||||
- Performance
|
||||
|
@ -1,9 +1,6 @@
|
||||
---
|
||||
layout: pattern
|
||||
title: Double Dispatch
|
||||
folder: double-dispatch
|
||||
permalink: /patterns/double-dispatch/
|
||||
categories: Idiom
|
||||
category: Idiom
|
||||
language: en
|
||||
tags:
|
||||
- Extensibility
|
||||
|
@ -1,9 +1,6 @@
|
||||
---
|
||||
layout: pattern
|
||||
title: EIP Aggregator
|
||||
folder: eip-aggregator
|
||||
permalink: /patterns/eip-aggregator/
|
||||
categories: Integration
|
||||
category: Integration
|
||||
language: en
|
||||
tags:
|
||||
- Enterprise Integration Pattern
|
||||
|
@ -1,9 +1,6 @@
|
||||
---
|
||||
layout: pattern
|
||||
title: EIP Message Channel
|
||||
folder: eip-message-channel
|
||||
permalink: /patterns/eip-message-channel/
|
||||
categories: Integration
|
||||
category: Integration
|
||||
language: en
|
||||
tags:
|
||||
- Enterprise Integration Pattern
|
||||
|
@ -1,9 +1,6 @@
|
||||
---
|
||||
layout: pattern
|
||||
title: EIP Publish Subscribe
|
||||
folder: eip-publish-subscribe
|
||||
permalink: /patterns/eip-publish-subscribe/
|
||||
categories: Integration
|
||||
category: Integration
|
||||
language: en
|
||||
tags:
|
||||
- Enterprise Integration Pattern
|
||||
|
@ -1,9 +1,6 @@
|
||||
---
|
||||
layout: pattern
|
||||
title: EIP Splitter
|
||||
folder: eip-splitter
|
||||
permalink: /patterns/eip-splitter/
|
||||
categories: Integration
|
||||
category: Integration
|
||||
language: en
|
||||
tags:
|
||||
- Enterprise Integration Pattern
|
||||
|
@ -1,9 +1,6 @@
|
||||
---
|
||||
layout: pattern
|
||||
title: EIP Wire Tap
|
||||
folder: eip-wire-tap
|
||||
permalink: /patterns/eip-wire-tap/
|
||||
categories: Integration
|
||||
category: Integration
|
||||
language: en
|
||||
tags:
|
||||
- Enterprise Integration Pattern
|
||||
|
@ -1,9 +1,6 @@
|
||||
---
|
||||
layout: pattern
|
||||
title: Event Aggregator
|
||||
folder: event-aggregator
|
||||
permalink: /patterns/event-aggregator/
|
||||
categories: Structural
|
||||
category: Structural
|
||||
language: en
|
||||
tags:
|
||||
- Reactive
|
||||
|
@ -1,9 +1,6 @@
|
||||
---
|
||||
layout: pattern
|
||||
title: Event-based Asynchronous
|
||||
folder: event-asynchronous
|
||||
permalink: /patterns/event-asynchronous/
|
||||
categories: Concurrency
|
||||
category: Concurrency
|
||||
language: en
|
||||
tags:
|
||||
- Reactive
|
||||
|
@ -1,9 +1,6 @@
|
||||
---
|
||||
layout: pattern
|
||||
title: Event Driven Architecture
|
||||
folder: event-driven-architecture
|
||||
permalink: /patterns/event-driven-architecture/
|
||||
categories: Architectural
|
||||
category: Architectural
|
||||
language: en
|
||||
tags:
|
||||
- Reactive
|
||||
@ -24,7 +21,6 @@ Use an Event-driven architecture when
|
||||
|
||||
## Real world examples
|
||||
|
||||
* SendGrid, an email API, sends events whenever an email is processed, delivered, opened etc... (https://sendgrid.com/docs/API_Reference/Webhooks/event.html)
|
||||
* Chargify, a billing API, exposes payment activity through various events (https://docs.chargify.com/api-events)
|
||||
* Amazon's AWS Lambda, lets you execute code in response to events such as changes to Amazon S3 buckets, updates to an Amazon DynamoDB table, or custom events generated by your applications or devices. (https://aws.amazon.com/lambda)
|
||||
* MySQL runs triggers based on events such as inserts and update events happening on database tables.
|
||||
@ -32,6 +28,6 @@ Use an Event-driven architecture when
|
||||
## Credits
|
||||
|
||||
* [Event-driven architecture - Wikipedia](https://en.wikipedia.org/wiki/Event-driven_architecture)
|
||||
* [Fundamental Components of an Event-Driven Architecture](http://giocc.com/fundamental-components-of-an-event-driven-architecture.html)
|
||||
* [What is an Event-Driven Architecture](https://aws.amazon.com/event-driven-architecture/)
|
||||
* [Real World Applications/Event Driven Applications](https://wiki.haskell.org/Real_World_Applications/Event_Driven_Applications)
|
||||
* [Event-driven architecture definition](http://searchsoa.techtarget.com/definition/event-driven-architecture)
|
||||
|
@ -1,9 +1,6 @@
|
||||
---
|
||||
layout: pattern
|
||||
title: Event Queue
|
||||
folder: event-queue
|
||||
permalink: /patterns/event-queue/
|
||||
categories: Concurrency
|
||||
category: Concurrency
|
||||
language: en
|
||||
tags:
|
||||
- Game programming
|
||||
|
@ -1,9 +1,6 @@
|
||||
---
|
||||
layout: pattern
|
||||
title: Event Sourcing
|
||||
folder: event-sourcing
|
||||
permalink: /patterns/event-sourcing/
|
||||
categories: Architectural
|
||||
category: Architectural
|
||||
language: en
|
||||
tags:
|
||||
- Performance
|
||||
|
@ -1,9 +1,6 @@
|
||||
---
|
||||
layout: pattern
|
||||
title: Execute Around
|
||||
folder: execute-around
|
||||
permalink: /patterns/execute-around/
|
||||
categories: Idiom
|
||||
category: Idiom
|
||||
language: en
|
||||
tags:
|
||||
- Extensibility
|
||||
|
@ -1,9 +1,6 @@
|
||||
---
|
||||
layout: pattern
|
||||
title: Extension objects
|
||||
folder: extension-objects
|
||||
permalink: /patterns/extension-objects/
|
||||
categories: Behavioral
|
||||
category: Behavioral
|
||||
language: en
|
||||
tags:
|
||||
- Extensibility
|
||||
|
@ -1,9 +1,6 @@
|
||||
---
|
||||
layout: pattern
|
||||
title: Facade
|
||||
folder: facade
|
||||
permalink: /patterns/facade/
|
||||
categories: Structural
|
||||
category: Structural
|
||||
language: en
|
||||
tags:
|
||||
- Gang Of Four
|
||||
|
@ -1,9 +1,6 @@
|
||||
---
|
||||
layout: pattern
|
||||
title: Factory Kit
|
||||
folder: factory-kit
|
||||
permalink: /patterns/factory-kit/
|
||||
categories: Creational
|
||||
category: Creational
|
||||
language: en
|
||||
tags:
|
||||
- Extensibility
|
||||
|
@ -1,9 +1,6 @@
|
||||
---
|
||||
layout: pattern
|
||||
title: Factory Method
|
||||
folder: factory-method
|
||||
permalink: /patterns/factory-method/
|
||||
categories: Creational
|
||||
category: Creational
|
||||
language: en
|
||||
tags:
|
||||
- Extensibility
|
||||
|
@ -1,9 +1,6 @@
|
||||
---
|
||||
layout: pattern
|
||||
title: Factory
|
||||
folder: factory
|
||||
permalink: /patterns/factory/
|
||||
categories: Creational
|
||||
category: Creational
|
||||
language: en
|
||||
tags:
|
||||
- Gang of Four
|
||||
|
117
fanout-fanin/README.md
Normal file
117
fanout-fanin/README.md
Normal file
@ -0,0 +1,117 @@
|
||||
---
|
||||
title: Fan-Out/Fan-In
|
||||
category: Integration
|
||||
language: en
|
||||
tags:
|
||||
- Microservices
|
||||
---
|
||||
|
||||
## Intent
|
||||
The pattern is used when a source system needs to run one or more long-running processes that will fetch some data.
|
||||
The source will not block itself waiting for the reply. <br> The pattern will run the same function in multiple
|
||||
services or machines to fetch the data. This is equivalent to invoking the function multiple times on different chunks of data.
|
||||
|
||||
## Explanation
|
||||
The FanOut/FanIn service will take in a list of requests and a consumer. Each request might complete at a different time.
|
||||
FanOut/FanIn service will accept the input params and returns the initial system an ID to acknowledge that the pattern
|
||||
service has received the requests. Now the caller will not wait or expect the result in the same connection.
|
||||
|
||||
Meanwhile, the pattern service will invoke the requests that have come. The requests might complete at different time.
|
||||
These requests will be processed in different instances of the same function in different machines or services. As the
|
||||
requests get completed, a callback service everytime is called that transforms the result into a common single object format
|
||||
that gets pushed to a consumer. The caller will be at the other end of the consumer receiving the result.
|
||||
|
||||
**Programmatic Example**
|
||||
|
||||
The implementation provided has a list of numbers and end goal is to square the numbers and add them to a single result.
|
||||
`FanOutFanIn` class receives the list of numbers in the form of list of `SquareNumberRequest` and a `Consumer` instance
|
||||
that collects the results as the requests get over. `SquareNumberRequest` will square the number with a random delay
|
||||
to give the impression of a long-running process that can complete at any time. `Consumer` instance will add the results from
|
||||
different `SquareNumberRequest` that will come random time instances.
|
||||
|
||||
Let's look at `FanOutFanIn` class that fans out the requests in async processes.
|
||||
|
||||
```java
|
||||
public class FanOutFanIn {
|
||||
public static Long fanOutFanIn(
|
||||
final List<SquareNumberRequest> requests, final Consumer consumer) {
|
||||
|
||||
ExecutorService service = Executors.newFixedThreadPool(requests.size());
|
||||
|
||||
// fanning out
|
||||
List<CompletableFuture<Void>> futures =
|
||||
requests.stream()
|
||||
.map(
|
||||
request ->
|
||||
CompletableFuture.runAsync(() -> request.delayedSquaring(consumer), service))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).join();
|
||||
|
||||
return consumer.getSumOfSquaredNumbers().get();
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
`Consumer` is used a callback class that will be called when a request is completed. This will aggregate
|
||||
the result from all requests.
|
||||
|
||||
```java
|
||||
public class Consumer {
|
||||
|
||||
private final AtomicLong sumOfSquaredNumbers;
|
||||
|
||||
Consumer(Long init) {
|
||||
sumOfSquaredNumbers = new AtomicLong(init);
|
||||
}
|
||||
|
||||
public Long add(final Long num) {
|
||||
return sumOfSquaredNumbers.addAndGet(num);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Request is represented as a `SquareNumberRequest` that squares the number with random delay and calls the
|
||||
`Consumer` once it is squared.
|
||||
|
||||
```java
|
||||
public class SquareNumberRequest {
|
||||
|
||||
private final Long number;
|
||||
public void delayedSquaring(final Consumer consumer) {
|
||||
|
||||
var minTimeOut = 5000L;
|
||||
|
||||
SecureRandom secureRandom = new SecureRandom();
|
||||
var randomTimeOut = secureRandom.nextInt(2000);
|
||||
|
||||
try {
|
||||
// this will make the thread sleep from 5-7s.
|
||||
Thread.sleep(minTimeOut + randomTimeOut);
|
||||
} catch (InterruptedException e) {
|
||||
LOGGER.error("Exception while sleep ", e);
|
||||
Thread.currentThread().interrupt();
|
||||
} finally {
|
||||
consumer.add(number * number);
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Class diagram
|
||||

|
||||
|
||||
## Applicability
|
||||
|
||||
Use this pattern when you can divide the workload into multiple chunks that can be dealt with separately.
|
||||
|
||||
## Related patterns
|
||||
|
||||
* [Aggregator Microservices](https://java-design-patterns.com/patterns/aggregator-microservices/)
|
||||
* [API Gateway](https://java-design-patterns.com/patterns/api-gateway/)
|
||||
|
||||
## Credits
|
||||
|
||||
* [Understanding Azure Durable Functions - Part 8: The Fan Out/Fan In Pattern](http://dontcodetired.com/blog/post/Understanding-Azure-Durable-Functions-Part-8-The-Fan-OutFan-In-Pattern)
|
||||
* [Fan-out/fan-in scenario in Durable Functions - Cloud backup example](https://docs.microsoft.com/en-us/azure/azure-functions/durable/durable-functions-cloud-backup)
|
||||
* [Understanding the Fan-Out/Fan-In API Integration Pattern](https://dzone.com/articles/understanding-the-fan-out-fan-in-api-integration-p)
|
BIN
fanout-fanin/etc/fanout-fanin.png
Normal file
BIN
fanout-fanin/etc/fanout-fanin.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 39 KiB |
39
fanout-fanin/etc/fanout-fanin.urm.puml
Normal file
39
fanout-fanin/etc/fanout-fanin.urm.puml
Normal file
@ -0,0 +1,39 @@
|
||||
@startuml
|
||||
package com.iluwatar.fanout.fanin {
|
||||
class App {
|
||||
- LOGGER : Logger {static}
|
||||
+ App()
|
||||
+ main(args : String[]) {static}
|
||||
}
|
||||
class Consumer {
|
||||
- sumOfSquaredNumbers : AtomicLong
|
||||
~ Consumer(init : Long)
|
||||
+ add(num : Long) : Long
|
||||
+ getSumOfSquaredNumbers() : AtomicLong
|
||||
}
|
||||
class FanOutFanIn {
|
||||
+ FanOutFanIn()
|
||||
+ fanOutFanIn(requests : List<SquareNumberRequest>, consumer : Consumer) : Long {static}
|
||||
}
|
||||
class SquareNumberRequest {
|
||||
- LOGGER : Logger {static}
|
||||
- number : Long
|
||||
+ SquareNumberRequest(number : Long)
|
||||
+ delayedSquaring(consumer : Consumer)
|
||||
}
|
||||
|
||||
object SquareNumberRequest1
|
||||
object SquareNumberRequest2
|
||||
object SquareNumberRequest3
|
||||
diamond dia
|
||||
}
|
||||
|
||||
App --> FanOutFanIn
|
||||
FanOutFanIn --> "fan out - running in parallel" SquareNumberRequest1
|
||||
FanOutFanIn --> "fan out" SquareNumberRequest2
|
||||
FanOutFanIn --> "fan out" SquareNumberRequest3
|
||||
SquareNumberRequest1 --> "fan in - aggregate using callback" dia
|
||||
SquareNumberRequest2 --> "fan in" dia
|
||||
SquareNumberRequest3 --> "fan in" dia
|
||||
dia --> Consumer
|
||||
@enduml
|
69
fanout-fanin/pom.xml
Normal file
69
fanout-fanin/pom.xml
Normal file
@ -0,0 +1,69 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!--
|
||||
|
||||
The MIT License (MIT)
|
||||
|
||||
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.
|
||||
|
||||
Module Model-view-viewmodel is using ZK framework
|
||||
ZK framework is licensed under LGPL and the license can be found at lgpl-3.0.txt
|
||||
|
||||
-->
|
||||
<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>fanout-fanin</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.fanout.fanin.App</mainClass>
|
||||
</manifest>
|
||||
</archive>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
@ -0,0 +1,74 @@
|
||||
/*
|
||||
* 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.fanout.fanin;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* FanOut/FanIn pattern is a concurrency pattern that refers to executing multiple instances of the
|
||||
* activity function concurrently. The "fan out" part is essentially splitting the data into
|
||||
* multiple chunks and then calling the activity function multiple times, passing the chunks.
|
||||
*
|
||||
* <p>When each chunk has been processed, the "fan in" takes place that aggregates results from each
|
||||
* instance of function and forms a single final result.
|
||||
*
|
||||
* <p>This pattern is only really useful if you can “chunk” the workload in a meaningful way for
|
||||
* splitting up to be processed in parallel.
|
||||
*/
|
||||
@Slf4j
|
||||
public class App {
|
||||
|
||||
/**
|
||||
* Entry point.
|
||||
*
|
||||
* <p>Implementation provided has a list of numbers that has to be squared and added. The list can
|
||||
* be chunked in any way and the "activity function" {@link
|
||||
* SquareNumberRequest#delayedSquaring(Consumer)} i.e. squaring the number ca be done
|
||||
* concurrently. The "fan in" part is handled by the {@link Consumer} that takes in the result
|
||||
* from each instance of activity and aggregates it whenever that particular activity function
|
||||
* gets over.
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
final List<Long> numbers = Arrays.asList(1L, 3L, 4L, 7L, 8L);
|
||||
|
||||
LOGGER.info("Numbers to be squared and get sum --> {}", numbers);
|
||||
|
||||
final List<SquareNumberRequest> requests =
|
||||
numbers.stream().map(SquareNumberRequest::new).collect(Collectors.toList());
|
||||
|
||||
var consumer = new Consumer(0L);
|
||||
|
||||
// Pass the request and the consumer to fanOutFanIn or sometimes referred as Orchestrator
|
||||
// function
|
||||
final Long sumOfSquaredNumbers = FanOutFanIn.fanOutFanIn(requests, consumer);
|
||||
|
||||
LOGGER.info("Sum of all squared numbers --> {}", sumOfSquaredNumbers);
|
||||
}
|
||||
}
|
@ -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.fanout.fanin;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Consumer or callback class that will be called everytime a request is complete This will
|
||||
* aggregate individual result to form a final result.
|
||||
*/
|
||||
@Getter
|
||||
public class Consumer {
|
||||
|
||||
private final AtomicLong sumOfSquaredNumbers;
|
||||
|
||||
Consumer(Long init) {
|
||||
sumOfSquaredNumbers = new AtomicLong(init);
|
||||
}
|
||||
|
||||
public Long add(final Long num) {
|
||||
return sumOfSquaredNumbers.addAndGet(num);
|
||||
}
|
||||
}
|
@ -0,0 +1,62 @@
|
||||
/*
|
||||
* 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.fanout.fanin;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* FanOutFanIn class processes long running requests, when any of the processes gets over, result is
|
||||
* passed over to the consumer or the callback function. Consumer will aggregate the results as they
|
||||
* keep on completing.
|
||||
*/
|
||||
public class FanOutFanIn {
|
||||
|
||||
/**
|
||||
* the main fanOutFanIn function or orchestrator function.
|
||||
* @param requests List of numbers that need to be squared and summed up
|
||||
* @param consumer Takes in the squared number from {@link SquareNumberRequest} and sums it up
|
||||
* @return Aggregated sum of all squared numbers.
|
||||
*/
|
||||
public static Long fanOutFanIn(
|
||||
final List<SquareNumberRequest> requests, final Consumer consumer) {
|
||||
|
||||
ExecutorService service = Executors.newFixedThreadPool(requests.size());
|
||||
|
||||
// fanning out
|
||||
List<CompletableFuture<Void>> futures =
|
||||
requests.stream()
|
||||
.map(
|
||||
request ->
|
||||
CompletableFuture.runAsync(() -> request.delayedSquaring(consumer), service))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).join();
|
||||
|
||||
return consumer.getSumOfSquaredNumbers().get();
|
||||
}
|
||||
}
|
@ -0,0 +1,63 @@
|
||||
/*
|
||||
* 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.fanout.fanin;
|
||||
|
||||
import java.security.SecureRandom;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* Squares the number with a little timeout to give impression of long running process that return
|
||||
* at different times.
|
||||
*/
|
||||
@Slf4j
|
||||
@AllArgsConstructor
|
||||
public class SquareNumberRequest {
|
||||
|
||||
private final Long number;
|
||||
|
||||
/**
|
||||
* Squares the number with a little timeout to give impression of long running process that return
|
||||
* at different times.
|
||||
* @param consumer callback class that takes the result after the delay.
|
||||
* */
|
||||
public void delayedSquaring(final Consumer consumer) {
|
||||
|
||||
var minTimeOut = 5000L;
|
||||
|
||||
SecureRandom secureRandom = new SecureRandom();
|
||||
var randomTimeOut = secureRandom.nextInt(2000);
|
||||
|
||||
try {
|
||||
// this will make the thread sleep from 5-7s.
|
||||
Thread.sleep(minTimeOut + randomTimeOut);
|
||||
} catch (InterruptedException e) {
|
||||
LOGGER.error("Exception while sleep ", e);
|
||||
Thread.currentThread().interrupt();
|
||||
} finally {
|
||||
consumer.add(number * number);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
/*
|
||||
* 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.fanout.fanin;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
|
||||
class AppTest {
|
||||
|
||||
@Test
|
||||
void shouldLaunchApp() {
|
||||
assertDoesNotThrow(() -> App.main(new String[]{}));
|
||||
}
|
||||
}
|
@ -0,0 +1,47 @@
|
||||
/*
|
||||
* 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.fanout.fanin;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
class FanOutFanInTest {
|
||||
|
||||
@Test
|
||||
void fanOutFanInTest() {
|
||||
final List<Long> numbers = Arrays.asList(1L, 3L, 4L, 7L, 8L);
|
||||
|
||||
final List<SquareNumberRequest> requests =
|
||||
numbers.stream().map(SquareNumberRequest::new).collect(Collectors.toList());
|
||||
|
||||
final Consumer consumer = new Consumer(0L);
|
||||
|
||||
final Long sumOfSquaredNumbers = FanOutFanIn.fanOutFanIn(requests, consumer);
|
||||
|
||||
Assertions.assertEquals(139, sumOfSquaredNumbers);
|
||||
}
|
||||
}
|
@ -0,0 +1,41 @@
|
||||
/*
|
||||
* 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.fanout.fanin;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class SquareNumberRequestTest {
|
||||
|
||||
@Test
|
||||
void delayedSquaringTest() {
|
||||
Consumer consumer = new Consumer(10L);
|
||||
|
||||
SquareNumberRequest squareNumberRequest = new SquareNumberRequest(5L);
|
||||
|
||||
squareNumberRequest.delayedSquaring(consumer);
|
||||
|
||||
Assertions.assertEquals(35, consumer.getSumOfSquaredNumbers().get());
|
||||
}
|
||||
}
|
@ -1,9 +1,6 @@
|
||||
---
|
||||
layout: pattern
|
||||
title: Feature Toggle
|
||||
folder: feature-toggle
|
||||
permalink: /patterns/feature-toggle/
|
||||
categories: Behavioral
|
||||
category: Behavioral
|
||||
language: en
|
||||
tags:
|
||||
- Extensibility
|
||||
|
@ -1,12 +1,8 @@
|
||||
---
|
||||
layout: pattern
|
||||
title: Filterer
|
||||
folder: filterer
|
||||
permalink: /patterns/filterer/
|
||||
description: Design pattern that helps container-like objects to return filtered version of themselves.# short meta description that shows in Google search results
|
||||
language: en
|
||||
categories:
|
||||
- Functional
|
||||
category: Functional
|
||||
tags:
|
||||
- Extensibility
|
||||
---
|
||||
|
@ -1,9 +1,6 @@
|
||||
---
|
||||
layout: pattern
|
||||
title: Fluent Interface
|
||||
folder: fluentinterface
|
||||
permalink: /patterns/fluentinterface/
|
||||
categories: Functional
|
||||
category: Functional
|
||||
language: en
|
||||
tags:
|
||||
- Reactive
|
||||
|
@ -1,9 +1,6 @@
|
||||
---
|
||||
layout: pattern
|
||||
title: Flux
|
||||
folder: flux
|
||||
permalink: /patterns/flux/
|
||||
categories: Structural
|
||||
category: Structural
|
||||
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