The Singleton Pattern with AS3

Since I mentioned it in a previous article, I’ll expand upon the Singleton design pattern and cover its implementation within AS3…

First, what is it? As the name implies, a singleton is a single-instance class created within your application. Why would you want to limit a class to being instantiated only once? Quite simply, for global use. When storing a single set of values that need to be accessible to all other objects within the application (such as application settings or scores), you want to make sure that all objects are looking to the same set of source values; at which time, a singleton is your best friend.

So, how do we limit a class to a single instance that all other objects can reference? The solution is brilliantly simple: store the one, single class instance as a static member of its own class; then all other objects can reference that one instance by importing the class and referencing the static attribute. However, this begs another question… why ever create a class instance when you could just store all the singleton’s data as static members, like we do with Flash’s “Math” class? This is a good consideration, and using a static model works fine when you just need a switchboard for storing values. However, the static model does not give us all the features and flexibility that an object instance might… for example, we cannot dispatch events when values are changed without an instance of an EventDispatcher at our disposal. So, we use the Singleton pattern when we need an object instance.

Finally, implementation… The Singleton pattern has a rigid structure and is ultra-simple to set up. The basic requirements are that we have a private static member to store a class instance, a public static getter that will either return the one class instance or else create it if it does not yet exist, and finally some means of security to prevent other objects from ever being allowed to create an instance of the class. So, here it is:

    public class Singleton extends EventDispatcher
        private static var _instance:Singleton;
        public static function get instance():Singleton
            if (_instance == null) _instance = new Singleton(new SingletonEnforcer());
            return _instance;

        public function Singleton(enforcer:SingletonEnforcer):void

internal class SingletonEnforcer {}

The above is the full extent of the AS3 singleton pattern. The only tricky aspect of this pattern is the “enforcer”, which prevents other classes from instantiating our singleton. This enforcer works thanks to AS3’s ability to define private classes available only to the main packaged class. By requiring a private class as an argument of the constructor, we are effectively eliminating the ability of any other object in our application to meet instantion requirements for our singleton… Incidentally, I’d like to extend a handshake to the diabolically clever AS3 engineer out there who thought up that security measure. It is truly brilliant.

So, from the above framework you just need to flesh out the class with additional instance properties and methods that are specific to your application. To access those properties and methods from other classes, you just need to import the Singleton class, and then reference attributes as Singleton.instance.myCustomValue.


2 comments so far

  1. will on

    Your code was cuttoff. is that new Singleton(new SingletonEnforcer):Singleton

    • bigmac on

      Sorry for the cut-off. I swear at some point I’ll reskin this blog with a larger body column… (someday). In RE: I think you’re looking at the instance accessor, which works like this:

      public function get instance():Singleton {
      if (_instance == null) _instance = new Singleton(new SingletonEnforcer());
      return _instance;

      If you’d like more indepth information about this or any other pattern, I’d highly recommend checking out the O’Riley book “Design Patters for AS3”.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: