singleton and inheritance in Java

I have a base class that captures some functionality common to two classes. In other words, I can create one base class and make these two classes subclasses of that base class. However, for each of these sub classes, the number of instances can be created is 1 (ie each sub class has to be a singleton). I googled and found that there's a reasonable debate going on over this. Although there are several solutions available, I am not sure whether they would fit in my case.

can anyone tell me how I should design this?


You can make each class separately a singleton, and make the base class abstract. Not sure what's the debate -- just that singletons, in general, aren't a great idea?


Use the Abstract factory pattern. Have a separate class with methods to retrieve the singletons, and let it hold the references to the singletons in instance variables or in a map.

You may not want the increased complexity, but frameworks like Spring were created to solve these kind of issues (among others).

It seems that Pico Container is alive and well, and it may be the simplest while still solid solution. Look at the inversion of control topics, and let the framework inject the singletons where you need them.

In short, don't try to make the singletons manage access to themselves. Delegate that on something else.

There's nothing inherently wrong in having singleton classes with complex inheritance. In fact, class hierarchies with private constructors (no instances) are very useful in many situations. You just have to decide how you want to manage the two important aspects of singletons: creation, and access.


I don't know if you need an example but I gave it a try. Without knowing any of your details this example is very vague. I am also here to learn so let us know what you end up implementing.

The Base class:

  public abstract class BaseClass {

        public void someMethod() {
            System.out.println("base class hello: " + this);
        }

        public abstract void someOtherMethod(String value);
    }

One of the subclasses:

public class SubClassOne extends BaseClass {

    private static SubClassOne instance;

    private SubClassOne() {}

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

    public void someOtherMethod(String value) {
        someMethod();
        System.out.println("sub class hello: " + value + " " + this);
    }

    public static void main(String[] args) {
        SubClassOne one = SubClassOne.getInstance();
        SubClassOne two = SubClassOne.getInstance();
        SubClassOne three = SubClassOne.getInstance();
        SubClassOne four = SubClassOne.getInstance();
        one.someOtherMethod("one");
        two.someOtherMethod("two");
        three.someOtherMethod("three");
        four.someOtherMethod("four");
    }
}
链接地址: http://www.djcxy.com/p/5748.html

上一篇: PHP获得真正的IP(代理检测)

下一篇: Java中的单例和继承