Singleton Class in ActionScript3 | Phoxer.com
Singleton Class in ActionScript3
08/02/2009


One of the main problems we have in the programming is the global data access.
So i Developed a Class based on the Pattern of Development Singleton.

"The singleton pattern is a design pattern that is used to restrict instantiation of a class to one object"

With my class, we can have global access to data stored in some kind of "data object memory"

let's see how it works:

import phoxer.Singleton;
 
var sng:Singleton = Singleton.getInstance();
 
//whether to save the Root in the data Object
sng.data.Root = this;
 
//Any data can be stored and read from either side
sng.data.userName = "phoxer";

Then when we are in any instance of our script we can implement the class and read the "data object".

Here is my class Singleton:

/**
by .:[PHOXER]:.
http://www.phoxer.com
v 1.0; 
*/
package phoxer{
	public class Singleton {
		public static var instance:Singleton;
		public var data:Object;
        public static function getInstance():Singleton{
            if( instance == null ) instance = new Singleton( new SingletonEnforcer() );
            return instance;
        }
 
        public function Singleton(pvt:SingletonEnforcer){
          	data= new Object();
        }
 
 
	}
}
internal class SingletonEnforcer{}




Canal Rss