Evaluating Software Design Patterns
— the "Gang of Four" patterns implemented in Java 6

dk.rode.thesis.singleton
Interface SingletonRegistry<T>

Type Parameters:
T - The super type of all singleton types handled by this registry. Use Object if any type of singleton type can be handled.
All Known Implementing Classes:
LoadableSingletonRegistry, StatefullSingletonRegistry, StatelessSingletonRegistry

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.

Author:
Gunni Rode / rode.dk

Method Summary
<S extends T>
S
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

getInstance

<S extends T> S getInstance(Class<S> type)
Returns the singleton instance of the type supplied as type.

Whether or not type is a singleton type handled by this registry can be determined by the isSingleton(Class) method.

Type Parameters:
S - The actual singleton type.
Parameters:
type - The class token identifying the singleton type; cannot be null.
Returns:
The singleton instance of the type supplied as type; never null.
Throws:
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.

isSingleton

boolean isSingleton(Class<?> type)
Returns true of the type supplied as 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!

Parameters:
type - The class token identifying the type; cannot be null.
Returns:
True if type represents a singleton type handled by this registry.
Throws:
NullPointerException - If type is null.

Gunni Rode / rode.dk

Feel free to use and/or modify the Java 6 source code developed for this thesis AT YOUR OWN RISK, but note that the source code comes WITHOUT ANY — and I do mean WITHOUT ANY — form of warranty WHAT SO EVER!

The original thesis and source code are available at rode.dk/thesis.