AS单例模式

AS3.0

懒汉模式:要用的时候就去实例化它,也就是只声明不实例化:var sp:Sprite;

饿汉模式:使用之前去实例化它,声明变量的时候实例化: var sp:Sprite=new Sprite();

单例模式:一个类只有一个实例,并且只实例化一次

 1 package  antCodes
 2 {
 3     /**
 4      * ...
 5      * AS单例模式
 6      * @author Dong
 7      */
 8     public class Singleton 
 9     {
10         private static var _instance:Singleton;
11         
12         public function Singleton() 
13         {
14             if (_instance != null) {
15                 throw new Error("Abstract Method!");
16             }
17             _instance = this;
18         }
19         private static function getInstance():Singleton {
20             if (_instance == null) {
21                 _instance = new Singleton();
22             }
23             return _instance;
24         }
25     }
26 }



posted on 2012-04-09 12:22  沙漠之泉  阅读(287)  评论(0编辑  收藏  举报

导航