Implementing the execute-around-method pattern
The execute-around-method pattern is intended to make it easy to reuse boilerplate code. For example, every time we modify a key value, we may want to log the result. Perhaps, we want to make sure resources are cleaned up after particular operations are performed.
Sometimes code needs to be executed before or after a method executes. To illustrate this pattern, we will examine how to determine the time required to perform an operation. We will obtain the time before and after an operation to calculate its execution time.
Object-oriented solution to the execute-around-method pattern
A simplistic approach is to copy and paste the code before and after the method call. Consider the situation where we have a complex computation, which we would like to time. We can use the currentTimeMillis
method before and after the computation to determine its duration.
In the following method, we perform this operation. However, to keep the example simple, we will...