I have a set of singleton classes and I want to avoid the template code. Here is what I have now:
public class Mammal { protected Mammal() {} } public class Cat extends Mammal { static protected Cat instance = null; static public Cat getInstance() { if (null == instance) { instance = new Cat(); } return instance; } private Cat() {
This works, and there is nothing wrong with that, except that I have many Mammal subclasses that the getInstance() method should replicate. I would prefer something like this if possible:
public class Mammal { protected Mammal() {} static protected Mammal instance = null; static public Mammal getInstance() { if (null == instance) { instance = new Mammal(); } return instance; } } public class Cat extends Mammal { private Cat() {
How to do it?
java code-formatting inheritance singleton
michelemarcon
source share