Singleton in AS3
Lots of discussion, loads of different versions, I’m using this and still haven’t noted any “flaws”:
// ActionScript file package { public class MyTestClass { /* singleton */ { trace ( "Initialising MyTestClass singleton" ); _singleton = true; _instance = new MyTestClass ( ); _singleton = false; } private static var _instance : MyTestClass; private static var _singleton : Boolean; public var strTestVariable : String = "Was the singleton test successful?"; public function MyTestClass ( ) : void { trace ( "Constructor" ); if ( ! _singleton ) { throw new Error ( "Error: Instantiation failedn Please use MyTestClass.getInstance ( )" ); } } public static function getInstance ( ) : MyTestClass { return _instance; } } }