I am in the middle of a programme that has a number of Singleton classes and they are therefore structured similar to the following:
Code: Select all
class Singleton {
private static Singleton instance = null;
private Singleton(){
// constructor code
}
public static Singleton getInstance(){
if (instance == null){
instance = new Singleton();
}
return instance;
}
// other code
}As there are a number of classes that behave in this way, I would like to enforce some form of uniformity to ensure that they all implement getInstance(). It would also allow me to take advantage of polymorphism and treat all these classes in a similar way. Normally such uniformity would be enforced using Interfaces and/or Abstract classes. Due to the static nature of the method though, these two directions are not possible.
Does anyone have any clue as to what I could do? My Google searches led to many people asking similar questions. While being very interesting and informative, the information didn't really answer my question.
It's not the end of the world if I can't get it done, but the uniformity would be nice, as well as being an extra piece of knowledge which is always a good thing.
Thoughts?