Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
iluwatar committed Sep 1, 2020
1 parent 2a5b8c9 commit b9b6777
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 24 deletions.
5 changes: 4 additions & 1 deletion private-class-data/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ tags:
---

## Intent

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.

Expand Down Expand Up @@ -124,9 +125,11 @@ immutableStew.mix(); // Mixing the immutable stew we find: 2 potatoes, 4 carrot
```

## Class diagram

![alt text](./etc/private-class-data.png "Private Class Data")

## Applicability

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.
41 changes: 22 additions & 19 deletions promise/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,38 +14,41 @@ CompletableFuture

## Intent

A Promise represents a proxy for a value not necessarily known when the promise is created. It allows you to associate
dependent promises to an asynchronous action's eventual success value or failure reason. Promises are a way to write
async code that still appears as though it is executing in a synchronous way.
A Promise represents a proxy for a value not necessarily known when the promise is created. It
allows you to associate dependent promises to an asynchronous action's eventual success value or
failure reason. Promises are a way to write async code that still appears as though it is executing
in a synchronous way.

## Explanation

The Promise object is used for asynchronous computations. A Promise represents an operation that hasn't completed yet,
but is expected in the future.
The Promise object is used for asynchronous computations. A Promise represents an operation that
hasn't completed yet, but is expected in the future.

Promises provide a few advantages over callback objects:
* Functional composition and error handling
* Prevents callback hell and provides callback aggregation
* Functional composition and error handling.
* Prevents callback hell and provides callback aggregation.

Real world example

> We are developing a software solution that downloads files and calculates the number of lines and character
frequencies in those files. Promise is an ideal solution to make the code concise and easy to understand.
> We are developing a software solution that downloads files and calculates the number of lines and
> character frequencies in those files. Promise is an ideal solution to make the code concise and
> easy to understand.
In plain words

> Promise is a placeholder for an asynchronous operation that is ongoing.
Wikipedia says

> In computer science, future, promise, delay, and deferred refer to constructs used for synchronizing program
execution in some concurrent programming languages. They describe an object that acts as a proxy for a result that is
initially unknown, usually because the computation of its value is not yet complete.
> In computer science, future, promise, delay, and deferred refer to constructs used for
> synchronizing program execution in some concurrent programming languages. They describe an object
> 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**

In the example a file is downloaded and its line count is calculated. The calculated line count is then consumed and
printed on console.
In the example a file is downloaded and its line count is calculated. The calculated line count is
then consumed and printed on console.

Let's first introduce a support class we need for implementation. Here's `PromiseSupport`.

Expand Down Expand Up @@ -195,7 +198,7 @@ public class Promise<T> extends PromiseSupport<T> {

public <V> Promise<V> thenApply(Function<? super T, V> func) {
Promise<V> dest = new Promise<>();
fulfillmentAction = new TransformAction<V>(this, dest, func);
fulfillmentAction = new TransformAction<>(this, dest, func);
return dest;
}

Expand Down Expand Up @@ -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
`Promise`.
Now we can show the full example in action. Here's how to download and count the number of lines in
a file using `Promise`.

```java
countLines().thenAccept(
Expand Down Expand Up @@ -280,8 +283,8 @@ Now we can show the full example in action. Here's how to download and count the

## Applicability

Promise pattern is applicable in concurrent programming when some work needs to be done asynchronously
and:
Promise pattern is applicable in concurrent programming when some work needs to be done
asynchronously and:

* Code maintainability and readability suffers due to callback hell.
* You need to compose promises and need better error handling for asynchronous tasks.
Expand Down
2 changes: 1 addition & 1 deletion promise/src/main/java/com/iluwatar/promise/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ private App() {
* @throws InterruptedException if main thread is interrupted.
* @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();
try {
app.promiseUsage();
Expand Down
2 changes: 1 addition & 1 deletion promise/src/main/java/com/iluwatar/promise/Promise.java
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public Promise<T> onError(Consumer<? super Throwable> exceptionHandler) {
*/
public <V> Promise<V> thenApply(Function<? super T, V> func) {
Promise<V> dest = new Promise<>();
fulfillmentAction = new TransformAction<V>(this, dest, func);
fulfillmentAction = new TransformAction<>(this, dest, func);
return dest;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -114,4 +113,4 @@ public T get(long timeout, TimeUnit unit) throws ExecutionException {
}
throw new ExecutionException(exception);
}
}
}

0 comments on commit b9b6777

Please sign in to comment.