單例模式

標籤: 設計模式java


單例模式保證一個類僅有一個實例,並提供一個訪問它的全局訪問點的模式。屬於建造型模式設計模式

1. 簡單方式, 懶加載,單線程

public class Singleton {
    private static Singleton instance;  
    private Singleton (){}
    
    public static Singleton getInstance() {  
        if (instance == null) {  
            instance = new Singleton();  
        }  
        return instance;  
    }  
}

2. 懶加載,考慮多線程,但鎖住整個get方法,效率低

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

3. 考慮多線程,不加鎖,效率高,但餓漢加載,浪費內存(推薦)

public class Singleton {
    //基於classloder機制避免多線程的同步問題
    private static Singleton instance = new Singleton();  
    private Singleton (){}  
    public static Singleton getInstance() {  
        return instance;  
    }  
}

4. 雙重校驗鎖,多線程,效率高,實現複雜

public class Singleton {  
    private volatile static Singleton instance;  
    private Singleton (){}  
    public static Singleton getInstance() {  
        //99%的狀況下instance不爲null,所以加鎖機率不多,整體效率高
        if (instance == null) {  
            synchronized (Singleton.class) {  
                if (instance == null) {  
                    instance = new Singleton();  
                }  
            }  
        }  
        return instance;  
    }  
}

5.靜態內部類,相似狀況3,但懶加載,多線程,比較簡單

public class Singleton {  
    private static class SingletonHolder {  
        //既基於classloder機制避免多線程的同步問題,又懶加載
        private static final Singleton INSTANCE = new Singleton();  
    }  
    private Singleton (){}  
    public static final Singleton getInstance() {  
        return SingletonHolder.INSTANCE;  
    }  
}

6.使用枚舉方法

public enum Singleton {  
    INSTANCE;  
    public void whateverMethod() {  
    }  
}

總結:
(1)通常狀況下,不建議使用第 1 種和第 2 種懶漢方式,建議使用第 3 種餓漢方式。
(2)只有在要明確實現 lazy loading 效果時,纔會使用第 5 種登記方式。
(3)若是涉及到反序列化建立對象時,能夠嘗試使用第 6 種枚舉方式。
(4)若是有其餘特殊的需求,能夠考慮使用第 4 種雙檢鎖方式。多線程

相關文章
相關標籤/搜索