I can't get this disentangled.
public class MySingleton<T extends AUsefulClass>{
Map<String, T> myUsefulRegistry;
private static MySingleton<T> _instance = null;
public static instance(){
if(instance == null)
_instance = new MySingleton<T>();
return _instance;
}
}
public class Test{
@Test
public void testThis(){
MySingleton<SomeClass> mySingleton = MySingleton<SomeClass>().instance();
}
}
This parametrization is faulty because you cannot make a static reference to a nonstatic T. Still, I would like to build a parameterizable singleton class.
Here's a solution:
If you are planning on reassigning
myUsefulRegistry
, you will obviously have to omit the final modifier and also implement state based on thetype
parameter (by storing it to use again).Edit: There's an issue if the user passes a different type to the original. You may want to store the type in any case then, so you can check whether any proceeding calls to
getInstance(Class<T>)
are valid.