4

To begin with, I am NOT trying to create a subclass of a singleton class. (At least I'm sure I'm not trying to).

I have a class ClassA that is abstract.

I have two classes, ClassA1 and ClassA2 that extend ClassA.

I want ClassA1 and ClassA2 to be singleton classes. I could just write the code in each one to do so but I would prefer to write the code once in ClassA and reuse it in all of it's sub-classes. Is there a way to do this?

3
  • 7
    Given that it takes about three lines of extra code to make a class into a singleton, I am not sure that whatever potential saving you're hoping to make is really worth the extra complexity...
    – NPE
    May 14, 2012 at 17:00
  • What code are you using to guarantee the classes will be valid singletons?
    – Colin D
    May 14, 2012 at 17:01
  • aix, you're right. It isn't worth the extra complexity. The goal is form the super class in a way so that each subclass wouldn't have to rewrite any functionality and all of the core functions will be done by the super, unfortunately the concept of a singleton class makes this a little difficult. Colin D, i'm using a standard pattern, I just wanted to see if I could make it generic in some way.
    – Sababado
    May 14, 2012 at 18:01

3 Answers 3

6

Probably not - the singleton pattern requires static methods/fields, which would be shared by ClassA and all its subclasses if defined in one place.

1
  • 2
    Also, if you're going to make singletons, try looking at this pattern for a thread-safe singleton, assuming that's important to you.
    – Rob I
    May 14, 2012 at 17:05
1

I would suggest private constructors for ClassA1 and A2 and having static methods for object creation. This way you have complete control over object creation. The book EffectiveJava details out the adavatages of this approach.

1

The only place this is done is in the enum classes which extend Enum. It uses a language twist, and something like class ClassA<T extends ClassA>. Either you do:

public class ClassA {

    private static final Map<Class<? extends ClassA>, ClassA> singletons = new HashMap<>();

    public static <T extends ClassA> T get(Class<T> klazz) {
        T singleton = klazz.cast(singletons.get(klazz));
        if (singleton == null) {
            try {
                singleton = klazz.getConstructor().newInstance();
            } catch (InstantiationException | IllegalAccessException
                    | IllegalArgumentException | InvocationTargetException
                    | NoSuchMethodException | SecurityException e) {
                throw new IllegalArgumentException(e);
            }
            singletons.put(klazz, singleton);
        }
        return singleton;
    }
    protected ClassA() { }
}

Or you rethink your desire of plural singletons, and either do a general container lookup, (maybe with declarative XML or annotations) or a bean container. EJB 3.1 is very nice and simple.

2
  • This is similar to what I have now actually, I was curious to see if I could do this with out any parameters in the get method.
    – Sababado
    May 14, 2012 at 18:05
  • 1
    Unfortunately because of type erasure not; you need the class to instantiate the singleton.
    – Joop Eggen
    May 14, 2012 at 20:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.