實現AS3的Singleton的兩種方法
實現AS3的Singleton的兩種方法
- 作者:柳大·Poechant
- 郵箱:zhongchao.ustc#gmail.com (# -> @)
- 部落格:blog.csdn.net/poechant
- 日期:April 8th, 2012
1 建構函式隨機引數
利用隨機數,當做建構函式的引數,實現建構函式無法隨意呼叫。
package {
class Singleton {
private static var _instance:Singleton = null;
// secret known only to this class
private static const secret:Number = Math.random();
/**
* @private
*/
public function Singleton(enforcer:Number) {
if (enforcer != secret) {
throw new Error("Error: use Singleton.instance instead");
}
}
/**
* Global single instance
*/
public static function get instance():Singleton {
if (_instance == null) {
_instance = new Singleton(secret);
}
return _instance;
}
}
}
2 私有類
在 AS3 檔案內部的包定義外部實現一個類,這個類就是檔案內私有的。
package
{
class Singleton {
private static var _instance:Singleton = null;
public function Singleton(enforcer:PrivateClass) {
}
public function getInstance():Singleton {
if (_instance == null) {
_instance = new Singleton();
}
return _instance;
}
}
}
class PrivateClass {
public PrivateClass() {
}
}
-
轉載請註明來自柳大的CSDN部落格:blog.csdn.net/poechant
-
相關文章
- Python單例模式(Singleton)的N種實現Python單例模式
- Java中Singleton的三種實現方式解析Java
- JAVA中實現單例(Singleton)模式的八種方式Java單例模式
- RabbitMQ實現延時訊息的兩種方法MQ
- 兩種方法使vue實現jQuery呼叫VuejQuery
- ts - 兩種方法實現忽略大小寫的字串排序字串排序
- Spring實現IOC容器的兩種實現方式Spring
- 雜湊表的兩種實現
- Golang兩種方法實現MD5加密Golang加密
- 單利模式的兩種最佳實現模式
- python 程式池的兩種不同實現Python
- Python爬蟲的兩套解析方法和四種爬蟲實現Python爬蟲
- html兩種方法來實現tab切換效果HTML
- 實現高可用的兩種方案與實戰
- 亂序的兩種方法
- Python實現"數字轉換為十六進位制"的兩種方法
- 前端--實現隔行變色的兩種方式前端
- vue 跳轉的兩種方法Vue
- 「分散式技術專題」兩種向量化執行引擎的實現方法分散式
- Spring宣告式事務的兩種實現方式Spring
- zuul實現Cors跨域的兩種方式(https)ZuulCORS跨域HTTP
- MyBatis中主鍵回填的兩種實現方式MyBatis
- 兩種形式的dma 實現memory copy程式碼
- 深入理解二分的兩種實現
- Css實現垂直居中的幾種方法CSS
- C#程式實現軟體開機自動啟動的兩種常用方法C#
- 兩種動態建立表格的方法
- python之建立類的兩種方法Python
- LRU 快取淘汰演算法的兩種實現快取演算法
- 關於多執行緒的兩種實現方式執行緒
- 實現三欄佈局的幾種方法
- Java 實現單例模式的 9 種方法Java單例模式
- 快速排序的三種實現方法 (C++)排序C++
- css實現垂直水平居中的幾種方法CSS
- python中7種方法實現字串的拼接Python字串
- div實現水平垂直居中的幾種方法
- go實現QR訂閱的幾種方法Go
- 一種子圖佈局方法的實現
- javascript實現tab切換的四種方法JavaScript