You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: execute-around/README.md
+16-15Lines changed: 16 additions & 15 deletions
Original file line number
Diff line number
Diff line change
@@ -23,7 +23,7 @@ Execute Around idiom frees the user from certain actions that should always be e
23
23
24
24
Real-world example
25
25
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.
27
27
28
28
In plain words
29
29
@@ -35,6 +35,8 @@ In plain words
35
35
36
36
**Programmatic Example**
37
37
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
+
38
40
`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.
39
41
40
42
```java
@@ -60,15 +62,18 @@ public class SimpleFileWriter {
60
62
The following code demonstrates how `SimpleFileWriter` is used. `Scanner` is used to print the file contents after the writing finishes.
61
63
62
64
```java
63
-
// create the file writer and execute the custom action
64
-
FileWriterAction writeHello = writer -> writer.write("Gandalf was here");
* 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:
118
119
119
120
*[Effective Java](https://amzn.to/4aDdWbs)
120
121
*[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