|
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 |
T
- The super type of all singleton types handled by
this registry. Use Object
if any type
of singleton type can be handled.public interface SingletonRegistry<T>
A singleton registry is a container for singleton
instances of any type, S
, inheriting the common
super type, T
.
A singleton type is identified by a class token supplied
to the getInstance(Class)
method, but how the actual
singleton creation and/or delivery is made is decided by the
actual registry implementation.
The registry also decides the specific singleton types
supported
.
Registries do not override hashCode
or equals(Object)
and are therefore only considered equal based on identity.
Implementation notes:
Registry functionality is completely separated from singleton types/instances.
Unlike the registry functionality described by Gamma et al. [Gamma95, p.131],
a SingletonRegistry
instance will never return a null value
in case a singleton type cannot be found. The calling context should
never have to test for a null return value, because either the singleton
instance exist (or be created if need be) or it does not; a null value
does not make any sense, and would in our view constitute a poor design
decision considering the intrinsic conceptual behaviour in the Singleton
Pattern. Hence, in the general case, the context should not have to worry
about checked exceptions as they would never be thrown. For convenience,
the isSingleton(Class)
method can be used to
test if a given type is a singleton supported by the registry.
By declaring an interface to implement for required registry functionality, as opposed to using static methods directly, we get all the standard benefits of object behaviour and polymorhpism at the cost of having to create (possibly singleton) registries as well. For example, a registry implementation may be synchronised or otherwise proxied or decorated, or used as a private member variable in a factory.
The upper bound T
on type parameters for implementation
classes can be used to ensure that all singleton types for
a given registry must implement some sort of Singleton
interface, if so desired.
Method Summary | ||
---|---|---|
|
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. |
Method Detail |
---|
<S extends T> S getInstance(Class<S> type)
type
.
Whether or not type
is a singleton type handled
by this registry can be determined by the isSingleton(Class)
method.
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.boolean isSingleton(Class<?> type)
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!
type
- The class token identifying the type; cannot be null.
type
represents a singleton type handled by
this registry.
NullPointerException
- If type
is null.
|
Gunni Rode / rode.dk | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |