The ActionScript Singleton

While it is true that ActionScript 3.0 lacks a private constructor - the loss of a programmatic fixture doesn't turn ActionScript into 'damaged goods.'

I stumbled upon a few critical blog posts (of ActionScript/Flex) that got me thinking about the big picture - of programming languages and application development. For their fire, the critics fuel was the lack of fixtures common to 'established' languages - one specifically mentioned the lack of the private constructor in ActionScript. O.k...

I need to mention at this point that I was about 80% finished with this blurb and ran across Samuel's post... the moral of the story there (for me) is that ActionScript 3.0 contains all of the nuts-and-bolts to keep pace with other more 'established' languages [MUWHAAHAHAHAAA]. I've expanded his example a bit - realizing what I feel is a pretty agreeable solution.


/**
 * @author Rick Winscot, Samuel Agesilas
 */
package com.quilix.samples
{
 
  import flash.utils.*;
 
  /**
   * The SingletonClass is a globally accessible object. Place the following 
   * reference in your primary application container or class. 
   *
   * private static var _singletonClass:SingletonClass = SingletonClass.getInstance();
   *
   * This 'dangling' reference will ensure that the SingletonClass is 
   * instantiated early in the application life cycle - preventing conflicts 
   * that may arise when using singletons in modules.
   */
  public class SingletonClass
  {
 
    /**
     * The Singleton 'self-reference' served up by getInstance().
     */
    private static var _singletonClass:SingletonClass = null;
 
    /**
     * Uses a scope constrained Constructor and ensures that the only 
     * class eligible to call the SingletonClass is itself.
     */
    public function SingletonClass( enforcer:SingletonEnforcer )
    {
      if ( getQualifiedClassName( super ) != "com.quilix.samples::SingletonClass" )
        throw new Error( "Invalid Singleton access. Please use SingletonClass.getInstance() instead." );
    }
 
    /**
     * The 'single' access point for your SingletonClass.
     */
    public static function getInstance():SingletonClass
    {
      if ( _singletonClass == null )
        _singletonClass= new SingletonClass( new SingletonEnforcer );
 
      return _singletonClass;
    }
 
 
  }// end SingletonClass
 
}// end package
 
/**
 * Used to enforce restricted instantiation of the SingletonClass.
 */
class SingletonEnforcer{}

There are a few caveats to any of the ActionScript 3.0 singleton approaches out there... but with the right frame of mind any problem has a reasonable solution. If you are inclined to call this a hack or kludge... realize that any 'concrete' feature in any 'established' language is implemented similarly under the hood.

( Some languages allow for a higher level of abstraction than others != 
          someone who makes furniture with an axe ? foo : bar );

Here is a quickie sample app that uses the SingletonClass - as always right click for source. ROWR!