blog

Update: ActionScript 3 singleton pattern at Jozef Chúťka's blog

October 8, 2009

2 Comments

ActionScript Flex instance lock private singleton

Update: ActionScript 3 singleton pattern

In software engineering, the singleton pattern is a design pattern that is used to restrict instantiation of a class to one object. This is useful when exactly one object is needed to coordinate actions across the system. The concept is sometimes generalized to systems that operate more efficiently when only one object exists, or that restrict the instantiation to a certain number of objects (say, five). Some consider it an anti-pattern, judging that it is overused, introduces unnecessary limitations in situations where a sole instance of a class is not actually required, and introduces global state into an application. (wikipedia)

In search of the best actionscript 3 singleton pattern I came to this one. It uses private lock so user is not allowed to create new instance…

package
{
    public class MySingleton
    {
        private static const SINGLETON_LOCK:Object = {};
        private static const _instance:MySingleton = new MySingleton(SINGLETON_LOCK);

        public function MySingleton(lock:Object):void
        {
            if (lock != SINGLETON_LOCK)
                throw new Error("Invalid Singleton access. Use MySingleton.instance.");
        }

        public static function get instance():MySingleton
        {
            return _instance;
        }
    }
}

usage:

var singleton:MySingleton = MySingleton.instance;

Simplier method:

package
{
    public class MySingleton
    {
        private static const _instance:MySingleton = new MySingleton();
        
        public function MySingleton() 
        {
            if(instance)
                throw new Error("Invalid Singleton access. Use MySingleton.instance.");
        }

        public static function get instance():MySingleton
        {
            return _instance;
        }
    }
}

Lazy instance:

package
{
    public class MySingleton
    {
        private static var _instance:MySingleton;
        private static var creationPhase:Boolean = false;

        public function MySingleton()
        {
            if(!creationPhase)
                throw new Error("Invalid Singleton access. Use MySingleton.instance.");
        }

        public static function get instance():MySingleton
        {
            if(!_instance)
            {
                creationPhase = true;
                _instance = new Singleton();
                creationPhase = false;
            }
            return _instance;
        }
    }
}

You may Leave a comment or Subscribe to Comments RSS or Trackback this entry.

2 comments so far

Extending FacebookOAuthGraph Class at Jozef Chúťka's blog June 26, 2010 18:51 […] lets make our class singleton, so the whole flash project can simply access and use one […]

venkatesh July 22, 2012 19:37 hi i need the on confrece source and running script please contact me i will give you money how much you need

Leave a comment

Please be polite and on topic. Your e-mail will never be published.