AS3 Tip: Singletons Pattern Demo

I will demo here how to use Singletons pattern.

First, for those who don’t know what is Singletons. Singleton is:
- is a useful Design Pattern for allowing only one instance of your class, but common mistakes can inadvertently allow more than one instance to be created. In this article, I’ll show you how that can happen and how to avoid it.
- is just a global dressed up to look like OOP. Question is “is it good or bad?”

IMO, Singleton in AS3 is a function that bridge a class to another class which only allow single instantiation. I agree it is just a static (global) function which has a private helper class, so I didn’t prefer this pattern of programming, although it is useful and I did used it in my latest project.

Here is a look for Singleton AS3 pattern done:

package {
	import flash.events.*;
	import flash.display.*;
 
	public class SingletonDemo extends Sprite {
 
		/// static var _instance use for getinstance();
		private static  var _instance:SingletonDemo;
 
		/// this will allow single isntance for this SingletonDemo class
		public function SingletonDemo(singletonEnforcer:SingletonEnforcer) {
		}
 
		/// static function getinstance() will return SingletonDemo
		public static function getInstance():SingletonDemo {
			if (_instance == null) {
				_instance=new SingletonDemo(new SingletonEnforcer  );
			}
			return _instance;
		}
	}
}
/// private helper class. this is to blocker of the clas SingletonDemo
internal class SingletonEnforcer { }

How to use this example? If you have another class example Controller, then you can call public functions and variables inside of the SingletonDemo class. Here is a demo how to use a Singleton pattern in AS3:
This is the SingletonDemo class

// SingletonDemo class
package {
	import flash.events.*;
	import flash.display.*;
 
	public class SingletonDemo extends Sprite {
 
		/// private static var _instance use for getinstance()
		private static  var _instance:SingletonDemo;
 
		/// for demo purposes
		public var isTest:Boolean = true;
		public var isSecondInstance:Boolean = true;
		public var isEnd:Boolean = true; 
 
		/// this will allow single instance for this SingletonDemo class
		public function SingletonDemo(singletonEnforcer:SingletonEnforcer) {
			trace("Single instantiate");
		}
 
		/// publci static function getinstance() will return SingletonDemo
		public static function getInstance():SingletonDemo {
			if (_instance == null) {
				_instance = new SingletonDemo(new SingletonEnforcer);
			}
			return _instance;
		}
	}
}
/// private helper class. this is to blocker of the clas SingletonDemo
internal class SingletonEnforcer { }

This is the Main class:

// Main class
package {
	import flash.display.MovieClip;
	import flash.events.Event;
 
	public class Main extends MovieClip {
 
		// Main function
		public function Main():void {
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
 
		private function init(e:Event = null):void {
			removeEventListener(Event.ADDED_TO_STAGE, init);
			// entry point
 
			trace(SingletonDemo.getInstance().isTest);
			trace(SingletonDemo.getInstance().isSecondInstance);
			trace(SingletonDemo.getInstance().isEnd);
 
			// trace ouput:
			// Single instantiate
			// true
			// true
			// true
 
			/// you can notice that it will only instantiate the class SingletonDemo once.
		}
	}
}

You can recode the SingletonDemo according with your need, and you can also rename the class. Just follow the pattern and it will look nice. Again, me and there are programmers which are not prefer this kind of pattern as OOP.

Bookmark and Share

3 Comments to “AS3 Tip: Singletons Pattern Demo”

  1. By PaulS, March 31, 2011 @ 6:31 am

    Thank you, crystal clear. This will be useful for many programmers

  2. By peterson lee, June 18, 2011 @ 8:38 pm

    This was really an attention-grabbing matter, I’m very lucky to be able to find this from yahoo.

  3. By thienhaflash, July 9, 2011 @ 10:38 am

    not sure why you need to extends from Sprite for singleton implementation :(

RSS feed for comments on this post. TrackBack URI

Leave a Reply