Revision: 18888
Updated Code
at October 11, 2009 12:50 by chrisaiv
Updated Code
/* Note: * Notice that both _instance & getInstance() are static. Thus, _instance exists before * any objects of the class have been created, and getInstance() can be called for the * class and so it can be called before there are any objects. */ package mvc { import mx.collections.ArrayCollection; /* Because ActionScript requires that all constructors are public, you can't prevent * anyone from making more Model Objects. Even without an explicit constructor, it * would appear as if it can't happen but unfortunately, but the compiler will still * synthesisize a default public constructor. Thus singleton behavior can only happen * by convention and the best you can do is communicate the intent of the pattern */ public class Model { private static var _instance:Model; [Bindable] public var buddies:ArrayCollection = new ArrayCollection( [ "ChrisAIV", "Jimmy", "Sandy", "Frosie" ] ); public static function getInstance():Model { if( !_instance ) _instance = new Model(); return _instance; } } }
Revision: 18887
Updated Code
at October 11, 2009 12:50 by chrisaiv
Updated Code
/* Note: * Notice that both _instance & getInstance() are static. Thus, _instance exists before * any objects of the class have been created, and getInstance() can be called for the * class and so it can be called before there are any objects. */ package mvc { import mx.collections.ArrayCollection; /* Because ActionScript requires that all constructors are public, you can't prevent * anyone from making more Model Objects. Even without an explicit constructor, it * would appear as if it can't happen but unfortunately, but the compiler will still * synthesisize a default public constructor. Thus singleton behavior can only happen * by convention and the best you can do is communicate the intent of the pattern */ public class Model { private static var _instance:Model; [Bindable] public var buddies:ArrayCollection = new ArrayCollection( [ "Sean", "Sam", "Seth", "Sally" ] ); public static function getInstance():Model { if( !_instance ) _instance = new Model(); return _instance; } } }
Revision: 18886
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at October 11, 2009 12:48 by chrisaiv
Initial Code
/* Note: * Notice that both _instance & getInstance() are static. Thus, _instance exists before * any objects of the class have been created, and getInstance() canbe called for the * class and so it can be called before there are any obejects. */ package mvc { import mx.collections.ArrayCollection; /* Because ActionScript requires that all constructors are public, you can't prevent * anyone from making more Model Objects. Event without an explicit constructor you * would think that it can't happen but unfortunately, but the compiler will still * synthesisize a default public constructor. Thus singleton behavior can only happen * by convention and the best you can do is communicate the intent of the pattern */ public class Model { private static var _instance:Model; [Bindable] public var buddies:ArrayCollection = new ArrayCollection( [ "Sean", "Sam", "Seth", "Sally" ] ); public static function getInstance():Model { if( !_instance ) _instance = new Model(); return _instance; } } }
Initial URL
Initial Description
Apparently Singletons in AS3 is pretty controversial so rather than provide the various solutions out there, my intent was to explain why it's such a headache.
Initial Title
AS3: Singleton Explanation
Initial Tags
Initial Language
ActionScript 3