The Simple Singleton
Welcome to your introduction into design patterns, starting with one of the most basic design patterns, the Singleton Pattern.
First, the definition. The Singleton Design Pattern is created to limit the Singleton object to only a single instance in an entire application. Simple and Powerful.
This is a great way to have a single access point for an object, however some programmers frown at Singleton objects for of a variety of reasons. First and foremost, new programmers can often find themselves using Singletons for all data access points and storage within their application. This can be a serious issue because Singletons aren’t garbage collected (traditionally). On many occasions there are other ways to manage your application’s data, but in some many situations your object can lend itself to only having a singular occurrence of an object, such as a data object that keeps information about the currently logged in application user – since there is only one user on an application at a time.
One other wonderful thing about the singleton object, is based on it’s way of instantiating the object, you don’t have to worry about the object from not being available. The object is available as soon as you call it.
So lets look at the coding:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | package com.unitedmindset.model // this is the package information { [Bindable] //By setting a class as "Bindable", all variables within the Class are bindable /** * MySingletonObject Class **/ public class MySingletonObject // this is a class information { static private var _instance:MySingletonObject; //instance information /** * The Constructor * <p>The constructor requires a SingletonEnforcer, so that another * instance of the constructor can't be called directly.</p> **/ public function MySingletonObject(enforcer:SingletonEnforcer) { } /** * Most Singleton Objects have a point of entry, the "getInstance" static function. * <p>The "getInstance" static function checks to see if the "_instance" private * variable exists, if it does return the existing "_instance", if not, return * a new MySingleton "_instance" variable.</p> **/ public static function getInstance():MySingletonObject { if(MySingletonObject._instance==null) MySingletonObject._instance = new MySingletonObject(new SingletonEnforcer()) return MySingletonObject._instance; } /** * This is an example of a variable available in the Singleton Class **/ public var myValue:String = "myValue"; } } /** * A Class, found declared such as this one, is only available to the package it is found in. * <p>This ensures that the class can only be called from the Singleton Object * "getInstance" static function</p> **/ class SingletonEnforcer{} |
Now you can call this class in a variety of ways:
1 | var a:MySingletonObject = MySingletonObject.getInstance(); |
And then get return the “myValue” variable through:
1 | var b:String = a.myValue; |
OR you can simplify the call through:
1 | var b:String = MySingletonObject.getInstance().myValue; |
Now go enjoy your new design pattern in your Flex applications!





Thanks for this simple yet very useful example.