Singleton Classes in Actionscript 3.0
27 Jul
Singleton Classes are based on the Singleton Design Pattern and have a single instance of the class throughout the application. When would one need a singleton class ? A simple example would be a database manager class which would instantiate a connection to the database at the application initialization and have methods for data manipulation of the database tables. In such a case multiple instances of this class is not required and will end up consuming memory and other issues.
How does one implement a Singleton Class ?
Recommended Way
- Declare a private variable of the type of the same singleton class inside it.
- Declare a private or protected constructor to prevent the initiation of the class
- Implement a public getInstance() method which will instantiate the variable declared in step 1 if it has not been instantiated, otherwise return the variable.
Pseudocode
1 2 3 4 5 6 7 8 9 10 11 12 | Class Singleton{ private variable SingletonInstance type Singleton ; private/protected Constructor(){ //not implemented } public function getInstance(){ if (SingletonInstance == null) then{ Instantiate SingletonInstance; } return SingletonInstance; } } |
The next obvious question, which is also the premise of this blog, is how does one implement a Singleton class in actionscript. Well, Actionscript 3.0 does not allow private or protected constructors thus leaving a lot of scope for experimentation and the quest for the perfect implementation, as this Google Search results with the words “singleton classes + actionscript” retruns quite a few implementation variations.
The one that I used for my implementation was sort of inspired by Andrew Trice’s Singleton in AS3 example.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | package { public class Singleton { private static var singleton : Singleton public static function getInstance() : Singleton { if ( singleton == null ) singleton = new Singleton( arguments.callee ); return singleton; } //NOTE: AS3 does not allow for private or protected constructors public function Singleton( caller : Function = null ) { if( caller != Singleton.getInstance ) throw new Error ("Singleton is a singleton class, use getInstance() instead"); if ( Singleton.singleton != null ) throw new Error( "Only one Singleton instance should be instantiated" ); //put instantiation code here } } } |
Have you come across a better Singleton Implementation ? I would definitely want to learn
Update : As Jove Shi pointed out on SDN the code above would break in the folowing case :
1 2 3 4 | var f:Function = Singleton.getInstance; var s1:Singleton = new Singleton(f); var s2:Singleton = new Singleton(f); trace("complete: " + (s1 == s2)); |
The suggested workaround would be :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | private static var instance:ModelLocator; public function ModelLocator(){ if(instance != null){ throw new YourError("ModelLocator singleton error!"); } instance = this; } public static function getInstance():ModelLocator{ if(instance == null){ instance = new ModelLocator(); } return instance; } |







Subscribe by Email