|
Evaluating Software Design Patterns — the "Gang of Four" patterns implemented in Java 6 |
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object dk.rode.thesis.singleton.StatefullSingletonRegistry<T>
T
- The super type of all singleton types handled by
this registry. Use Object
if any type
of singleton type can be handled.public class StatefullSingletonRegistry<T>
A statefull singleton registry stores a reference to each acquired singleton instance using a live daemon thread: as long as the registry is not garbage collected, the singleton instances acquired through it will not be garbage collected either.
By using composition, this registry defers acquisition of the singleton types
to another registry. The registry to use is specified at
construction
time.
References to created singletons can also be
discarded
explicitly.
Implementation notes:
This registry do not utilise an internal cache to store the
acquired singletons because that would require synchronisation.
Furthermore, we do not know how the actual singleton classes are
implemented, i.e. static initialisation, lazily initialised, synchronised,
or not synchronised!? Hence, each time the getInstance(Class)
method is invoked the call is forwarded to the registry actually
used to acquire the singleton types.
By deferring the responsibility to another registry, the other registry
may also be able to acquire broader singleton types, i.e. any singleton
type <? super T>
. It also ensures that the internal state
kept by this registry has nothing to do with the actual creation of
singleton types, only with references to the unique instances of the
singleton types once created.
The anonymous java.lang.Runnable
target
object used by the live daemon thread is an application of the
Adapter pattern.
Field Summary | |
---|---|
private SingletonRegistry<? super T> |
registry
The actual registry used to acquire the singleton types supplied at construction time. |
private Set<T> |
singletons
References to all acquired singleton types based on their identity so they will not be garbage collected. |
private Thread |
thread
Live daemon thread to ensure that the references to acquired singleton instances never goes out of scope. |
Constructor Summary | |
---|---|
StatefullSingletonRegistry()
Constructor. |
|
StatefullSingletonRegistry(SingletonRegistry<? super T> registry)
Constructor. |
Method Summary | ||
---|---|---|
|
discard(Class<S> type)
Discards the reference to the singleton instance of the type supplied as type , if such a reference is
kept by this registry. |
|
|
getInstance(Class<S> type)
Returns the singleton instance of the type supplied as type . |
|
boolean |
isSingleton(Class<?> type)
Returns true of the type supplied as type is a singleton
type handled by this registry, false if not. |
|
Iterator<T> |
iterator()
Returns an iterator to iterate over all singleton instances referenced by this registry. |
|
String |
toString()
Returns the string representation of this registry. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
---|
private final SingletonRegistry<? super T> registry
Never null.
private final Set<T> singletons
Never null, but can be empty.
private final Thread thread
Never null.
Constructor Detail |
---|
public StatefullSingletonRegistry()
An instance of the StatelessSingletonRegistry
type
will be used to acquire the singleton instances.
public StatefullSingletonRegistry(SingletonRegistry<? super T> registry)
registry
- The registry that will be used to acquire the
singleton instances; cannot be null.
NullPointerException
- If registry
is null.Method Detail |
---|
public <S extends T> boolean discard(Class<S> type)
type
, if such a reference is
kept by this registry. If this registry has no such reference, false is returned.
S
- The actual singleton type.type
- The class token identifying the type; cannot be null.
type
represents a singleton type this registry
had a reference to, false if not.
NullPointerException
- If type
is null.isSingleton(Class)
public <S extends T> S getInstance(Class<S> type)
SingletonRegistry
type
.
Whether or not type
is a singleton type handled
by this registry can be determined by the SingletonRegistry.isSingleton(Class)
method.
getInstance
in interface SingletonRegistry<T>
S
- The actual singleton type.type
- The class token identifying the singleton type; cannot be null.
type
;
never null.
NullPointerException
- If type
is null.
SingletonException
- If type
is not a singleton type
handled by this registry, or if the type cannot be created when
first requested.public boolean isSingleton(Class<?> type)
SingletonRegistry
type
is a singleton
type handled by this registry, false if not.
Even if this method returns true, the actual singleton instance
may not be created before it is requested
.
The creation of the singleton instance can therefore still
fail!
isSingleton
in interface SingletonRegistry<T>
type
- The class token identifying the type; cannot be null.
type
represents a singleton type handled by
this registry.
NullPointerException
- If type
is null.discard(Class)
public Iterator<T> iterator()
Modifying the returned iterator will not affect this registry.
iterator
in interface Iterable<T>
public String toString()
toString
in class Object
|
Gunni Rode / rode.dk | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |