public class Singleton extends Object
This class provide the implementation of a thread-safe, generic singleton
pattern.
instance(java.lang.Class)
is a
thread-safe operation for both retreval and creation.
Derived classes are responsible
for initializing themselves in a thread-safe manner. If initialization is
required, the code involved is contained in a synchronized
block tied to the Singleton
class.
Deriving classes should declare a non-public default constructor. This class assumes the existing of a default constructor (ie, takes no parameters) and uses reflection to locate the constructor if an instance of a particular derived class has not yet been instantiated. For safety reasons, derived classes should not declare any public constructors.
This class provides no facilities for calling a non-default constructor and initialization of the created instance is left to the derived class.
The expect form of a derived class looks is as follows:
public class IntegerContainer
extends Singleton
{
// instance local data
public int a;
// private constructor for initialization
private IntegerContainer()
{
a = 1;
}
// convenience method
public static IntegerContainer instance()
throws InstantiationException,
IllegalAccessException,
NoSuchMethodException,
IllegalArgumentException,
InvocationTargetException,
ExceptionInInitializerError,
SecurityException
{
return IntegerContainer.instance(IntegerContainer.class);
}
}
The addition of the simplified instance()
is purely for the
convenience of the programmer. It simplifes the invocation and reduces
accidental type mis-matches. With this additional method, getting an
instance is as simple as :
IntegerContainer i = IntegerContainer.instance();
Modifier | Constructor and Description |
---|---|
protected |
Singleton()
Constructor.
|
Modifier and Type | Method and Description |
---|---|
static Map<Class<? extends Singleton>,Singleton> |
getInstances()
retrieve the instances backing store.
|
static <U extends Singleton> |
instance(Class<U> c)
Access the instance backing the singleton
|
protected Singleton()
Scope is protected for instantiation control.
public static <U extends Singleton> U instance(Class<U> c) throws InstantiationException, IllegalAccessException, NoSuchMethodException, IllegalArgumentException, InvocationTargetException, ExceptionInInitializerError, SecurityException, NullPointerException
This method returns an existing derived
Singleton
instance or
allocates a new instance. This method uses reflection, specifically
Constructor.newInstance(java.lang.Object[])
and
Class.getDeclaredConstructor(java.lang.Class[])
to
accomplish this. The method also assumes the existence of the default
constructor taking zero parameters and will throw
NoSuchMethodException
if
the default constructor is not found.
This is a thread-safe operation.
U
- the type parameter of the derived object object.c
- the Class
object corresponding to U.Singleton
object.InstantiationException
- if the class that declares the
underlying constructor represents an abstract class.IllegalAccessException
- if this Constructor object
enforces Java language access control and the underlying
constructor is inaccessible.NoSuchMethodException
- if the default constructor is not
found.IllegalArgumentException
- if the number of actual and
formal parameters differ; if an unwrapping conversion for
primitive arguments fails; or if, after possible unwrapping, a
parameter value cannot be converted to the corresponding formal
parameter type by a method invocation conversion; if this
constructor pertains to an enum type.InvocationTargetException
- if the underlying
constructor throws an exception.ExceptionInInitializerError
- if the initialization
provoked by this method fails.SecurityException
- if the security manager is present
and prevents access. See Class.getDeclaredConstructor(java.lang.Class[])
NullPointerException
- if c is null.Copyright © 2013–2020 Michael Lasmanis. All rights reserved.