Compare commits
9 Commits
adapter-gr
...
broken-lin
Author | SHA1 | Date | |
---|---|---|---|
81f9291e76 | |||
58c0008b75 | |||
bc7040566e | |||
bb49e3c4e9 | |||
842b400fee | |||
ec90320eda | |||
3126ad3106 | |||
e2ebb59fe7 | |||
d87e8cf10d |
@ -1568,6 +1568,24 @@
|
||||
"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"
|
||||
]
|
||||
}
|
||||
],
|
||||
"contributorsPerLine": 4,
|
||||
|
@ -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)
|
||||
|
||||
<br/>
|
||||
|
||||
@ -333,6 +333,10 @@ 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>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
<!-- markdownlint-restore -->
|
||||
|
@ -123,4 +123,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 |
@ -18,10 +18,10 @@ couldn't otherwise because of incompatible interfaces.
|
||||
|
||||
## Explanation
|
||||
|
||||
Real-world example
|
||||
Real world example
|
||||
|
||||
> Consider that you have some pictures on your memory card and you need to transfer them to your computer. To transfer them, you need some kind of adapter that is compatible with your computer ports so that you can attach a memory card to your computer. In this case card reader is an adapter.
|
||||
> Another example would be the famous power adapter; a three-legged plug can't be connected to a two-pronged outlet, it needs to use a power adapter that makes it compatible with the two-pronged outlets.
|
||||
> Consider that you have some pictures in your memory card and you need to transfer them to your computer. In order to transfer them you need some kind of adapter that is compatible with your computer ports so that you can attach memory card to your computer. In this case card reader is an adapter.
|
||||
> Another example would be the famous power adapter; a three legged plug can't be connected to a two pronged outlet, it needs to use a power adapter that makes it compatible with the two pronged outlet.
|
||||
> Yet another example would be a translator translating words spoken by one person to another
|
||||
|
||||
In plain words
|
||||
@ -36,7 +36,7 @@ Wikipedia says
|
||||
|
||||
Consider a captain that can only use rowing boats and cannot sail at all.
|
||||
|
||||
First, we have interfaces `RowingBoat` and `FishingBoat`
|
||||
First we have interfaces `RowingBoat` and `FishingBoat`
|
||||
|
||||
```java
|
||||
public interface RowingBoat {
|
||||
@ -68,7 +68,7 @@ public class Captain {
|
||||
}
|
||||
```
|
||||
|
||||
Now let's say the pirates are coming and our captain needs to escape but there is only a fishing boat available. We need to create an adapter that allows the captain to operate the fishing boat with his rowing boat skills.
|
||||
Now let's say the pirates are coming and our captain needs to escape but there is only fishing boat available. We need to create an adapter that allows the captain to operate the fishing boat with his rowing boat skills.
|
||||
|
||||
```java
|
||||
@Slf4j
|
||||
@ -100,25 +100,25 @@ captain.row();
|
||||
## Applicability
|
||||
Use the Adapter pattern when
|
||||
|
||||
* You want to use an existing class, and its interface does not match the one you need
|
||||
* You want to create a reusable class that cooperates with unrelated or unforeseen classes, that is, classes that don't necessarily have compatible interfaces
|
||||
* You need to use several existing subclasses, but it's impractical to adapt their interface by subclassing everyone. An object adapter can adapt the interface of its parent class.
|
||||
* Most of the applications using third-party libraries use adapters as a middle layer between the application and the 3rd party library to decouple the application from the library. If another library has to be used only an adapter for the new library is required without having to change the application code.
|
||||
* you want to use an existing class, and its interface does not match the one you need
|
||||
* you want to create a reusable class that cooperates with unrelated or unforeseen classes, that is, classes that don't necessarily have compatible interfaces
|
||||
* you need to use several existing subclasses, but it's impractical to adapt their interface by subclassing every one. An object adapter can adapt the interface of its parent class.
|
||||
* most of the applications using third party libraries use adapters as a middle layer between the application and the 3rd party library to decouple the application from the library. If another library has to be used only an adapter for the new library is required without having to change the application code.
|
||||
|
||||
## Consequences
|
||||
Class and object adapters have different trade-offs. A class adapter
|
||||
|
||||
* Adapts Adaptee to Target by committing to a concrete Adaptee class. As a consequence, a class adapter won’t work when we want to adapt a class and all its subclasses.
|
||||
* Let’s Adapter override some of Adaptee’s behavior since Adapter is a subclass of Adaptee.
|
||||
* Introduces only one object, and no additional pointer indirection is needed to get to the adaptee.
|
||||
* adapts Adaptee to Target by committing to a concrete Adaptee class. As a consequence, a class adapter won’t work when we want to adapt a class and all its subclasses.
|
||||
* let’s Adapter override some of Adaptee’s behavior, since Adapter is a subclass of Adaptee.
|
||||
* introduces only one object, and no additional pointer indirection is needed to get to the adaptee.
|
||||
|
||||
An object adapter
|
||||
|
||||
* Lets a single Adapter work with many Adaptees—that is, the Adaptee itself and all of its subclasses (if any). The Adapter can also add functionality to all Adaptees at once.
|
||||
* Makes it harder to override Adaptee behavior. It will require subclassing Adaptee and making the Adapter refer to the subclass rather than the Adaptee itself.
|
||||
* let’s a single Adapter work with many Adaptees—that is, the Adaptee itself and all of its subclasses (if any). The Adapter can also add functionality to all Adaptees at once.
|
||||
* makes it harder to override Adaptee behavior. It will require subclassing Adaptee and making Adapter refer to the subclass rather than the Adaptee itself.
|
||||
|
||||
|
||||
## Real-world examples
|
||||
## Known uses
|
||||
|
||||
* [java.util.Arrays#asList()](http://docs.oracle.com/javase/8/docs/api/java/util/Arrays.html#asList%28T...%29)
|
||||
* [java.util.Collections#list()](https://docs.oracle.com/javase/8/docs/api/java/util/Collections.html#list-java.util.Enumeration-)
|
||||
|
@ -33,7 +33,7 @@ import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
/**
|
||||
* Tests for the adapter pattern.
|
||||
* Test class
|
||||
*/
|
||||
class AdapterPatternTest {
|
||||
|
||||
|
@ -33,7 +33,9 @@ import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
class AppTest {
|
||||
|
||||
/**
|
||||
* Check whether the execution of the main method in {@link App}
|
||||
* Issue: Add at least one assertion to this test case.
|
||||
*
|
||||
* Solution: Inserted assertion to check whether the execution of the main method in {@link App}
|
||||
* throws an exception.
|
||||
*/
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
---
|
||||
---
|
||||
layout: pattern
|
||||
title: Command
|
||||
folder: command
|
||||
|
@ -25,4 +25,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/)
|
||||
|
@ -103,4 +103,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)
|
||||
|
@ -24,7 +24,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 +31,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)
|
||||
|
120
fanout-fanin/README.md
Normal file
120
fanout-fanin/README.md
Normal file
@ -0,0 +1,120 @@
|
||||
---
|
||||
layout: pattern
|
||||
title: Fan-Out/Fan-In
|
||||
folder: fanout-fanin
|
||||
permalink: /patterns/fanout-fanin/
|
||||
categories: 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());
|
||||
}
|
||||
}
|
23
gpl-3.0.txt
23
gpl-3.0.txt
@ -1,3 +1,26 @@
|
||||
====
|
||||
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.
|
||||
====
|
||||
|
||||
GNU GENERAL PUBLIC LICENSE
|
||||
Version 3, 29 June 2007
|
||||
|
||||
|
@ -30,7 +30,7 @@ Use Half-Sync/Half-Async pattern when
|
||||
|
||||
* [BSD Unix networking subsystem](https://www.dre.vanderbilt.edu/~schmidt/PDF/PLoP-95.pdf)
|
||||
* [Real Time CORBA](http://www.omg.org/news/meetings/workshops/presentations/realtime2001/4-3_Pyarali_thread-pool.pdf)
|
||||
* [Android AsyncTask framework](http://developer.android.com/reference/android/os/AsyncTask.html)
|
||||
* [Android AsyncTask framework](https://developer.android.com/reference/android/os/AsyncTask)
|
||||
|
||||
## Credits
|
||||
|
||||
|
23
lgpl-3.0.txt
23
lgpl-3.0.txt
@ -1,3 +1,26 @@
|
||||
====
|
||||
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.
|
||||
====
|
||||
|
||||
GNU LESSER GENERAL PUBLIC LICENSE
|
||||
Version 3, 29 June 2007
|
||||
|
||||
|
@ -163,7 +163,7 @@ public class LambdaStrategy {
|
||||
|
||||
## 클래스 다이어그램
|
||||
|
||||

|
||||

|
||||
|
||||
## 적용 가능성
|
||||
|
||||
|
@ -122,4 +122,4 @@ public class Orc extends ActiveCreature {
|
||||
|
||||
## 类图
|
||||
|
||||

|
||||

|
||||
|
@ -139,7 +139,7 @@ king.makeRequest(new Request(RequestType.COLLECT_TAX, "collect tax")); // Orc so
|
||||
```
|
||||
|
||||
## 类图
|
||||

|
||||

|
||||
|
||||
## 适用性
|
||||
使用责任链模式当
|
||||
|
@ -28,5 +28,5 @@ In this pattern, parallel processing is performed using a system consisting of a
|
||||
|
||||
## Credits
|
||||
|
||||
* [https://docs.gigaspaces.com/sbp/master-worker-pattern.html]
|
||||
* [http://www.cs.sjsu.edu/~pearce/oom/patterns/behavioral/masterslave.htm]
|
||||
* [Master-Worker Pattern](https://docs.gigaspaces.com/sbp/master-worker-pattern.html)
|
||||
* [The Master-Slave Design Pattern](https://www.cs.sjsu.edu/~pearce/oom/patterns/behavioral/masterslave.htm)
|
||||
|
@ -118,7 +118,7 @@ To deploy the example, go to model-view-viewmodel folder and run:
|
||||
|
||||
* [Zkoss Demo](https://www.zkoss.org/zkdemo/getting_started/mvvm)
|
||||
* [Learn MVVM](https://www.learnmvvm.com/)
|
||||
* [Android Developer CodeLabs](https://codelabs.developers.google.com/codelabs/android-databinding)
|
||||
* [Data Binding in Android](https://developer.android.com/codelabs/android-databinding#0)
|
||||
|
||||
## Typical Use Case
|
||||
|
||||
|
@ -114,7 +114,7 @@ the [Single Responsibility Principle (SRP)](https://java-design-patterns.com/pri
|
||||
|
||||
## Related patterns
|
||||
|
||||
* [Chain of Responsibility](https://java-design-patterns.com/patterns/chain/)
|
||||
* [Chain of Responsibility](https://java-design-patterns.com/patterns/chain-of-responsibility/)
|
||||
|
||||
## Credits
|
||||
|
||||
|
1
pom.xml
1
pom.xml
@ -232,6 +232,7 @@
|
||||
<module>table-module</module>
|
||||
<module>presentation</module>
|
||||
<module>lockable-object</module>
|
||||
<module>fanout-fanin</module>
|
||||
<module>domain-model</module>
|
||||
</modules>
|
||||
|
||||
|
@ -36,16 +36,12 @@ public final class ThreadSafeDoubleCheckLocking {
|
||||
|
||||
private static volatile ThreadSafeDoubleCheckLocking instance;
|
||||
|
||||
private static boolean flag = true;
|
||||
|
||||
/**
|
||||
* private constructor to prevent client from instantiating.
|
||||
*/
|
||||
private ThreadSafeDoubleCheckLocking() {
|
||||
// to prevent instantiating by Reflection call
|
||||
if (flag) {
|
||||
flag = false;
|
||||
} else {
|
||||
if (instance != null) {
|
||||
throw new IllegalStateException("Already initialized.");
|
||||
}
|
||||
}
|
||||
|
@ -34,9 +34,7 @@ public final class ThreadSafeLazyLoadedIvoryTower {
|
||||
|
||||
private ThreadSafeLazyLoadedIvoryTower() {
|
||||
// Protect against instantiation via reflection
|
||||
if (instance == null) {
|
||||
instance = this;
|
||||
} else {
|
||||
if (instance != null) {
|
||||
throw new IllegalStateException("Already initialized.");
|
||||
}
|
||||
}
|
||||
|
@ -25,4 +25,4 @@ The Subclass Sandbox pattern is a very simple, common pattern lurking in lots of
|
||||
|
||||
## Credits
|
||||
|
||||
* [Game Programming Patterns - Subclass Sandbox]([http://gameprogrammingpatterns.com/subclass-sandbox.html](http://gameprogrammingpatterns.com/subclass-sandbox.html))
|
||||
* [Game Programming Patterns - Subclass Sandbox](https://gameprogrammingpatterns.com/subclass-sandbox.html)
|
||||
|
Reference in New Issue
Block a user