|
|||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||||
java.lang.Objectmandala.util.SingletonGiver
This class provides a general purpose singleton implementation.
Each instance of this class is associated with:
SingletonGiver.Factory
implementation - which allows the creation of singleton;Map implementation which
prevent multiple instanciation of a singleton for the same
object.The method getInstance(Object, Object[] args) returns the
singleton mapped to the specified object in the
cache or the one returned by the factory. In this latter
case, the singleton is also mapped to the specified
object in the cache using the register(Object
singleton, Object object) method.
The use of SoftReference in the cache prevent an OutOfMemoryError to occur before the cache being cleared of
singletons. This means, cached singletons may be garbage collected when they
are not strongly referenced and when memory is considered low by the garbage
collector. Hence, multiple call to getInstance(Object) may
occurs for the same object when a singleton has already been mapped
in the cache but has been garbage collected.
Users must be aware of the mapping they want for their
singletons. Default mapping is done by a IdentityHashMap
which provides the "natural behavior": two objects share the same
singleton if and only if they are the same objects (in the sense of
the '==' operator). Note that it is possible to use a "traditionnal" Map implementation such as HashMap for the
mapping. In this case, two objects share the same singleton if and
only if they are equals (in the sense of Object.equals(Object)).
The method unregister(Object) allows mapping to be removed from
the cache. Registered objects may use their method Object.finalize()
to ensure they are unregistered when garbage collected.
| Nested Class Summary | |
static interface |
SingletonGiver.Factory
Interface that define how singleton are created. |
| Field Summary | |
protected Map |
cache
References cache. |
protected SingletonGiver.Factory |
factory
The factory used to create singletons. |
protected static Syslog |
syslog
Message logger. |
| Constructor Summary | |
SingletonGiver(SingletonGiver.Factory factory)
Creates a new SingletonGiver instance. |
|
SingletonGiver(SingletonGiver.Factory factory,
Map cache)
Creates a new SingletonGiver instance. |
|
| Method Summary | |
Object |
getInstance(Object object)
Equivalent to getInstance(object, null). |
Object |
getInstance(Object object,
Object[] args)
Returns the singleton to which the specified object is mapped to. |
static Syslog |
getSyslog()
Returns the logger. |
void |
register(Object object,
Object singleton)
Register a mapping. |
static void |
setSyslog(Syslog syslog)
Sets the logger. |
void |
unregister(Object object)
Unregister a mapping. |
| Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
protected static Syslog syslog
Message logger.
Each message (debug, log, warning, error, ...) are wrote to this message logger.
Syslogprotected SingletonGiver.Factory factory
The factory used to create singletons.
protected Map cache
References cache.
| Constructor Detail |
public SingletonGiver(SingletonGiver.Factory factory)
Creates a new SingletonGiver instance.
Use the default IdentityHashMap implementation for the
cache.
factory - the factory used to create new singletonSingletonGiver.Factory
public SingletonGiver(SingletonGiver.Factory factory,
Map cache)
Creates a new SingletonGiver instance.
factory - the factory used to create new singletoncache - the map implementation to use as a cache| Method Detail |
public static Syslog getSyslog()
Returns the logger.
public static void setSyslog(Syslog syslog)
Sets the logger.
syslog - the logger to set
public void register(Object object,
Object singleton)
Register a mapping.
object - the object mapped to the given singletonsingleton - the singletonpublic void unregister(Object object)
Unregister a mapping.
object - the object previously mapped to a singletonpublic Object getInstance(Object object)
Equivalent to getInstance(object, null).
getInstance(Object, Object[])
public Object getInstance(Object object,
Object[] args)
Returns the singleton to which the specified object is mapped to.
If a mapping has already been registered for the given object, the
mapped singleton is returned, otherwise a new one is created
using the current factory SingletonGiver.Factory.newInstance(Object, Object[]),
and returned after having registered its mapping.
Notice that the mapping depends on the cache
implementation: if the mapping is based on the '==' operator (such as
the IdentityHashMap class), then:
getInstance(a) == getInstance(b) <=> a == b
Otherwise, if the mapping is based on the Object.equals(Object),
(as documented by the Map interface and as implemented
by HashMap), then:
getInstance(a) == getInstance(b) <=> a.equals(b)
object - the object to get a singleton fromargs - the arguments to use to create the mapped singleton
for the sepcified object
SingletonGiver.Factory
|
|||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||||