ActionScript Singleton
snipped by vixiom
The singleton pattern is a design pattern that is used to restrict instantiation of a class to one object. Often a singleton is used as the Model in a Model View Controller (MVC) structure. Useful if you're looking for a quick (and better) way to store global variables and data than dumping them in a global or root holder.
A Singleton class (Model.as)
package { public class Model { private static var instance:Model; // vars public var myVar:String; ////////////////////////////////////////////////////////////////////// // // instance // ////////////////////////////////////////////////////////////////////// public static function getInstance():Model { if ( instance == null ) instance = new Model(); return instance as Model; } } }
How to use
import Model; private var model:Model = Model.getInstance(); model.myVar = "something";
You can also make the entire Model bindable in Flex...
package { [Bindable] public class Model...
Usage...
<mx:Script> <![CDATA[ import Model; [Bindable] private var model:Model = Model.getInstance(); model.myVar = "something"; ]]> </mx:Script>




