Actionscript 3.0中Singleton實現(xiàn) 修正篇
更新時間:2009年02月16日 12:38:03 作者:
說明:上一篇"一個簡單的Actionscript的單態(tài)模式類"的實現(xiàn)在Actionscript中報錯,具體原因會在這篇Blog中詳細說明。
在前面的blog中,代碼如下:
package Src
{
/**
* Written by Leezhm, 10th February, 2009
* Contact : Leezhm@126.com
*
* An example of singleton class
**/
public class CSingleton
{
// variable
private static var _instance = new CSingleton();
protected function CSingleton()
{
}
public static function getInstance():CSingleton
{
if (undefined != CSingleton._instance)
{
return CSingleton._instance;
}
else
{
throw Error("Could not create the instace!");
}
}
}
}
Rebuild會出現(xiàn)1153:A constructor can only be declared public.錯誤,錯誤原因在錯誤描述語句描述的很清楚,也就是Constructor在Actionscript中只能聲明為public。而我當時寫的時候,犯了習慣性的錯誤,因為我學習的C++和C#中寫singleton pattern總是將constructor聲明為protected或者private,所以也就"理所當然"地這樣寫了(還是應該好好重視每種編程語言的基礎,雖然都是標準的OO語言,但應該還是各有自己的特色的,不然也就沒吸引力了)。既然這樣,我們就無法保證用戶不用new來創(chuàng)建singleton class對象了,在我思考中,同QQ群上一位網(wǎng)友討論了哈,他給我推薦了一種解決方案,如下:
Public function CSingleton()
{
Throw Error("error!");
}
但后來通過自己的測試,發(fā)現(xiàn)這樣是不行的,Actionscript的異常機制貌似跟C#和C++不同,其實還是創(chuàng)建了對象,即使拋出了Exception(當然我沒有很深入的測試,也許結(jié)果并不正確,但這里我要推薦另一種在Actionscript中實現(xiàn)singleton pattern的方法)。后來自己在網(wǎng)上找到一本好書《Advanced Actionscript 3 with Design Pattern》,在它的Part III中的Chapter 4中找到了關(guān)于Actionscript中singleton的討論。
由于我們沒法把constructor聲明為private,那就可以給constructor傳遞一個private的variable,同樣可以達到不能new的目的。但是怎么樣才能讓一個只對singleton class有private作用域的variable呢,我們要注意Actionscript的一些規(guī)則,比如在一個AS文件中只能有一個Package,一個和AS文件同名的Main Class。但我們在AS文件的包外聲明一個class,而且這樣聲明的class只對package中的class有作用域,對包外是不可見的。所以可以如下來寫這個Singleton class:
package Src
{
/**
* Written by Leezhm, 14th February, 2009
* Contact : Leezhm@126.com
*
* An example of singleton class
**/
public class CSingleton
{
// variable
private static var _instance = new CSingleton(new SingletonEnforcer());
public function CSingleton(enforcer:SingletonEnforcer)
{
}
public static function getInstance():CSingleton
{
if (undefined != CSingleton._instance)
{
return CSingleton._instance;
}
else
{
throw Error("Could not create the instace!");
}
}
}
}
class SingletonEnforcer {}
BTW: 順便附上一張電子書中關(guān)于Actionscript 3中Singleton Pattern Class講解的照片,如下:
復制代碼 代碼如下:
package Src
{
/**
* Written by Leezhm, 10th February, 2009
* Contact : Leezhm@126.com
*
* An example of singleton class
**/
public class CSingleton
{
// variable
private static var _instance = new CSingleton();
protected function CSingleton()
{
}
public static function getInstance():CSingleton
{
if (undefined != CSingleton._instance)
{
return CSingleton._instance;
}
else
{
throw Error("Could not create the instace!");
}
}
}
}
Rebuild會出現(xiàn)1153:A constructor can only be declared public.錯誤,錯誤原因在錯誤描述語句描述的很清楚,也就是Constructor在Actionscript中只能聲明為public。而我當時寫的時候,犯了習慣性的錯誤,因為我學習的C++和C#中寫singleton pattern總是將constructor聲明為protected或者private,所以也就"理所當然"地這樣寫了(還是應該好好重視每種編程語言的基礎,雖然都是標準的OO語言,但應該還是各有自己的特色的,不然也就沒吸引力了)。既然這樣,我們就無法保證用戶不用new來創(chuàng)建singleton class對象了,在我思考中,同QQ群上一位網(wǎng)友討論了哈,他給我推薦了一種解決方案,如下:
復制代碼 代碼如下:
Public function CSingleton()
{
Throw Error("error!");
}
但后來通過自己的測試,發(fā)現(xiàn)這樣是不行的,Actionscript的異常機制貌似跟C#和C++不同,其實還是創(chuàng)建了對象,即使拋出了Exception(當然我沒有很深入的測試,也許結(jié)果并不正確,但這里我要推薦另一種在Actionscript中實現(xiàn)singleton pattern的方法)。后來自己在網(wǎng)上找到一本好書《Advanced Actionscript 3 with Design Pattern》,在它的Part III中的Chapter 4中找到了關(guān)于Actionscript中singleton的討論。
由于我們沒法把constructor聲明為private,那就可以給constructor傳遞一個private的variable,同樣可以達到不能new的目的。但是怎么樣才能讓一個只對singleton class有private作用域的variable呢,我們要注意Actionscript的一些規(guī)則,比如在一個AS文件中只能有一個Package,一個和AS文件同名的Main Class。但我們在AS文件的包外聲明一個class,而且這樣聲明的class只對package中的class有作用域,對包外是不可見的。所以可以如下來寫這個Singleton class:
復制代碼 代碼如下:
package Src
{
/**
* Written by Leezhm, 14th February, 2009
* Contact : Leezhm@126.com
*
* An example of singleton class
**/
public class CSingleton
{
// variable
private static var _instance = new CSingleton(new SingletonEnforcer());
public function CSingleton(enforcer:SingletonEnforcer)
{
}
public static function getInstance():CSingleton
{
if (undefined != CSingleton._instance)
{
return CSingleton._instance;
}
else
{
throw Error("Could not create the instace!");
}
}
}
}
class SingletonEnforcer {}
BTW: 順便附上一張電子書中關(guān)于Actionscript 3中Singleton Pattern Class講解的照片,如下:

相關(guān)文章
AS3 navigateToURL導致ExternalInterface 執(zhí)行失敗問題
AS3 navigateToURL導致ExternalInterface 執(zhí)行失敗問題2009-02-02Actionscript 3.0中Singleton實現(xiàn) 修正篇
說明:上一篇"一個簡單的Actionscript的單態(tài)模式類"的實現(xiàn)在Actionscript中報錯,具體原因會在這篇Blog中詳細說明。2009-02-02