4

I want to force implementation of the singleton pattern on any of the extended classes of my parent class. That is, I only want one instance of every child class to ever be around (accessible through Child.INSTANCE or something like this).

Ideally what I would like would be for the Child.INSTANCE object to be made, and then no other object of type Parent to be made.

Currently I get my instances through something like:

public class Child extends Parent {
    public static final Child INSTANCE = new Child();
    ....

I wonder, can a java class be made static or something in some way?

Thanks =]

Dartoxian
  • 750
  • 1
  • 6
  • 10
  • 1
    To answer one part of the question: inner classes can be made static; as far as the rest of the question, I'm not entirely sure if what you're looking for can be done – Daniel DiPaolo Jan 03 '11 at 16:27
  • 1
    Are you trying to have a single instance of EACH class in the inheritance tree, or only of a single class for the entire tree? – Jonathan B Jan 03 '11 at 16:30
  • Thanks, I was aware of that though. Unfortunately that would make my class inappropriately long - and I think I'd have to make the Parent a static inner... – Dartoxian Jan 03 '11 at 16:31
  • What is the purpose of doing this? There might be a better design to use than to try to force each subclass to be a singleton. – jzd Jan 03 '11 at 16:31
  • @Jonathan A single instance of each class in the inheritance tree. @jzd This is just to stay loyal to the State Pattern, and there are parts of existing code ( =[ ) which are dependant upon the fact that the instance of Child that they see is the only one. – Dartoxian Jan 03 '11 at 16:32
  • @Dartoxian: In that case, my `enum` solution is just what the doctor ordered. :-) `enum`s are perfect for representing states in a state machine, for example. – C. K. Young Jan 03 '11 at 16:33

5 Answers5

9

Is the set of your child classes fixed? If so, consider using an enum.

public enum Parent {
    CHILD1 {
        // class definition goes here
    },

    CHILD2 {
        // class definition goes here
    };

    // common definitions go here
}

Since the OP mentioned about state pattern, here are two examples of enum-based state machines: a simple one and a complex one.

C. K. Young
  • 219,335
  • 46
  • 382
  • 435
1

It can be done, but I don't see the point. You would be better off using an enum IMHO.

enum {
  Singleton, 
  Child { /* override methods here * }
}

However to answer you question, you could do the following

class SingletonParent {
   private static final Set<Class> classes = new CopyOnArraySet();
   { if (!classes.add(getClass()) throw new AssertionError("One "+getClass()+" already created."); }
}
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
1

You are looking at the wrong design pattern.

Take a look at the Factory pattern. A factory can create a single instance of a class and then hand it out to anyone who wants it. The factory can hand out a singleton of the parent, any child, or anything else you want.

Jonathan B
  • 1,040
  • 6
  • 11
0

You can use a Java enum. Basically it makes a bunch of public static classes (like you do) but puts a few restrictions on them.

public enum Child {
   INSTANCE;
}

Note that enums are full classes, so you can easily add methods on a per-instance basis if you wish.

Edwin Buck
  • 69,361
  • 7
  • 100
  • 138
0

I will answer with a question I asked a while back that was very similar. I believe that this will do what you want it to do, but comes with all the restrictions mentioned in the answers.

Community
  • 1
  • 1
Riggy
  • 1,347
  • 1
  • 14
  • 26