public abstract class ObjectFactory extends Object
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
For arrays of variable length ArrayFactory
is recommended.
For convenience, this class provides a static getInstance(java.lang.Class)
method
to retrieve a factory implementation for any given class.
For example:[code]
ObjectFactory
Modifier | Constructor and Description |
---|---|
protected |
ObjectFactory()
Default constructor.
|
Modifier and Type | Method and Description |
---|---|
protected void |
cleanup(Object obj)
Cleans-up this factory's objects for future reuse.
|
protected abstract Object |
create()
Constructs a new object for this factory (using the
new
keyword). |
Allocator |
currentAllocator()
Returns the factory allocator for the current thread (equivalent
to
AllocatorContext.current().getAllocator(this) ). |
protected boolean |
doCleanup()
Indicates if this factory requires cleanup.
|
static ObjectFactory |
getInstance(Class forClass)
Returns a factory implementation producing instances of the specified
class.
|
Object |
object()
Returns a factory object possibly recycled or preallocated.
|
void |
recycle(Object obj)
Recycles the specified object.
|
static void |
setInstance(ObjectFactory factory,
Class forClass)
Sets explicitely the factory to be used for the specified class
(see
getInstance(java.lang.Class) ). |
public static ObjectFactory getInstance(Class forClass)
set explicitly
:[code]
class LogContext {
public static final ClassforClass
- the class for which an object factory is returned.public static void setInstance(ObjectFactory factory, Class forClass)
getInstance(java.lang.Class)
).factory
- the factory to use.forClass
- the associated class.getInstance(Class)
public final Object object()
currentAllocator().next()
.public final void recycle(Object obj)
getAllocator().recycle(obj)
.obj
- the object to be recycled.public final Allocator currentAllocator()
AllocatorContext.current().getAllocator(this)
).protected abstract Object create()
new
keyword).protected void cleanup(Object obj)
resets
reusable
instance. For non Reusable
, this method can be
overriden to dispose of system resources or to clear references to
external objects potentially on the heap (it allows these external
objects to be garbage collected immediately and therefore reduces
the memory footprint). For example:[code]
static ObjectFactoryobj
- the factory object being recycled.protected final boolean doCleanup()
true
if cleanup(java.lang.Object)
is overriden and
cleanup(java.lang.Object)
has been called at least once;
false
otherwise.Copyright © 2005–2015 Javolution. All rights reserved.