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

dk.rode.thesis.singleton
Class StatelessSingletonRegistry<T>

java.lang.Object
  extended by dk.rode.thesis.singleton.StatelessSingletonRegistry<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 Implemented Interfaces:
SingletonRegistry<T>

public class StatelessSingletonRegistry<T>
extends Object
implements SingletonRegistry<T>

A stateless singleton registry is a registry with no internal state that allows any type annotated correctly with the Singleton annotation to be fetched, and possibly created, using the getInstance(Class) method.

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 actual singleton method identified by the Singleton annotation used to annotate the type supplied as the Class argument. This means that all singleton initialisation and creation is handled by the individual singleton types!

Author:
Gunni Rode / rode.dk

Constructor Summary
StatelessSingletonRegistry()
          Constructor.
 
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.
 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
 

Constructor Detail

StatelessSingletonRegistry

public StatelessSingletonRegistry()
Constructor.

Method Detail

getInstance

public <S extends T> S getInstance(Class<S> type)
Description copied from interface: SingletonRegistry
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 SingletonRegistry.isSingleton(Class) method.

Specified by:
getInstance in interface SingletonRegistry<T>
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

public boolean isSingleton(Class<?> type)
Description copied from interface: SingletonRegistry
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!

Specified by:
isSingleton in interface SingletonRegistry<T>
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.

toString

public String toString()
Returns the string representation of this registry.

Overrides:
toString in class Object
Returns:
The string representation; never 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.