单态类示例

以下代码是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();
        }
    }
}

 

 

 

posted @ 2009-12-14 14:06  vily_雷  阅读(367)  评论(0编辑  收藏  举报