mercredi 7 janvier 2015

How to Inherit singleton

Sometimes there is a demand for inherit a singleton but because in singleton you are using a static reference and static method that cannot be overridden.


For example (Java):



public class Singleton {
private static Singleton instance = null;

public static Singleton getInstance() {
if (instance == null)
instance = new Singleton();
return instance;
}
}


If i would inherit "Singleton" with "SingletonChild" class i won't be able to generate an instance by calling getInstance() method. If i will create another getInstanceChild() method the base method: getInstance() also will be exposed.


Aucun commentaire:

Enregistrer un commentaire