I have a singleton that depends on another class so I'd like to inject this dependency in order to make it unit-testable. Since there's no way to use constructor injection on the singleton I guess I should use setter injcetion instead but I really don't like it since at some point someone (probably myself) will forget to call the setter. You could of course inject the dependent objects to the singleton's getInstance method but this is pretty ugly as well. Are there some better ways of solving this (not using tools like IoC containers)?
public class Singleton {
private ISomeDependency _dependency;
private static final Singleton INSTANCE = new Singleton();
private Singleton() {
}
public static Singleton getInstance() {
return INSTANCE;
}
...
}