Softwaredesign: Beispiel: Dynamisches Laden eines Singletons |
public
class DynamicSingleton {
// ...
private
static
DynamicSingleton ref = null;
protected
DynamicSingleton() {
// ...
}
public
static
void initDynamicSingleton(String classPath)
throws
ClassNotFoundException,
IllegalAccessException,
InstantiationException,
ClassCastException {
if ( ref == null ) {
Class c = Class.forName(classPath);
ref = (DynamicSingleton)c.newInstance();
}
}
public
static
DynamicSingleton getRef()
throws NullPointerException {
if ( ref == null )
throw
new NullPointerException();
// Alternative
//
// if ( ref == null )
// initDynamicSingleton("defaultSingletonClass");
return
ref;
}
}
|
Letzte Änderung: 13.04.2012 | © Prof. Dr. Uwe Schmidt |