posts - 7, comments - 1, trackbacks - 0, articles - 0

Reading notes -- Singleton pattern

Posted on 2006-07-09 12:42 Jedi 阅读(284) 评论(0)  编辑  收藏 所属分类: Design Patterns
public   class  Singleton {

    
private   volatile   static  Singleton uniqueInstance;
    
private  Singleton(){
        
    }
    
    
public   static  Singleton getInstance(){
        
if (uniqueInstance == null ){
            
synchronized (Singleton. class ){
                
if (uniqueInstance == null ){
                    uniqueInstance 
=   new  Singleton();
                }
            }
        }        
        
return  uniqueInstance; 
    }

}

1. private constructor
2. static getInstance
3. syncronized..waste a lot of time
4. double check..modified syncronize, so time-waste might occurs only when first time the instance construct

只有注册用户登录后才能发表评论。


网站导航: