December 28, 2006

Singleton in ActionScript 3.0

There are some ways to create singleton in ActionScript 3.0, though I prefer public "facade" with static initialization of internal class:

Internal class (it should be in the same package with "facade"):

Usage example:

5 comments:

Anonymous said...

But you could create 2 instances of AuthenticationManager right?

Unknown said...

Yes, though you can't create AuthenticationManagerInstance instances, because it's internal, and the only instance of it could be referenced via AuthenticationManager.instance. Sure, constructor that throws exception could be added to the AuthenticationManager facade, but I don't see the reason. ;)

Unknown said...

Ah but, what's stopping me from writing

AuthenticationManager.instance = null;

;)

Unknown said...

Ok, you could change static property to static method with lazy initialization. ;)

Anonymous said...

I just came across this again, and I guess if you were storing a refence to your singleton you'd aslo have to type it as differenet to your public singleton class (or what you would think is the singleton class).

i.e. although publically the Manager is the singleton, the return object needs the be type as Instance.

var singleton:Instance = Manager.instance;