Succinct Singletons
A singleton can be a very useful and essential Object-Oriented device in many instances (no pun intended). Singleton classes only allow one instance of that particular class, and this can be very useful in situations where you want one object to be used throughout your Flash/Flex application instead of many separate ones. I usually use singleton classes for classes that merely act as utility proxies to my applications.
Below is the code to create a very succinct singleton
package { public class Singleton { private static var _instance:Singleton; public static function getInstance():Singleton { if(!_instance) _instance = new Singleton(); return _instance; } } }
In the upcoming API i’m planning on releasing, i’ll be using Singletons extensively, and the API will – of course – be open source.
| Print article | This entry was posted by Danny Kopping on February 11, 2009 at 9:29 pm, and is filed under ActionScript 3.0, Flash, Flex, Lessons. Follow any responses to this post through RSS 2.0. You can skip to the end and leave a response. Pinging is currently not allowed. |