First page Back Continue Last page Summary Graphics

Execute-Around-Method


Notes:

It is both tedious and a maintenance problem to keep reproducing the same code in slightly different contexts, and if a resource is repeatedly allocated, used and released for a variety of purposes then it may be useful to employ the Execute-Around-Method idiom[5] to factor out this code.
The way this is expressed in Java is to pass an anonymous local class to a method whose responsibility is to allocate the resource, pass it to the supplied code, and finally release the resource.
In the example the "resource" is a (conceptual) lock on the notification of selection changes on a ListSelectionModel – by having a single function responsible for changing the valueIsAdjusting property it is easy to see that it is managed correctly.