Singleton Registry

A Facade to handle Concrete Singleton creation in a AbstractSingletonPattern

Code Sample:

public class SingletonRegistry {

private SingletonRegistry() {
}
private static ConcreteSingleton1 concreteSingleton1;
private static ConcreteSingleton2 concreteSingleton2;
public static Singleton getSingleton(final String which) {
try {
if("ConcreteSingleton1".equalsIgnoreCase(which)) {
return getInstanceOf1();
}
else if("ConcreteSingleton2".equalsIgnoreCase(which)) {
return getInstanceOf2();
}
}
catch (SingletonException se) {
return se.getInstance();
}
throw new RuntimeException("Match not found exception");
}
private static Singleton getInstanceOf1() throws SingletonException {
if(concreteSingleton1 == null) {
concreteSingleton1 = new ConcreteSingleton1();
}
return concreteSingleton1;
}
private static Singleton getInstanceOf2() throws SingletonException {
if(concreteSingleton2 == null) {
concreteSingleton2 = new ConcreteSingleton2();
}
return concreteSingleton2;
}

}

-- NitinVerma