2009년 5월 12일 화요일

Singleton pattern 예제 - Head First Design Pattern 중에서

다음은 Singleton pattern 예제 코드이다.

/**
 * Singleton.java
 * 
 * Singleton pattern의 예제
 */
package net.wiseant.designpattern.singleton;

/**
 * @author Sang Hyup Lee
 * @version 1.0
 *
 */
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;
    }
    
}

댓글 없음: