单态类示例
以下代码是as3实现的单态类,当然还有其他的实现方式。
代码
package{
/**
单态类示例
*/
public class Singleton{
//记录已经存在的实例
private static var _instance:Singleton = null;
public function Singleton(){
if(_instance == null){
_instance = this;
}else{
throw Error("Error: Singleton类是单态类,只能创建一个实例");
}
}
//获取单态类的实例
public function getInstance():Singleton{
if(_instance != null){
return _instance;
}
return new Singleton();
}
}
}
/**
单态类示例
*/
public class Singleton{
//记录已经存在的实例
private static var _instance:Singleton = null;
public function Singleton(){
if(_instance == null){
_instance = this;
}else{
throw Error("Error: Singleton类是单态类,只能创建一个实例");
}
}
//获取单态类的实例
public function getInstance():Singleton{
if(_instance != null){
return _instance;
}
return new Singleton();
}
}
}