为 Actionscript 实现单例类 [英] implementing singleton class for Actionscript
本文介绍了为 Actionscript 实现单例类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我知道动作脚本在任何时候都不允许使用私有构造函数,但是如果我想在动作脚本中编写一个单例类那么如何在动作脚本中实现它.
I know actionscript does not allowed private contstructor at any time and But if i want to write a sinlgleton class in action script So how to implement it in actionscript.
谁能提供 actionscript 中单例模式的示例?
Can anyone provide an sample example of a singleton pattern in actionscript?
推荐答案
我使用的是这样的:
package singletons
{
[Bindable]
public class MySingleton
{
private static var _instance:MySingleton;
public function MySingleton(e:Enforcer) {
if(e == null) {
throw new Error("Hey! You can't do that! Call getInstance() instead!");
}
}
public static function getInstance():MySingleton {
if(_instance == null) {
_instance = new MySingleton (new Enforcer);
}
return _instance;
}
}
}
// an empty, private class, used to prevent outside sources from instantiating this locator
// directly, without using the getInstance() function....
class Enforcer{}
这篇关于为 Actionscript 实现单例类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文