2010-07-22 65 views
1

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

任何人都可以在actionscript中提供一个单例模式的示例吗?

回答

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

您需要修改Alxx的回答略有因为它不会停止工作新辛格尔顿()...

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

私有类是使用由辛格尔顿停止其他类简单地做新的辛格尔顿( ),然后通过getInstance()获得第二个实例。

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

2

基本上,所有的答案是对的,那些Reid和格里高尔的提供更多的编译时的安全性。我想,最好的办法就是然而,申报单例和接口通过一个静态类暴露私人实施者:

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

+0

如何确保静态类不会发生在被实例化? – ScootyPuff 2011-01-11 21:00:06

+0

@ ScootyPuff:为什么我需要确保?这是一个没有任何目的的空课。如果有人想实例化它,为什么我会阻止它们?他们可以创造一个空的物体。单例模式的要点是确保给定的对象仅实例化一次。 – back2dos 2011-01-12 12:16:52

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类和节约使用一个以上的单身时输入一些代码的类...

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