Update README.md
This commit is contained in:
parent
2a5b8c977a
commit
b9b6777d15
@ -9,6 +9,7 @@ tags:
|
|||||||
---
|
---
|
||||||
|
|
||||||
## Intent
|
## Intent
|
||||||
|
|
||||||
Private Class Data design pattern seeks to reduce exposure of attributes by limiting their
|
Private Class Data design pattern seeks to reduce exposure of attributes by limiting their
|
||||||
visibility. It reduces the number of class attributes by encapsulating them in single Data object.
|
visibility. It reduces the number of class attributes by encapsulating them in single Data object.
|
||||||
|
|
||||||
@ -124,9 +125,11 @@ immutableStew.mix(); // Mixing the immutable stew we find: 2 potatoes, 4 carrot
|
|||||||
```
|
```
|
||||||
|
|
||||||
## Class diagram
|
## Class diagram
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
## Applicability
|
## Applicability
|
||||||
|
|
||||||
Use the Private Class Data pattern when
|
Use the Private Class Data pattern when
|
||||||
|
|
||||||
* You want to prevent write access to class data members
|
* You want to prevent write access to class data members.
|
||||||
|
@ -14,23 +14,25 @@ CompletableFuture
|
|||||||
|
|
||||||
## Intent
|
## Intent
|
||||||
|
|
||||||
A Promise represents a proxy for a value not necessarily known when the promise is created. It allows you to associate
|
A Promise represents a proxy for a value not necessarily known when the promise is created. It
|
||||||
dependent promises to an asynchronous action's eventual success value or failure reason. Promises are a way to write
|
allows you to associate dependent promises to an asynchronous action's eventual success value or
|
||||||
async code that still appears as though it is executing in a synchronous way.
|
failure reason. Promises are a way to write async code that still appears as though it is executing
|
||||||
|
in a synchronous way.
|
||||||
|
|
||||||
## Explanation
|
## Explanation
|
||||||
|
|
||||||
The Promise object is used for asynchronous computations. A Promise represents an operation that hasn't completed yet,
|
The Promise object is used for asynchronous computations. A Promise represents an operation that
|
||||||
but is expected in the future.
|
hasn't completed yet, but is expected in the future.
|
||||||
|
|
||||||
Promises provide a few advantages over callback objects:
|
Promises provide a few advantages over callback objects:
|
||||||
* Functional composition and error handling
|
* Functional composition and error handling.
|
||||||
* Prevents callback hell and provides callback aggregation
|
* Prevents callback hell and provides callback aggregation.
|
||||||
|
|
||||||
Real world example
|
Real world example
|
||||||
|
|
||||||
> We are developing a software solution that downloads files and calculates the number of lines and character
|
> We are developing a software solution that downloads files and calculates the number of lines and
|
||||||
frequencies in those files. Promise is an ideal solution to make the code concise and easy to understand.
|
> character frequencies in those files. Promise is an ideal solution to make the code concise and
|
||||||
|
> easy to understand.
|
||||||
|
|
||||||
In plain words
|
In plain words
|
||||||
|
|
||||||
@ -38,14 +40,15 @@ In plain words
|
|||||||
|
|
||||||
Wikipedia says
|
Wikipedia says
|
||||||
|
|
||||||
> In computer science, future, promise, delay, and deferred refer to constructs used for synchronizing program
|
> In computer science, future, promise, delay, and deferred refer to constructs used for
|
||||||
execution in some concurrent programming languages. They describe an object that acts as a proxy for a result that is
|
> synchronizing program execution in some concurrent programming languages. They describe an object
|
||||||
initially unknown, usually because the computation of its value is not yet complete.
|
> that acts as a proxy for a result that is initially unknown, usually because the computation of
|
||||||
|
> its value is not yet complete.
|
||||||
|
|
||||||
**Programmatic Example**
|
**Programmatic Example**
|
||||||
|
|
||||||
In the example a file is downloaded and its line count is calculated. The calculated line count is then consumed and
|
In the example a file is downloaded and its line count is calculated. The calculated line count is
|
||||||
printed on console.
|
then consumed and printed on console.
|
||||||
|
|
||||||
Let's first introduce a support class we need for implementation. Here's `PromiseSupport`.
|
Let's first introduce a support class we need for implementation. Here's `PromiseSupport`.
|
||||||
|
|
||||||
@ -195,7 +198,7 @@ public class Promise<T> extends PromiseSupport<T> {
|
|||||||
|
|
||||||
public <V> Promise<V> thenApply(Function<? super T, V> func) {
|
public <V> Promise<V> thenApply(Function<? super T, V> func) {
|
||||||
Promise<V> dest = new Promise<>();
|
Promise<V> dest = new Promise<>();
|
||||||
fulfillmentAction = new TransformAction<V>(this, dest, func);
|
fulfillmentAction = new TransformAction<>(this, dest, func);
|
||||||
return dest;
|
return dest;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -246,8 +249,8 @@ public class Promise<T> extends PromiseSupport<T> {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
Now we can show the full example in action. Here's how to download and count the number of lines in a file using
|
Now we can show the full example in action. Here's how to download and count the number of lines in
|
||||||
`Promise`.
|
a file using `Promise`.
|
||||||
|
|
||||||
```java
|
```java
|
||||||
countLines().thenAccept(
|
countLines().thenAccept(
|
||||||
@ -280,8 +283,8 @@ Now we can show the full example in action. Here's how to download and count the
|
|||||||
|
|
||||||
## Applicability
|
## Applicability
|
||||||
|
|
||||||
Promise pattern is applicable in concurrent programming when some work needs to be done asynchronously
|
Promise pattern is applicable in concurrent programming when some work needs to be done
|
||||||
and:
|
asynchronously and:
|
||||||
|
|
||||||
* Code maintainability and readability suffers due to callback hell.
|
* Code maintainability and readability suffers due to callback hell.
|
||||||
* You need to compose promises and need better error handling for asynchronous tasks.
|
* You need to compose promises and need better error handling for asynchronous tasks.
|
||||||
|
@ -81,7 +81,7 @@ public class App {
|
|||||||
* @throws InterruptedException if main thread is interrupted.
|
* @throws InterruptedException if main thread is interrupted.
|
||||||
* @throws ExecutionException if an execution error occurs.
|
* @throws ExecutionException if an execution error occurs.
|
||||||
*/
|
*/
|
||||||
public static void main(String[] args) throws InterruptedException, ExecutionException {
|
public static void main(String[] args) throws InterruptedException {
|
||||||
var app = new App();
|
var app = new App();
|
||||||
try {
|
try {
|
||||||
app.promiseUsage();
|
app.promiseUsage();
|
||||||
|
@ -141,7 +141,7 @@ public class Promise<T> extends PromiseSupport<T> {
|
|||||||
*/
|
*/
|
||||||
public <V> Promise<V> thenApply(Function<? super T, V> func) {
|
public <V> Promise<V> thenApply(Function<? super T, V> func) {
|
||||||
Promise<V> dest = new Promise<>();
|
Promise<V> dest = new Promise<>();
|
||||||
fulfillmentAction = new TransformAction<V>(this, dest, func);
|
fulfillmentAction = new TransformAction<>(this, dest, func);
|
||||||
return dest;
|
return dest;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -26,7 +26,6 @@ package com.iluwatar.promise;
|
|||||||
import java.util.concurrent.ExecutionException;
|
import java.util.concurrent.ExecutionException;
|
||||||
import java.util.concurrent.Future;
|
import java.util.concurrent.Future;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
import java.util.concurrent.TimeoutException;
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
@ -114,4 +113,4 @@ class PromiseSupport<T> implements Future<T> {
|
|||||||
}
|
}
|
||||||
throw new ExecutionException(exception);
|
throw new ExecutionException(exception);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user