標籤: 設計模式java
單例模式保證一個類僅有一個實例,並提供一個訪問它的全局訪問點的模式。屬於建造型模式。設計模式
public class Singleton { private static Singleton instance; private Singleton (){} public static Singleton getInstance() { if (instance == null) { instance = new Singleton(); } return instance; } }
public class Singleton { private static Singleton instance; private Singleton (){} public static synchronized Singleton getInstance() { if (instance == null) { instance = new Singleton(); } return instance; } }
public class Singleton { //基於classloder機制避免多線程的同步問題 private static Singleton instance = new Singleton(); private Singleton (){} public static Singleton getInstance() { return instance; } }
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; } }
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; } }
public enum Singleton { INSTANCE; public void whateverMethod() { } }
總結:
(1)通常狀況下,不建議使用第 1 種和第 2 種懶漢方式,建議使用第 3 種餓漢方式。
(2)只有在要明確實現 lazy loading 效果時,纔會使用第 5 種登記方式。
(3)若是涉及到反序列化建立對象時,能夠嘗試使用第 6 種枚舉方式。
(4)若是有其餘特殊的需求,能夠考慮使用第 4 種雙檢鎖方式。多線程