Actionscript Design Patterns: the Singleton
I've been wanting to write down some of the stuff that's in my head for awhile. One of the main things I've wanted to do were some tutorials. For the most part, I've encountered two types of Flash developers at agencies.
- Those who know design patterns, but don't want to share the information, and frequently use them to write poor code.
- Those who are not familiar with design patterns, thought they've heard the buzz word and sometimes ignorantly criticize it.
I'd like to start off with one of the most basic patterns out there, which is also one of the most commonly used.
The singleton is simply an object, or class, that is solely limited to one instance. This same instance can be viewed by any class or object that wants to use its methods, and view it's public properties.
This comes in handy when you want to share data with multiple classes, for instance the current score on a game you're making. Without the singleton you'd have to put the score in some sort of loosely typed global object, where anything can access it and change it's values. Generally, I like to keep my code locked down, so I know which classes are accessing each other. I also like to keep as few dependencies as possible.
Let's take a look at a simple game singleton class, in Actionscript 2.0 first, then Actionscript 3.0:
-
class GameSingleton
-
{
-
//the _instance variable is a static variable that holds the single instance
-
//of GameSingleton
-
static private var _instance:GameSingleton;
-
-
//the score variable will only be available through getter/setters
-
private var _score:Number;
-
-
//I use a private constructor so that only GameSingleton itself can create an instance
-
private function GameSingleton()
-
{
-
-
}
-
-
/*
-
* I use a static getter to retrieve the
-
*_instance variable to whom ever needs it
-
*/
-
static public function get instance():GameSingleton
-
{
-
//if this is the first time the instance
-
//is requested, make a new one and store it
-
if (!_instance)
-
_instance = new GameSingleton();
-
-
return _instance;
-
}
-
-
public function get score():Number
-
{
-
return _score;
-
}
-
-
public function set score(val:Number):Void
-
{
-
_score = val;
-
}
-
-
}
Now here's the AS3 version:
-
package
-
{
-
/**
-
* ...
-
* @author Ryan Luce
-
*
-
* Actionscript 3 sample singleton.
-
*/
-
public class GameSingleton
-
{
-
-
static private var _instance:GameSingleton;
-
-
private var _score:int = 0;
-
-
public function GameSingleton()
-
{
-
-
}
-
-
/*
-
* I use a static getter to retrieve the
-
* _instance variable to whom ever needs it
-
*/
-
public static function get instance():GameSingleton
-
{
-
//if this is the first time the instance
-
//is requested, make a new one and store it
-
if (!_instance)
-
_instance = new GameSingleton();
-
-
return _instance;
-
}
-
-
public function get score():uint
-
{
-
return _score;
-
}
-
-
public function set score(val:int):void
-
{
-
_score = val;
-
}
-
}
-
-
}
As you can see both are very similar. They both have the same implementation and use, here is an example of how I would use this in a game:
-
private function shotLanded():void
-
{
-
//more typically, in the constructor of any particular class,
-
//you would declare an instance
-
//such as: private var
-
// _gameSingleton:GameSingleton = GameSingleton.instance
-
-
GameSingleton.instance.score += 1;
-
}
Singletons are used in a variety of cases, and can really help just about any flash app you are doing. They help control your data by enclosing all variable in one single instance of a class, and allowing and class in your application to access that single instance. Throw away your _global as2 reference, and use Singleotns instead! Create less dependencies by allowing data to be shared in one, none connected instance.