單例模式的使用場景
例如:數據源、session工廠
- 需要頻繁的創建和銷燬對象
- 經常需要使用的對象
- 創建的對象銷燬過多的資源
- 工具類對象
餓漢式
靜態常量
class Singleton {
//1. 構造器私有化, 外部能new
private Singleton() {}
//2.本類內部創建對象實例
private final static Singleton instance = new Singleton();
//3. 提供一個公有的靜態方法,返回實例對象
public static Singleton getInstance() {
return instance;
}
}
靜態代碼塊
class Singleton {
//1. 構造器私有化, 外部能new
private Singleton() {}
//2.本類內部創建對象實例
private static Singleton instance;
static { // 在靜態代碼塊中,創建單例對象
instance = new Singleton();
}
//3. 提供一個公有的靜態方法,返回實例對象
public static Singleton getInstance() {
return instance;
}
}
懶漢式
懶漢式(單線程內安全,多線程不安全)
class Singleton {
private static Singleton instance;
private Singleton() {}
//提供一個靜態的公有方法,當使用到該方法時,纔去創建 instance
//即懶漢式
public static Singleton getInstance() {
if(instance == null) {
instance = new Singleton();
}
return instance;
}
}
懶漢式(線程安全,同步鎖方法,但是效率低)
class Singleton {
private static Singleton instance;
private Singleton() {}
//提供一個靜態的公有方法,加入同步處理的代碼,解決線程安全問題
//即懶漢式
public static synchronized Singleton getInstance() {
if(instance == null) {
instance = new Singleton();
}
return instance;
}
}
懶漢式(線程安全,雙重檢查instance變量是否已經實例化) volatile關鍵字 推薦的法方法!
需要注意的是這裏使用的volatile 關鍵字,如果沒有線程共享volatile則多線程不安全
class Singleton {
private static volatile Singleton instance;
private Singleton() {}
//提供一個靜態的公有方法,加入雙重檢查代碼,解決線程安全問題, 同時解決懶加載問題
//同時保證了效率, 推薦使用
public static synchronized Singleton getInstance() {
if(instance == null) {
synchronized (Singleton.class) {
if(instance == null) {
instance = new Singleton();
}
}
}
return instance;
}
}
懶漢式:靜態內部類完成, 推薦使用
靜態內部類SingletonInstance在調用getInstance方法是纔會裝載SingletonInstance,因此是安全的。
class Singleton {
//構造器私有化
private Singleton() {}
//寫一個靜態內部類,該類中有一個靜態屬性 Singleton
private static class SingletonInstance {
private static final Singleton INSTANCE = new Singleton();
}
//提供一個靜態的公有方法,直接返回SingletonInstance.INSTANCE
public static synchronized Singleton getInstance() {
return SingletonInstance.INSTANCE;
}
}
枚舉,可以實現單例, 推薦使用
enum Singleton {
INSTANCE; //屬性
public void method() {
System.out.println("枚舉調用方法");
}
}