Implementing the Singleton Pattern in Flex

One of the things that wasn’t immediately clear to me when I started working with Flex was how to implement a Singleton. In ActionScript 3, a developer can’t write static classes. So it takes some discipline on the developer’s part to to set up a class correctly so that it can be used as a singleton.

After trying a couple of different approaches, I settled on the following approach that takes advantage of accessor methods, and the ability of static methods to reference private members inside of their own class.

Let’s create a Carpenter class, which contains a set of tools: hammer, screwdriver, and clamp. We only need one carpenter and one set of tools throughout our application, but we’re going to make use of them frequently.

Here’s the class definition:

package com.risingspiral.example {

	public class Carpenter {

		private static var _hammer : Hammer;
		private static var _screwdriver : Screwdriver;
		private static var _clamp : Clamp;

		private static function get hammer() : Hammer {
			if (!_hammer) {
				_hammer = new Hammer;
			}
                        return _hammer;
		}

		private static function get screwdriver() : Screwdriver {
			if (!_screwdriver) {
				_screwdriver = new Screwdriver;
			}
                        return _screwdriver;
		}

		private static function get clamp() : Clamp {
			if (!_clamp) {
				_clamp = new Clamp;
			}
                        return _clamp;
		}

		public static function turnTheScrew(screw : Screw) : void {
			screwdriver.screw(screw);
		}

		public static function poundTheNail(nail : Nail) : void {
			hammer.pound(nail);
		}

		public static function tightenJoint(joint : Joint) : void {
			clamp.apply(joint);
		}

	}
}

Here’s the implementation:

var nail : Nail = new Nail();
var screw : Screw = new Screw();
var joint : Joint = new Joint();

Carpenter.poundTheHammer(nail);
Carpenter.turnTheScrew(screw);
Carpenter.tightenJoint(joint);

Ok, let’s breakdown how this is working.

First I define a set of tools that are private and static to the class:

private static var _hammer : Hammer;
private static var _screwdriver : Screwdriver;
private static var _clamp : Clamp;

Then, I define a private getter function for each of these members. This prevents the implementation from getting a copy of any of these tools and taking up memory with more instances of a tool class than we need.

private static function get hammer() : Hammer {
	if (!_hammer) {
		_hammer = new Hammer;
	}
        return _hammer;
}

Note that I don’t have a corresponding setter defined for the variable. Instead, the first time that the member is accessed, I check to see if it’s null. If it is, then we create a new instance of the tool.

I can now reference the hammer property from any of the public static methods defined in my class and exercise its methods, like pound().

I hope you find this useful. Please feel free to point out any improvements or potential downsides of this approach.

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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