JD Isaacks
JD Isaacks

Reputation: 57974

extending a Singleton class?

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.

Upvotes: 1

Views: 5858

Answers (5)

alinnemet
alinnemet

Reputation: 301

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 :/

Upvotes: -1

tylermac
tylermac

Reputation: 489

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.

Upvotes: 0

Luke
Luke

Reputation: 21236

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.

Upvotes: -1

Elijah
Elijah

Reputation: 13604

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).

Upvotes: 5

Andrew Hare
Andrew Hare

Reputation: 351476

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.

Upvotes: 7

Related Questions