This is the Execute Around Method idiom (see the template repository and this SO question ). It is very related to the strategy template. This particular example also uses the low-level Double Dispatch pattern.
I'm not sure when it was first described as an OOP pattern, but probably in KentBeck's Smalltalk Best Practice Patterns. It was probably used in LISP before, although I have no evidence of this from my head.
The executed Around method is an abstraction around try ... catch ... finally
and is used when you have something that needs to be done after a block of code. Resource management is a typical example: fixing or rolling back transactions , releasing locks and mutexes , closing files and sockets , etc.
Usually you find the idiom used with closure , as in the second example. Smalltalk and Ruby are stars using this idiom where it is visible everywhere.
Some languages ββare starting to build syntactic sugar around this pattern. Python has context managers "; C # has a using
statement; Java 7 has a try
with a resource . In languages ββwith destructors (C ++ / D), the associated idiom is Resource Assquisition Is Initilation (RAII) . D also has Scope Guard>, which are also with one abstraction around try ... catch ... finally
.
Update
Besides managing resources, this is useful when you need to do common things around a call or during an iteration.
Spring -JDBC uses it for abstract iteration on request: see RowMapper, RowCallbackHandler, and ResultSetExtractor
JDK uses pattern to perform privileged actions
You can use it in a stopwatch implementation.
I even saw that it was used with registered hooks to do unrelated things around a block of code. For example: write the state to the database, record the results, the runtime of the log and plan the next action.