기타 이슈

싱글톤 SingleTone

Michelle Hwang 2021. 5. 14. 12:31

* 사용

	 static void SingleToneInstanceTest() {
		 
		 // SingletoneInstance inst = new SingletoneInstance() ; // ERROR as constructor is private 
		 SingletoneInstance inst = SingletoneInstance.newInstance() ;
		
	 }

 

class SingletoneInstance
{
	// 단 하나의 객체로만 운영할 때 
	
    private static SingletoneInstance single = null ;
	
    private SingletoneInstance() { // make can't instance with new 
    	
    }
    
    static SingletoneInstance newInstance() {
    	
    	if (single==null)
    		return new SingletoneInstance() ;
    	else
    		return single ;
    }
}