在Actionscript 3中实现单例模式类的最佳方法是什么?
时间:2020-03-06 14:42:01 来源:igfitidea点击:
由于AS3不允许使用私有构造函数,因此构造单例并确保未通过" new"显式创建构造函数的唯一方法似乎是传递单个参数并进行检查。
我听说过两个建议,一个是检查调用方并确保它是静态的getInstance(),另一个是在同一包名称空间中有一个私有/内部类。
传递给构造函数的私有对象似乎更可取,但是看起来我们不能在同一包中拥有私有类。这是真的?更重要的是,这是实现单例的最佳方法吗?
解决方案
如果再次调用构造函数,Cairngorm使用的模式(可能不是最好的)是在构造函数中引发运行时异常。例如:
public class Foo {
private static var instance : Foo;
public Foo() {
if( instance != null ) {
throw new Exception ("Singleton constructor called");
}
instance = this;
}
public static getInstance() : Foo {
if( instance == null ) {
instance = new Foo();
}
return instance;
}
}
我们可以像下面这样获得一个私人班级:
package some.pack
{
public class Foo
{
public Foo(f : CheckFoo)
{
if (f == null) throw new Exception(...);
}
}
static private inst : Foo;
static public getInstance() : Foo
{
if (inst == null)
inst = new Foo(new CheckFoo());
return inst;
}
}
class CheckFoo
{
}
我已经使用了一段时间,我相信我最初是从所有地方的维基百科获得的。
package {
public final class Singleton {
private static var instance:Singleton = new Singleton();
public function Singleton() {
if( Singleton.instance ) {
throw new Error( "Singleton and can only be accessed through Singleton.getInstance()" );
}
}
public static function getInstance():Singleton {
return Singleton.instance;
}
}
}
这是问题的有趣摘要,它导致了类似的解决方案。
enobrev答案的一个略微修改是将实例作为吸气剂。有人会说这更优雅。另外,如果在调用getInstance之前调用构造函数,enobrev的答案将不会强制执行Singleton。这可能并不完美,但我已经对此进行了测试,并且可以正常工作。 (在"带有设计模式的高级ActionScrpt3"一书中,肯定也存在另一种好的方法)。
package {
public class Singleton {
private static var _instance:Singleton;
public function Singleton(enforcer:SingletonEnforcer) {
if( !enforcer)
{
throw new Error( "Singleton and can only be accessed through Singleton.getInstance()" );
}
}
public static function get instance():Singleton
{
if(!Singleton._instance)
{
Singleton._instance = new Singleton(new SingletonEnforcer());
}
return Singleton._instance;
}
}
}
class SingletonEnforcer{}

