views:

1150

answers:

5

Is there a way to make like a template singleton class so that when you extend it, the extended class is also a singleton class, so I can quickly make a new singleton class just my extending the template singleton class? I am using ActionScript 3.0 if it matters.

Thanks.

+5  A: 

The singleton pattern is designed in such a way that the type ought not to be inherited. I am not familiar with ActionScript but having an singleton type that is also able to be extended seems like a mistake.

If you were to do this then that would mean that the parent and child types could both have their single instances loaded into the same application. Since the types are, polymorphically, of type Parent, Parent would technically no longer be a singleton.

Andrew Hare
Thanks, I never even considered polymorphism bugs, I think you right. Thanks.
John Isaacks
"The singleton pattern is designed in such a way that the type ought not to be inherited."That's simply not true. It's designed to be only instantiated once.
Luke
@Luke: How can you have a type of which only one instance can ever exist and have that same type be the base class of other types? Would not an instance of a child type of this "singleton" be a second instance of the "singleton"?
Andrew Hare
@Andrew Hare: There is no such thing as one type of singleton. Many types of objects can be singletons and an app can use many singletons at the same time. Why not capture singleton behavior in a base class? Besides the fact that it doesn't really work (I tried to explain below, use an interface instead) there is nothing wrong with a singelton having a base class.
Luke
+3  A: 

First, I can't stress this enough - make sure your use of a Singleton is not the Singleton as an antipattern.

I don't know enough about ActionScript to comment intelligently about it, but typically a singleton instance is stored as a static variable in the class. This means that your subclass would inherit the static variable from the parent and thus the instance as well! So with that in mind, you won't be able to make an insta-singleton.

However, one thing you can do is make a Singleton interface, if all of your singletons need a common set of functionality such as destructors for a clean unload, etc. Any other hacks to make this work (I can think of a few) - I'm not sure it would be ethical for me to tell you because Singletons are better used sparsely (IMHO).

Elijah
A: 

A Singleton is defined in a class just like any other class and you can extend it (inherit from it) as much as you want. The point of a Singleton is to be instantiated only once, it says nothing about inheritance.

The question is, is it convenient to do so? If you try you will find that you need to re-implement (override) most of the behavior defined in the base class. You might as well start a new class from scratch. What might be better is to create a Singleton interface that defines the required behavior instead.

Luke
A: 

The flex framework utilizes this pattern for several managers.

If you look at the Flex code for mx.core.Singleton, it works by maintaining a map of the class name to an instance of that class. So before the first instance is created for the singleton, you are able to create a instance of any class in the hierarchy.

tylermac
A: 

In Java, in a singleton class, the constructor is private ! So, if class X has a private constructor, if you try to subclass it in class Y, the compiler will say that it cant invoke the super() constructor of X class, because it doesnt have a public visibility. So in Java, we cant sublass a singleton, I guess :/