Skip to content

Commit b901e51

Browse files
committed
docs: update execute around
1 parent 74f039f commit b901e51

File tree

1 file changed

+16
-15
lines changed

1 file changed

+16
-15
lines changed

execute-around/README.md

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Execute Around idiom frees the user from certain actions that should always be e
2323

2424
Real-world example
2525

26-
> A class needs to be provided for writing text strings to files. To make it easy for the user, the service class opens and closes the file automatically. The user only has to specify what is written into which file.
26+
> A real-world analogy for the Execute Around pattern can be found in the use of rental cars. When you rent a car, the rental company handles all the setup (cleaning the car, filling it with gas, ensuring it's in good condition) and cleanup (checking the car back in, inspecting it for damage, refueling it if necessary) processes for you. As a customer, you simply use the car for your intended purpose without worrying about the setup and cleanup. This pattern of abstracting away the repetitive tasks around the main operation is similar to the Execute Around pattern in software, where the setup and cleanup of resources are handled by a reusable method, allowing the main logic to be executed seamlessly.
2727
2828
In plain words
2929

@@ -35,6 +35,8 @@ In plain words
3535
3636
**Programmatic Example**
3737

38+
A class needs to be provided for writing text strings to files. To make it easy for the user, the service class opens and closes the file automatically. The user only has to specify what is written into which file.
39+
3840
`SimpleFileWriter` class implements the Execute Around idiom. It takes `FileWriterAction` as a constructor argument allowing the user to specify what gets written into the file.
3941

4042
```java
@@ -60,15 +62,18 @@ public class SimpleFileWriter {
6062
The following code demonstrates how `SimpleFileWriter` is used. `Scanner` is used to print the file contents after the writing finishes.
6163

6264
```java
63-
// create the file writer and execute the custom action
64-
FileWriterAction writeHello = writer -> writer.write("Gandalf was here");
65-
new SimpleFileWriter("testfile.txt", writeHello);
66-
67-
// print the file contents
68-
try (var scanner = new Scanner(new File("testfile.txt"))) {
69-
while (scanner.hasNextLine()) {
70-
LOGGER.info(scanner.nextLine());
71-
}
65+
public static void main(String[] args) throws IOException {
66+
67+
// create the file writer and execute the custom action
68+
FileWriterAction writeHello = writer -> writer.write("Gandalf was here");
69+
new SimpleFileWriter("testfile.txt", writeHello);
70+
71+
// print the file contents
72+
try (var scanner = new Scanner(new File("testfile.txt"))) {
73+
while (scanner.hasNextLine()) {
74+
LOGGER.info(scanner.nextLine());
75+
}
76+
}
7277
}
7378
```
7479

@@ -81,10 +86,6 @@ Here's the console output.
8186
21:18:07.199 [main] INFO com.iluwatar.execute.around.App - Gandalf was here
8287
```
8388

84-
## Class diagram
85-
86-
![alt text](./etc/execute-around.png "Execute Around")
87-
8889
## Applicability
8990

9091
* Useful in scenarios requiring repetitive setup and cleanup activities, particularly in resource management (e.g., files, network connections, database sessions).
@@ -118,4 +119,4 @@ Trade-offs:
118119

119120
* [Effective Java](https://amzn.to/4aDdWbs)
120121
* [Java Design Patterns: A Hands-On Experience with Real-World Examples](https://amzn.to/3vUGApm)
121-
* [Functional Programming in Java: Harnessing the Power of Java 8 Lambda Expressions](https://www.amazon.com/gp/product/1937785467/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=1937785467&linkCode=as2&tag=javadesignpat-20&linkId=7e4e2fb7a141631491534255252fd08b)
122+
* [Functional Programming in Java](https://amzn.to/3JUIc5Q)

0 commit comments

Comments
 (0)