AS3 singleton implémentations

J'ai vu tellement de nombreuses implémentations de singletons, et je veux juste un singleton qui

1.- cas sur le premier appel
2.- les instances qu'une seule fois (duh)

Donc dans la performance et basse de la mémoire de la consommation, ce qui est la meilleure mise en œuvre pour cela?

Exemple 1

package Singletons
{
    public class someClass
    {
        private static var _instance:someClass;

        public function AlertIcons(e:Blocker):void{}

        public static function get instance():someClass{
            test!=null || (test=new someClass(new Blocker()));
            return _instance;
        }
    }
}
class Blocker{}

Exemple2

public final class Singleton
{
    private static var _instance:Singleton = new Singleton();

    public function Singleton()
    {
        if (_instance != null)
        {
            throw new Error("Singleton can only be accessed through Singleton.instance");
        }
    }

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

Exemple 3

package {

    public class SingletonDemo {
        private static var instance:SingletonDemo;
        private static var allowInstantiation:Boolean;

        public static function getInstance():SingletonDemo {
            if (instance == null) {
                allowInstantiation = true;
                instance = new SingletonDemo();
                allowInstantiation = false;
            }
            return instance;
        }

        public function SingletonDemo():void {
            if (!allowInstantiation) {
                 throw new Error("Error: Instantiation failed: Use SingletonDemo.getInstance() instead of new.");
            }
        }
    }
}
InformationsquelleAutor Ziul | 2012-11-10