Strict Singletons in AS 3.0

With As 3.0 conforming to ECMA there you can no longer have private constructors which makes creating strict singletons a little bit more tricky. If I write a singleton, I want it written in a way that only a single instance can be created and if someone makes a mistake and tries to create an instance without using getInstance() an error will be thrown. This is a little workaround me and Rich came up with.


public class Singleton

private static var instance:Singleton;
private static var creatingSingleton:Boolean = false;

public function Singleton()
if( !creatingSingleton ) throw new Error( “Singleton and can only be accessed through Singleton.getInstance()” );

public static function getInstance():Singleton

if( !instance )
creatingSingleton = true;
instance = new Singleton();
creatingSingleton = false;

return instance;

19 Responses to “Strict Singletons in AS 3.0”

  1. Arrix says:

    Very useful tip! I think we should set creatingSingleton back to false before returning in the static function getInstance, or after a call to getInstance, creatingSingleton will hold true forever and the check in ctor will always pass.

  2. Arrix says:

    Sorry for my carelessness. creatingSingleton is set to false at the end of the constructor*_*

  3. Tink says:

    Hmmm you made me think about it though.

    The way it was, as you say, creatingSingleton was set to false in the constructor. This meant that if getInstance() was invoked and an instance had already been created, creatingSingleton would get set to true, and remain true as the constructor wouldn’t be invoked.

    I’ve now edited this, so that it is set to false within the getInstance() method.

  4. The flag on/off thing had been mentioned by Michael over at some time ago, however I personally never cared much for it. A colleague and I came up with a new approach that I dont think has been mentioned yet:

    static private var instance:Singleton;

    static public function getInstance():Singleton{
    if(instance == null) instance = createSingleton();
    return instance;

    static private function createSingleton():Singleton{
    return new Singleton(arguments.callee);

    public function Singleton(caller:Function = null){
    if(caller != createSingleton) throw new Error(“ERROR!”);

  5. Tink says:

    Hey Steve

    This post is actually from back in March.

    The approach you mention is posted on JAM. Both work just as well i guess. One question, why bother with arguments.callee instead of just passing the method through in teh params?

    static private function createSingleton():Singleton
    return new Singleton( Singleton.createSingleton );

  6. Ohh…I was lead to believe your post after this one was your newest one. Sorry about that.
    Hmm…someone else already of that huh? Darn….

    Anyway, sure either approach would work, I just like arguments.callee because it takes less effort to port to other singletons since you don’t have to replace the “Singleton” part of “Singleton.createSingleton”

  7. Tink says:

    cool i get what your saying about arguments.callee.

    This is the one i was refering to on JAM. Not the same as yours :) .

    public class Singleton
    private static var singleton : Singleton

    public static function getInstance() : Singleton
    if ( singleton == null )
    singleton = new Singleton( arguments.callee );
    return singleton;

    public function Singleton( caller : Function = null )
    if( caller != Singleton.getInstance ){
    throw new Error (“Singleton is a singleton class, use getInstance() instead”);
    if ( Singleton.singleton != null )
    throw new Error( “Only one Singleton instance should be instantiated” );

    //put instantiation code here

  8. Haha, wow….well its always neat to develop a solution from scratch and realize one of the big guys were thinkin the same thing. =]

  9. [...] tronicDevice to implement this function themselves. Using the methods developed to enforce Singletons in ActionScript 3 as a guid [...]

  10. [...]
    Singletons in AS 3.0
    August 31st, 2007 . by Sajeev


    Posted in Actionscript |


  11. [...] obe Flex and Flash. Stephen Downs has a great example for creating singletons in AS 3.0. His example is clean and simple. So head on over and che [...]

  12. Dowell says:

    One way to make a singleton is by using an outside-package-class as argument to the constructor. This way you don’t have to worry about run-time errors since it will be caught by the compiler.

    package my.dummy.package
    public class Singleton
    public function Singleton(theClass:CreationClass){ … }

    public function getInstance():void
    _instance = new Singleton(new CreationClass());
    class CreationClass {} //Not visible outside of file

  13. Clox says:

    “Singleton and can only be accessed…”, that “and” shouldn’t be there should it?

    Good stuff though =)

  14. Tink says:

    @ Clox

    Sorry it should say something like:

    MySingletonClass is a singleton and can only be accessed through MySingletonClass.getInstance()

    At the moment I’m creating all my singletons accessable using the same approach as Flex 2.0 uses, so instead of getInstance() i used a getter and the name of the class.


  15. [...] different posts from some well known ActionScript developers’ blogs I often follow; Stephen Down AKA T [...]

  16. jonny says:

    It’s always possible to try catch your constructor and pass anything, or null to it.

    That way, I still have the instance.

    There is one technique you can use to do the true singleton:

    But its a little overkill.

    Usually, the technique mentioned here is good enough.

    Just be careful not to use code like that in frameworks etc.

    People can and will use your code badly.

Leave a Reply