This class represents an object factory; it allows for object
recycling, pre-allocation and stack allocations.
Object factories are recommended over class constructors (ref. "new"
keyword) to allows for custom allocation policy (see
AllocatorContext ). For example:[code]
static ObjectFactory BOARD_FACTORY = new ObjectFactory() {
protected int[][] create() {
return new int[8][8];
}
};
...
int[][] board = BOARD_FACTORY.object();
// The board object might have been preallocated at start-up,
// it might also be on the thread "stack/pool" for threads
// executing in a StackContext.
...
BOARD_FACTORY.recycle(board); // Immediate recycling of the board object (optional).
[/code]
For arrays of variable length
ArrayFactory is recommended.
For convenience, this class provides a static
ObjectFactory.getInstance method
to retrieve a factory implementation for any given class.
For example:[code]
ObjectFactory listFactory = ObjectFactory.getInstance(ArrayList.class);
ArrayList list = listFactory.object();
... // Do something.
listFactory.recycle(list); // Optional.
[/code]
author: Jean-Marie Dautelle version: 5.2, August 14, 2007 |