I have a situation where I have three requirements:
- Lazy initialization - don't create the collection until asked for it
- Caching - keep the collection in memory on the object
- Reinitialization - be able to reinitialize the collection when desired, instead of simply getting the existing results.
This is simply an optimization inside a single class - it is not loading anything from a database and ideally I'd like just a good method design pattern for this, not a multiple-class design.
Usually for lazy initialization I'd have this:
Collection getCollection() {
if (collection != null) {
// generate and set collection
}
return collection;
}
But now I'm having trouble deciding on the best way to provide for reinitialization of a fresh collection and getting that collection. A fresh
boolean parameter would work, but adding a parameter to a getter doesn't seem to feel right (maybe that's the Java in me talking — I could be convinced).