为Actionscript实现singleton类

时间:2010-07-22 05:45:27

标签: flex actionscript-3 design-patterns singleton

我知道actionscript在任何时候都不允许私有contstructor但是如果我想在动作脚本中编写一个sinlgleton类那么如何在actionscript中实现它。

任何人都可以在动作中提供单例模式的示例吗?

5 个答案:

答案 0 :(得分:8)

我使用这样的东西:

package singletons
{

    [Bindable]
    public class MySingleton
    {
        private static var _instance:MySingleton;

        public function MySingleton(e:Enforcer)  {
            if(e == null) {
                throw new Error("Hey!  You can't do that!  Call getInstance() instead!");
            }   
        }

        public static function getInstance():MySingleton {
            if(_instance == null) {
                _instance = new MySingleton (new Enforcer);
            }
            return _instance;
        }
    }
}

// an empty, private class, used to prevent outside sources from instantiating this locator
// directly, without using the getInstance() function....
class Enforcer{}

答案 1 :(得分:4)

你需要稍微改变Alxx的答案,因为它不能阻止新的Singleton()工作......

public class Singleton {
    private static var _instance : Singleton;

    public function Singleton( newBlocker : ClassLock ) {
    }

    public static function getInstance() : Singleton {
        if ( _instance == null ) {
            _instance = new Singleton( new ClassLock() );
        }
        return _instance;
    }
}
class ClassLock{}

Singleton使用私有类来阻止其他类最初只执行新的Singleton(),然后通过执行getInstance()来获取第二个实例。

请注意,这仍然不是水密的...如果有人决心打破它,他们可以访问私人课程,但这是单身人士的最佳选择。

答案 2 :(得分:2)

基本上,所有答案都是正确的,reid和gregor的答案提供了更多的编译时安全性。我想,最好的方法是为单例声明一个接口,并通过静态类声明私有实现者:

package {
    interface IFoo {
        function foo():void;
    }
}

然后:

package Foo {
    private static var _instance:IFoo;
    public static function getInstance():IFoo {
        if (_instance == null) _instance = new Impl();
        return _instance;
    }
}
class Impl implements IFoo {
    public function foo():void {
        trace("fooooooooooooooooooo");
    }
}

这不依赖于运行时错误的安全性。此外,它降低了耦合。

格尔茨
back2dos

答案 3 :(得分:1)

public class Singleton {
    private static var _instance:Singleton;

    public **static** function get instance():Singleton
    {
        if (_instance == null)
        {
             _instance = new Singleton();
        }
        return _instance;
    }

    public function Singleton()

    {
        if (_instance != null) throw new Error("You can't create Singleton twice!");
    }
}

缺少私有构造函数的运行时检查。

答案 4 :(得分:1)

我使用这种方法......

package
{
    public class Main
    {
        private static var _instance:Main;
        private static var _singletonLock:Boolean = false;

        /**
         * Creates a new instance of the class.
         */
        public function Main()
        {
            if (!_singletonLock) throw new SingletonException(this);
        }

        /**
         * Returns the singleton instance of the class.
         */
        public static function get instance():Main
        {
            if (_instance == null)
            {
                _singletonLock = true;
                _instance = new Main();
                _singletonLock = false;
            }
            return _instance;
        }
    }
}

...不像其他方法那样简洁,但它绝对安全,不需要空包级别的类。另请注意SingletonException的快捷方式,它是一个扩展AS3 Error类的类,并在使用多个Singleton时保存输入一些代码......

package
{
    public class SingletonException extends Error
    {
        public function SingletonException(object:Object)
        {
            super("Tried to instantiate the singleton " + object + " through it's constructor."
                + " Use the 'instance' property to get an instance of this singleton.");
        }
    }
}