YellowBadger Blog http://www.yellowbadger.com/blog/index.cfm YellowBadger technology blog. Tue, 07 Sep 2010 16:NN:13 +0800 http://www.yellowbadger.com en Singletons in ActionScript 3 http://www.yellowbadger.com/blog/showPost.cfm?id=SingletonsinActionScript3 http://www.yellowbager.com/blog/showPost.cfm?id=SingletonsinActionScript3#comments Wed, 07 Feb 2007 16:NN:00 +0800 Spike http://www.yellowbadger.com/blog/showPost.cfm?id=SingletonsinActionScript3 Mike Chambers posted an entry on his blog about using Application.application.foo as an approach to allow global variables in Flex Applications. A couple of people, myself included, mentioned in the comments that an alternative was to use one or more singleton classes instead and Dave asked for a more complete example of using singletons. Well here it is Dave, for you and anyone else who might...]]> Mike Chambers posted an entry on his blog about using Application.application.foo as an approach to allow global variables in Flex Applications. A couple of people, myself included, mentioned in the comments that an alternative was to use one or more singleton classes instead and Dave asked for a more complete example of using singletons. Well here it is Dave, for you and anyone else who might be interested.

As with many things, Wikipedia has an excellent overview of what a singleton class is. All I'm going to do here is show how to create and use one in ActionScript 3...

A simple user preferences singleton class might look something like this:

package com.yellowbadger.example {

  public class Preferences {

    private static var _instance:Preferences;

    public static function getCurrentPreferences():Preferences {

      if (_instance == null) {

        _instance = new Preferences();

      }

    }

  }

}

This satisfies the requirement that all code in your application *can* use the same instance of the Preferences class, but it doesn't satisfy the requirement that all *must* use the same instance. The standard way to enforce the second requirement in Java is to make the constructor for the Preferences class private so that only code within the Preferences class can create the single instance. ActionScript 3 does not permit private constructors but we can get around it using a little bit of lateral thinking.

package com.yellowbadger.example {

  public class Preferences {

    private static var _instance:Preferences;

   

    public function Preferences(enforcer:SingletonEnforcer) {}

   

    public static function getCurrentPreferences():Preferences {

      if (_instance == null) {

        _instance = new Preferences(new SingletonEnforcer());

      }

    }

  }

}

 

internal class SingletonEnforcer {}

By declaring the SingletonEnforcer class outside the package, the compiler allows us to have a second class declared in the same file, but the class is not visible to any code outside Preferences.as. Since SingletonEnforcer is a required argument in the constructor of the Preferences class, no code outside Preferences.as can create an instance of the Preferences class. We have effectively guaranteed that all code will use the same instance of the Preferences class.

Any time we want to use the Preferences class in the application the code would look something like this:

private var prefs:Preferences = Preferences.getCurrentPreferences();

]]>