|
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> dk.rode.thesis.singleton.LoadableSingletonRegistry<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 abstract class LoadableSingletonRegistry<T>
A loadable singleton registry is a registry that offers
the same functionality as StatefullSingletonRegistry
, but
also allows loading of the actual singleton classes if not already
loaded using the getInstance(String)
method.
All loaded singleton classes will be loaded using the class loader
that loaded this class, or by a specific class loader supplied at
construction time. Only singleton classes matching the bound
supplied as T
can be loaded, regardless if T
is
generic or not.
The class is abstract in order to preserve the generic information, and must be created in the following manner:
LoadableSingletonRegistry<Foo<? extends CharSequence>> registry = newIn the above example, only singleton types conforming toLoadableSingletonRegistry
<Foo<? extends CharSequence>>(){}; // default:StatelessSingletonRegistry
is used ClassLoader classLoader = ..;SingletonRegistry
<Foo<? extends CharSequence>> aggregate = ..; LoadableSingletonRegistry<Foo<? extends CharSequence>> registry = newLoadableSingletonRegistry
<Foo<? extends CharSequence>>(classLoader, aggregate){};
Foo<? extends CharSequence>
can be loaded.
Implementation notes:
This class cannot load singleton types implemented using enumerations, because
generic information from super-classes is used and all enumerations extend
java.lang.Enum
. They can still be acquired using the
StatefullSingletonRegistry.getInstance(Class)
method, though.
Field Summary | |
---|---|
private ClassLoader |
classLoader
The class loader used by this registry. |
Constructor Summary | |
---|---|
protected |
LoadableSingletonRegistry()
Constructor. |
protected |
LoadableSingletonRegistry(ClassLoader classLoader)
Constructor. |
protected |
LoadableSingletonRegistry(ClassLoader classLoader,
SingletonRegistry<T> registry)
Constructor. |
Method Summary | |
---|---|
T |
getInstance(String className)
Loads the singleton type represented by the class name supplied as className using the class loader that loaded this
registry, and then invokes StatefullSingletonRegistry.getInstance(Class) using
the loaded singleton type to return the singleton instance. |
Methods inherited from class dk.rode.thesis.singleton.StatefullSingletonRegistry |
---|
discard, getInstance, isSingleton, iterator, toString |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
---|
private final ClassLoader classLoader
Default class loader is the class loader that loaded this class.
Never null.
Constructor Detail |
---|
protected LoadableSingletonRegistry()
An instance of the StatelessSingletonRegistry
type
will be used to acquire the singleton instances.
The class loader that loaded this class will be used by this registry.
protected LoadableSingletonRegistry(ClassLoader classLoader)
An instance of the StatelessSingletonRegistry
type
will be used to acquire the singleton instances.
classLoader
- The class loader to use; cannot be null.
NullPointerException
- If classLoader
is null.protected LoadableSingletonRegistry(ClassLoader classLoader, SingletonRegistry<T> registry)
classLoader
- The class loader to use; cannot be null.registry
- The registry that will be used to acquire the
singleton instances; cannot be null.
NullPointerException
- If either argument is null.Method Detail |
---|
public T getInstance(String className) throws ClassNotFoundException
className
using the class loader that loaded this
registry, and then invokes StatefullSingletonRegistry.getInstance(Class)
using
the loaded singleton type to return the singleton instance. A cast is required to obtain the exact singleton type.
className
- The class name of the singleton type; cannot
be null.
NullPointerException
- If className
is null.
ClassNotFoundException
- If className
is illegal.
ClassCastException
- If className
represents a type
that is not an instance of T
, including if
T
is generic.
SingletonException
- If the class represented by className
is not a singleton type handled by this registry, or if the
type cannot be created when first requested.
|
Gunni Rode / rode.dk | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |