博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Implement the Singleton In AS3
阅读量:4562 次
发布时间:2019-06-08

本文共 1174 字,大约阅读时间需要 3 分钟。

Implement the Singleton In AS3

The following example implement the Singleton In AS3 at the run-time.I don’t know if there is some other way to implement the Singleton in Compile-time.

 

From

Strict Singletons in AS 3.0

With As 3.0 conforming to ECMA there you can no longer have private constructors which makes creating strict singletons a little bit more tricky. If I write a singleton, I want it written in a way that only a single instance can be created and if someone makes a mistake and tries to create an instance without using getInstance() an error will be thrown. This is a little workaround me and Rich came up with.

package{	public class Singleton	{		private static var instance:Singleton;		private static var creatingSingleton:Boolean = false;				public function Singleton()		{		if( !creatingSingleton ) throw new Error( “Singleton and can only be accessed through Singleton.getInstance()” );		}				public static function getInstance():Singleton		{					if( !instance )			{				creatingSingleton = true;				instance = new Singleton();				creatingSingleton = false;			}					return instance;		}	}}

转载于:https://www.cnblogs.com/Bill_Lee/archive/2011/03/11/1981809.html

你可能感兴趣的文章
urllib 中的异常处理
查看>>
通过SQL Server的扩展事件来跟踪SQL语句在运行时,时间都消耗到哪儿了?
查看>>
gulp
查看>>
pgsql查询优化之模糊查询
查看>>
不变模式
查看>>
20181227 新的目标
查看>>
androidtab
查看>>
php 事件驱动 消息机制 共享内存
查看>>
剑指offer 二叉树的bfs
查看>>
LeetCode Maximum Subarray
查看>>
让我们再聊聊浏览器资源加载优化
查看>>
underscore demo
查看>>
CSS hack
查看>>
浏览器全屏之requestFullScreen全屏与F11全屏
查看>>
软件包管理:rpm命令管理-安装升级与卸载
查看>>
旋转图像
查看>>
每日一小练——数值自乘递归解
查看>>
qq登陆错误提示
查看>>
bzoj 1192: [HNOI2006]鬼谷子的钱袋 思维 + 二进制
查看>>
没写完,没调完,咕咕咕的代码
查看>>