原文地址:xeblog.cn/articles/16git
確保某一個類只有一個實例,並且自行實例化並向整個系統提供這個實例。github
UML類圖設計模式
單例類的構造函數是 private
內部私有的,確保外部不能經過 new
的方式建立新對象,內部自行實例化,並對外提供一個訪問該單一實例的靜態的方法 Instance()
。安全
/** * 普通餓漢式 * * @author anlingyi */
public class Singleton {
/** * 類加載時進行實例化對象 */
private static final Singleton SINGLETON = new Singleton();
/** * 私有構造,防止外部new對象 */
private Singleton() {
}
/** * 經過靜態方法獲取對象實例 * * @return */
public static Singleton getInstance() {
return SINGLETON;
}
public void say() {
System.out.println("普通餓漢式:Hello World!");
}
}
複製代碼
調用方式:多線程
Singleton singleton = Singleton.getInstance();
singleton.say();
複製代碼
優勢: 類加載時就進行實例化,以後的操做效率會很高。
缺點: 因爲類加載時就進行實例化,若是後續不對此類進行任何操做,就會致使內存的浪費。函數
/** * 懶漢式(線程不安全) * * @author anlingyi */
public class SingletonTwo {
private static SingletonTwo instance;
/** * 私有構造,防止外部new對象 */
private SingletonTwo() {
}
/** * 經過靜態方法獲取對象實例 * * @return */
public static SingletonTwo getInstance() {
if(instance == null) {
instance = new SingletonTwo();
}
return instance;
}
public void say() {
System.out.println("懶漢式(線程不安全):Hello World!");
}
}
複製代碼
調用方式:性能
SingletonTwo singleton = SingletonTwo.getInstance();
singleton.say();
複製代碼
優勢: 在第一次調用的時候才進行實例化。
缺點: 當多個線程同時進入到 if(instance == null) {...}
時,會建立多個對象。測試
/** * 同步鎖懶漢式(線程安全,效率低) * * @author anlingyi */
public class SingletonThree {
private static SingletonThree instance;
/** * 私有構造,防止外部new對象 */
private SingletonThree() {
}
/** * 經過靜態方法獲取對象實例 * * @return */
public static synchronized SingletonThree getInstance() {
if(instance == null) {
instance = new SingletonThree();
}
return instance;
}
public void say() {
System.out.println("同步鎖懶漢式(線程安全,效率低):Hello World!");
}
}
複製代碼
調用方式:優化
SingletonThree singleton = SingletonThree.getInstance();
singleton.say();
複製代碼
優勢: 在第一次調用的時候才進行實例化,且線程安全。
缺點: 使用 synchronized
的方式對方法加鎖,會影響效率。spa
/** * 雙重校驗鎖懶漢式(線程安全,且多線程環境下能夠保持高性能) * * @author anlingyi */
public class SingletonFour {
/** * volatile是爲了防止指令重排序 */
private static volatile SingletonFour instance;
/** * 私有構造,防止外部new對象 */
private SingletonFour() {
}
/** * 經過靜態方法獲取對象實例 * * @return */
public static SingletonFour getInstance() {
if(instance == null) {
synchronized (SingletonFour.class) {
if(instance == null) {
instance = new SingletonFour();
}
}
}
return instance;
}
public void say() {
System.out.println("雙重校驗鎖懶漢式(線程安全,且多線程環境下能夠保持高性能):Hello World!");
}
}
複製代碼
調用方式:
SingletonFour singleton = SingletonFour.getInstance();
singleton.say();
複製代碼
優勢: 在第一次調用的時候才進行實例化,且線程安全,效率較高。
缺點: 實現複雜,且 volatile
須要在JDK1.5以後的版本才能確保安全。
/** * 靜態內部類懶漢式 * * @author anlingyi */
public class SingletonFive {
/** * 私有構造,防止外部new對象 */
private SingletonFive() {
}
/** * 經過靜態方法獲取對象實例 * * @return */
public static SingletonFive getInstance() {
return Singleton.SINGLETON;
}
public void say() {
System.out.println("靜態內部類懶漢式:Hello World!");
}
/** * 靜態內部類實例化對象 */
private static class Singleton {
/** * 類加載時進行實例化對象 */
private static final SingletonFive SINGLETON = new SingletonFive();
}
}
複製代碼
調用方式:
SingletonFive singleton = SingletonFive.getInstance();
singleton.say();
複製代碼
優勢: 只有在調用 getInstance()
方法的時候,靜態內部類纔會被加載,從而對主類(咱們須要的類)進行實例化,即線程安全,又效率高。
缺點: 多建立一個類。
/** * 枚舉類餓漢式(防止反序列化) * * @author anlingyi */
public enum SingletonSix {
INSTANCE;
public void say() {
System.out.println("枚舉類餓漢式(防止反序列化):Hello World!");
}
}
複製代碼
調用方式:
SingletonSix singleton = SingletonSix.INSTANCE;
singleton.say();
複製代碼
優勢: 實現簡單,防止反序列化生成多個實例,且線程安全。
缺點: Enum
需在JDK1.5以後版本使用。