Java Reference
In-Depth Information
Listing 6.19. Using a map of closures to implement an interface
The bottom line is that closures can be used as the implementation of an interface, and that
this is an easy and very powerful technique for providing stub implementations of collab-
orators.
Next I want to test the DAO implementation class that uses a flat file to store the accounts.
The goal in this case will be to provide a stub that stands in for the java.io.File class.
6.3.2. The Expando class
I'm going to use a file as my persistence mechanism, but for the testing environment I'm
going to keep a cache of accounts in a map. This means that when I initialize the DAO I
need to read the file and store the accounts found there in a map, and any time I make a
change to an account I need to write the results back to a file. When reading the data I can
just use the map—unless the file has been changed, in which case I'll have to re-read the
file. [ 8 ]
8 I got the idea of using an expando this way from Jeff Brown, indefatigable coauthor of Definitive Guide to Grails
2 (Apress, 2013).
To start, here are the attributes in my FileAccountDAO class:
 
Search WWH ::




Custom Search