本文介绍了实现单例类对于ActionScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我知道动作不容许私人contstructor在任何时间,但是如果我想要写在行动脚本sinlgleton类,因此如何在动作执行。
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!