简体   繁体   English

为Actionscript实现单例类

[英]implementing singleton class for Actionscript

I know actionscript does not allowed private contstructor at any time and But if i want to write a sinlgleton class in action script So how to implement it in actionscript. 我知道动作脚本在任何时候都不允许私有构造函数,但是如果我想在动作脚本中编写一个sinlgleton类,那么如何在动作脚本中实现它。

Can anyone provide an sample example of a singleton pattern in actionscript? 谁能在动作脚本中提供单例模式的示例示例?

I use something like this: 我用这样的东西:

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{}

You need to alter Alxx's answer slightly as it doesn't stop new Singleton() from working... 您需要稍微更改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{}

The private class is used by the Singleton to stop other classes simply doing new Singleton() initially and then getting a second instance by doing getInstance(). Singleton使用私有类停止其他类,只需先执行新的Singleton(),然后通过执行getInstance()来获取第二个实例。

Note that this still isn't watertight... If someone is determined to break it, they can get access to the private class, but this is about the best option for Singletons. 请注意,这仍然不是不漏水的……如果确定要破坏它,则可以访问私有类,但这是Singletons的最佳选择。

basically, all answers are right, those of reid and gregor provide more compile time safety. 基本上,所有答案都是正确的,reid和gregor的答案提供了更多的编译时安全性。 I suppose, the best thing is however, to declare an interface for the singleton and a private implementor exposed through a static class: 我想,最好的办法是为单例和通过静态类公开的私有实现程序声明一个接口:

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

and then: 然后:

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");
    }
}

this doesn't rely on runtime errors for safety. 为了安全起见,这不依赖于运行时错误。 Also, it lowers coupling. 而且,它降低了耦合。

greetz 格里茨
back2dos back2dos

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!");
    }
}

Runtime check in lack of private constructor. 运行时检查缺少私有构造函数。

I use this approach ... 我用这种方法...

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;
        }
    }
}

... not as terse as some other methods but it's absolutely safe and there's no need for an empty package-level class. ...并不像其他方法那么简洁,但是它绝对安全,并且不需要空的包级类。 Also note the shortcut with SingletonException which is a class that extends the AS3 Error class and saves typing some code when using more than one Singleton ... 另请注意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.");
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM