singleton pattern(单件模式)的几种实现方式

来源:互联网 发布:淘宝站外活动平台 编辑:程序博客网 时间:2024/05/18 14:25
由于AS3.0本身不支持private/protected 构造器,所以实现一个singleton pattern很麻烦,必须采用曲线救国的方式。下面先列出几种实现方式,这几种方式都有一些漏洞。
(1)
  1. // faulty example
  2. package {
  3.     public class Singleton {
  4.         private static var singleton : Singleton;
  5.         
  6.         public static function getInstance() : Singleton {
  7.             if ( singleton == null )
  8.                 singleton = new Singleton( arguments.callee );
  9.             return singleton;
  10.         }
  11.        
  12.         //NOTE: AS3 does not allow for private or protected constructors
  13.         public function Singleton( caller : Function = null ) { 
  14.             if( caller != Singleton.getInstance )
  15.                 throw new Error ("Singleton is a singleton class, use getInstance() instead");
  16.             if ( Singleton.singleton != null )
  17.                 throw new Error( "Only one Singleton instance should be instantiated" );    
  18.             //put instantiation code here
  19.         }
  20.     }
  21. }
因为构造器只检测caller是否等于Singleton.getInstance,来确保它只被实例化一次,这样传一个同样的参数进去来饶过检查,就不能保证实例化次数。如下:
  1. var a:Singleton = new Singleton(Singleton.getInstance);
  2. var b:Singleton = new Singleton(Singleton.getInstance);
  3. // a !== b
  4. (2)
  5. // faulty example
  6. package whatever {
  7.   public class MySingleton {
  8.     public function MySingleton(singletonEnforcer:MySingletonEnforcer) { }
  9. private static var instance:MySingleton;
  10. pubic function getInstance():MySingleton {
  11. if (instance == null)
  12. instance = new MySingleton(new MySingletonEnforcer());
  13. return instance;
  14. }
  15. }
  16. }
  17. //this is in MySingleton.as but is outside the package block
  18. class MySingletonEnforcer {}
这个办法也是《design pattern》列出的标准做法,通过传递package外的内部class,来确保
实例化的个数。作者提到这样不能避免别人传递null参数来绕开。如下:
var a:MySingleton = new MySingleton(null);
var b:MySingleton = new MySingleton(null);

// a !== b
如果在构造器裏加入检测传参是否是null的判断,就可以避免了。
  1. package whatever {
  2.   public final class MySingleton {
  3.     public function MySingleton(singletonEnforcer:MySingletonEnforcer) {
  4.         if (singletonEnforcer == null) {
  5.             throw new Error ("MySingleton is a singleton class, use getInstance() instead");            
  6.         }
  7.     }
  8. private static var instance:MySingleton;
  9. public static function getInstance():MySingleton {
  10. if (instance == null)
  11. instance = new MySingleton(new MySingletonEnforcer());
  12. return instance;
  13. }
  14. }
  15. }
  16. //this is in MySingleton.as but is outside the package block
  17. class MySingletonEnforcer {}

例子1也可以用下面的方法完善:
  1. package {
  2.     public final class Singleton {
  3.         private static var singleton : Singleton;
  4.         
  5.         public static function getInstance() : Singleton {
  6.             if ( singleton == null )
  7.                 singleton = new Singleton( hidden );
  8.             return singleton;
  9.         }
  10.         private static function hidden():void {}       
  11.         //NOTE: AS3 does not allow for private or protected constructors
  12.         public function Singleton( caller : Function = null ) { 
  13.             if( caller != hidden )
  14.                 throw new Error ("Singleton is a singleton class, use getInstance() instead");
  15.             if ( Singleton.singleton != null )
  16.                 throw new Error( "Only one Singleton instance should be instantiated" );    
  17.             //put instantiation code here
  18.         }
  19.     }
  20. }

下面给出最简介的一种办法来实现:
  1. package {
  2.         public final class Singleton {
  3.                 private static var instance:Singleton = new Singleton();
  4. public function Singleton() {
  5. if( instance ) throw new Error( "Singleton and can only be accessed through Singleton.getInstance()" );
  6. }
  7. public static function getInstance():Singleton {
  8. return instance;
  9. }
  10. }
  11. }
这样做的妙处就在该单件类在被引用的时候,VM初始化过程中,静态变量为null而在构造器裏的检测
并没有被触发,所以无法使用new()/ new(***)的办法来实例化,只能通过getInstance()来调用。

来源:http://life.neophi.com/danielr/2006/10/singleton_pattern_in_as3.html

原创粉丝点击