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
Execute Around idiom frees the user from certain actions that should always be executed before and
12
-
after the business method. A good example of this is resource allocation and deallocation leaving
13
-
the user to specify only what to do with the resource.
20
+
Execute Around idiom frees the user from certain actions that should always be executed before and after the business method. A good example of this is resource allocation and deallocation leaving the user to specify only what to do with the resource.
14
21
15
22
## Explanation
16
23
17
24
Real-world example
18
25
19
-
> A class needs to be provided for writing text strings to files. To make it easy for
20
-
> the user, the service class opens and closes the file automatically. The user only has to
21
-
> specify what is written into which file.
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.
22
27
23
28
In plain words
24
29
25
-
> Execute Around idiom handles boilerplate code before and after business method.
30
+
> Execute Around idiom handles boilerplate code before and after business method.
> Basically it's the pattern where you write a method to do things which are always required, e.g.
30
-
> resource allocation and clean-up, and make the caller pass in "what we want to do with the
31
-
> resource".
34
+
> Basically it's the pattern where you write a method to do things which are always required, e.g. resource allocation and clean-up, and make the caller pass in "what we want to do with the resource".
32
35
33
36
**Programmatic Example**
34
37
35
-
`SimpleFileWriter` class implements the Execute Around idiom. It takes `FileWriterAction` as a
36
-
constructor argument allowing the user to specify what gets written into the file.
38
+
`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.
* Useful in scenarios requiring repetitive setup and cleanup activities, particularly in resource management (e.g., files, network connections, database sessions).
91
+
* Ideal for ensuring proper resource handling and cleanup in the face of exceptions, ensuring resources do not leak.
92
+
* Suitable in any Java application where the same preparation and finalization steps are executed around varying core functionalities.
93
+
94
+
## Known Uses
95
+
96
+
* Java's try-with-resources statement, which ensures that resources are closed after execution regardless of whether an exception was thrown.
97
+
* Frameworks like Spring for managing database transactions, where predefined cleanup or rollback operations are performed depending on the execution outcome.
98
+
99
+
## Consequences
100
+
101
+
Benefits:
102
+
103
+
* Reduces boilerplate code by abstracting routine setup and cleanup tasks.
104
+
* Increases code clarity and maintainability by separating business logic from resource management.
105
+
* Ensures robustness by automatically handling resource cleanup, even in error situations.
106
+
107
+
Trade-offs:
108
+
109
+
* Introduces additional abstraction layers, which might increase complexity and obscure control flow for some developers.
110
+
* May require more sophisticated understanding of closures and functional interfaces in Java.
111
+
112
+
## Related Patterns
88
113
89
-
* An API requires methods to be called in pairs such as open/close or allocate/deallocate.
114
+
*[Template Method](https://java-design-patterns.com/patterns/template-method/): Similar in concept but differs in that it uses inheritance and abstract classes, while Execute Around typically uses interfaces and lambdas.
115
+
*[Decorator](https://java-design-patterns.com/patterns/decorator/): Shares the concept of adding functionality around a core component; can be extended to wrap additional behaviors dynamically.
90
116
91
117
## Credits
92
118
119
+
*[Effective Java](https://amzn.to/4aDdWbs)
120
+
*[Java Design Patterns: A Hands-On Experience with Real-World Examples](https://amzn.to/3vUGApm)
93
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)
0 commit comments