@@ -17,10 +17,10 @@ the user to specify only what to do with the resource.
17
17
18
18
## Explanation
19
19
20
- Real world example
20
+ Real- world example
21
21
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
24
24
> specify what is written into which file.
25
25
26
26
In plain words
@@ -35,35 +35,50 @@ In plain words
35
35
36
36
** Programmatic Example**
37
37
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.
39
40
40
41
``` java
41
42
@FunctionalInterface
42
43
public interface FileWriterAction {
43
-
44
44
void writeFile (FileWriter writer ) throws IOException ;
45
-
46
45
}
47
46
47
+ @Slf4j
48
48
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
+ }
53
56
}
54
- }
55
57
}
56
58
```
57
59
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.
59
62
60
63
``` 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
67
82
```
68
83
69
84
## Class diagram
@@ -74,8 +89,7 @@ To utilize the file writer the following code is needed.
74
89
75
90
Use the Execute Around idiom when
76
91
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.
79
93
80
94
## Credits
81
95
0 commit comments