Skip to content

Commit 07ee94d

Browse files
iluwatarohbus
andauthored
refactoring: execute around idiom (#1945)
* Refactor execute around the idiom * fix checkstyle errors Co-authored-by: Subhrodip Mohanta <[email protected]>
1 parent c549218 commit 07ee94d

File tree

3 files changed

+54
-24
lines changed

3 files changed

+54
-24
lines changed

execute-around/README.md

+34-20
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ the user to specify only what to do with the resource.
1717

1818
## Explanation
1919

20-
Real world example
20+
Real-world example
2121

22-
> We need to provide a class that can be used to write text strings to files. To make it easy for
23-
> the user we let our service class open and close the file automatically, the user only has to
22+
> A class needs to be provided for writing text strings to files. To make it easy for
23+
> the user, the service class opens and closes the file automatically. The user only has to
2424
> specify what is written into which file.
2525
2626
In plain words
@@ -35,35 +35,50 @@ In plain words
3535
3636
**Programmatic Example**
3737

38-
Let's introduce our file writer class.
38+
`SimpleFileWriter` class implements the Execute Around idiom. It takes `FileWriterAction` as a
39+
constructor argument allowing the user to specify what gets written into the file.
3940

4041
```java
4142
@FunctionalInterface
4243
public interface FileWriterAction {
43-
4444
void writeFile(FileWriter writer) throws IOException;
45-
4645
}
4746

47+
@Slf4j
4848
public class SimpleFileWriter {
49-
50-
public SimpleFileWriter(String filename, FileWriterAction action) throws IOException {
51-
try (var writer = new FileWriter(filename)) {
52-
action.writeFile(writer);
49+
public SimpleFileWriter(String filename, FileWriterAction action) throws IOException {
50+
LOGGER.info("Opening the file");
51+
try (var writer = new FileWriter(filename)) {
52+
LOGGER.info("Executing the action");
53+
action.writeFile(writer);
54+
LOGGER.info("Closing the file");
55+
}
5356
}
54-
}
5557
}
5658
```
5759

58-
To utilize the file writer the following code is needed.
60+
The following code demonstrates how `SimpleFileWriter` is used. `Scanner` is used to print the file
61+
contents after the writing finishes.
5962

6063
```java
61-
FileWriterAction writeHello = writer -> {
62-
writer.write("Hello");
63-
writer.append(" ");
64-
writer.append("there!");
65-
};
66-
new SimpleFileWriter("testfile.txt", writeHello);
64+
FileWriterAction writeHello = writer -> {
65+
writer.write("Gandalf was here");
66+
};
67+
new SimpleFileWriter("testfile.txt", writeHello);
68+
69+
var scanner = new Scanner(new File("testfile.txt"));
70+
while (scanner.hasNextLine()) {
71+
LOGGER.info(scanner.nextLine());
72+
}
73+
```
74+
75+
Here's the console output.
76+
77+
```
78+
21:18:07.185 [main] INFO com.iluwatar.execute.around.SimpleFileWriter - Opening the file
79+
21:18:07.188 [main] INFO com.iluwatar.execute.around.SimpleFileWriter - Executing the action
80+
21:18:07.189 [main] INFO com.iluwatar.execute.around.SimpleFileWriter - Closing the file
81+
21:18:07.199 [main] INFO com.iluwatar.execute.around.App - Gandalf was here
6782
```
6883

6984
## Class diagram
@@ -74,8 +89,7 @@ To utilize the file writer the following code is needed.
7489

7590
Use the Execute Around idiom when
7691

77-
* You use an API that requires methods to be called in pairs such as open/close or
78-
allocate/deallocate.
92+
* An API requires methods to be called in pairs such as open/close or allocate/deallocate.
7993

8094
## Credits
8195

execute-around/src/main/java/com/iluwatar/execute/around/App.java

+14-4
Original file line numberDiff line numberDiff line change
@@ -23,29 +23,39 @@
2323

2424
package com.iluwatar.execute.around;
2525

26+
import java.io.File;
2627
import java.io.IOException;
28+
import java.util.Scanner;
29+
30+
import lombok.extern.slf4j.Slf4j;
2731

2832
/**
29-
* The Execute Around idiom specifies some code to be executed before and after a method. Typically
33+
* The Execute Around idiom specifies executable code before and after a method. Typically
3034
* the idiom is used when the API has methods to be executed in pairs, such as resource
3135
* allocation/deallocation or lock acquisition/release.
3236
*
3337
* <p>In this example, we have {@link SimpleFileWriter} class that opens and closes the file for
3438
* the user. The user specifies only what to do with the file by providing the {@link
3539
* FileWriterAction} implementation.
3640
*/
41+
@Slf4j
3742
public class App {
3843

3944
/**
4045
* Program entry point.
4146
*/
4247
public static void main(String[] args) throws IOException {
4348

49+
// create the file writer and execute the custom action
4450
FileWriterAction writeHello = writer -> {
45-
writer.write("Hello");
46-
writer.append(" ");
47-
writer.append("there!");
51+
writer.write("Gandalf was here");
4852
};
4953
new SimpleFileWriter("testfile.txt", writeHello);
54+
55+
// print the file contents
56+
var scanner = new Scanner(new File("testfile.txt"));
57+
while (scanner.hasNextLine()) {
58+
LOGGER.info(scanner.nextLine());
59+
}
5060
}
5161
}

execute-around/src/main/java/com/iluwatar/execute/around/SimpleFileWriter.java

+6
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,24 @@
2626
import java.io.FileWriter;
2727
import java.io.IOException;
2828

29+
import lombok.extern.slf4j.Slf4j;
30+
2931
/**
3032
* SimpleFileWriter handles opening and closing file for the user. The user only has to specify what
3133
* to do with the file resource through {@link FileWriterAction} parameter.
3234
*/
35+
@Slf4j
3336
public class SimpleFileWriter {
3437

3538
/**
3639
* Constructor.
3740
*/
3841
public SimpleFileWriter(String filename, FileWriterAction action) throws IOException {
42+
LOGGER.info("Opening the file");
3943
try (var writer = new FileWriter(filename)) {
44+
LOGGER.info("Executing the action");
4045
action.writeFile(writer);
46+
LOGGER.info("Closing the file");
4147
}
4248
}
4349
}

0 commit comments

Comments
 (0)