二十三種設計模式分類
一、概述
單例(Singleton
)模式的定義:指一個類只有一個實例,且該類能自行創建這個實例的一種模式。例如,Windows
中只能打開一個任務管理器,這樣可以避免因打開多個任務管理器窗口而造成內存資源的浪費,或出現各個窗口顯示內容的不一致等錯誤。
注意:
1、單例類只能有一個實例。
2、單例類必須自己創建自己的唯一實例。
3、單例類必須給所有其他對象提供這一實例。
優點
1、在內存裏只有一個實例,減少了內存的開銷,尤其是頻繁的創建和銷燬實例(比如首頁頁面緩存)。
2、避免對資源的多重佔用(比如寫文件操作)。
缺點
沒有接口,不能繼承,與單一職責原則衝突,一個類應該只關心內部邏輯,而不關心外面怎麼樣來實例化。
應用場景
1、要求生產唯一序列號。
2、WEB
中的計數器,不用每次刷新都在數據庫里加一次,用單例先緩存起來。
3、創建的一個對象需要消耗的資源過多,比如 I/O
與數據庫的連接等。
二、實現
1. 單例模式結構圖
PS
:UML
結構圖可以參考,例子實現並不根據UML
圖來完成,靈活實現即可;
2. 單例模式實現
2.1. 餓漢式
在JVM
加載類階段就構建對象,適合絕大部分場景,JVM
只加載一次類,保證了線程安全;
public class Singleton01 {
private static Singleton01 instance = new Singleton01();
private Singleton01() {
}
public static Singleton01 getInstance() {
return instance;
}
public static void main(String[] args) {
Singleton01 singleton01 = Singleton01.getInstance();
Singleton01 singleton011 = Singleton01.getInstance();
System.out.println("Compare:" + (singleton01 == singleton011));
}
}
2.2. 懶漢式
線程不安全,在需要時再構建對象,多個線程同時構建對象時,會構建出多個對象;
public class Singleton02 {
private static Singleton02 instance;
private Singleton02(){
}
public static Singleton02 getInstance() {
if (instance == null) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
instance = new Singleton02();
}
return instance;
}
public static void main(String[] args) {
for (int i = 0; i < 100; i++) {
new Thread(()->{
System.out.println(Singleton02.getInstance().hashCode());
}).start();
}
}
}
2.3. 雙重鎖檢查
使用synchronized
類鎖限制每次只能有一個線程可以構建新對象,兩次判斷instance
對象是否爲null
,第一次判斷如果爲null
,則構建新對象;第二次判斷是否爲null
,則是爲了防止A線程已經構建完成了對象,避免B
線程獲得鎖後再次構建對象,確保該類只有一個實例;
public class Singleton03 {
///volatile防止指令重排序
private volatile static Singleton03 instance;
private Singleton03(){
}
public static Singleton03 getInstance() {
if (instance == null) {
synchronized (Singleton03.class) {
if (instance == null) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
instance = new Singleton03();
}
}
}
return instance;
}
public static void main(String[] args) {
for (int i = 0; i < 100; i++) {
new Thread(()->{
System.out.println(Singleton03.getInstance().hashCode());
}).start();
}
}
}
2.4. 靜態內部類
靜態內部類模式,JVM
加載靜態內部類只會加載一次,加載過程中完成了新對象構建,保證了線程安全,不能解決反序列化問題;
public class Singleton04 {
private Singleton04(){
}
//使用static來修飾一個內部類,則這個內部類就屬於外部類本身,而不屬於外部類的某個對象
private static class SingletonHolder{
private static Singleton04 instance = new Singleton04();
}
public static Singleton04 getInstance() {
return SingletonHolder.instance;
}
public static void main(String[] args) {
for (int i = 0; i < 100; i++) {
new Thread(()->{
System.out.println(Singleton04.getInstance().hashCode());
}).start();
}
}
}
2.5. 枚舉實現
枚舉沒有構造函數,所以可解決反序列化的問題,並且保證線程安全;
·public enum Singleton05 {
/**
* 單一實例
*/
INSTANCE;
public static Singleton05 getInstance() {
return Singleton05.INSTANCE;
}
public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
Singleton05 singleton1 = Singleton05.INSTANCE;
Singleton05 singleton2 = Singleton05.INSTANCE;
System.out.println("正常情況下,實例化兩個實例是否相同:" + (singleton1 == singleton2));
Constructor<Singleton05> constructor = null;
constructor = Singleton05.class.getDeclaredConstructor();
constructor.setAccessible(true);
Singleton05 singleton3 = null;
singleton3 = constructor.newInstance();
System.out.println(singleton1 + "\n" + singleton2 + "\n" + singleton3);
System.out.println("通過反射攻擊單例模式情況下,實例化兩個實例是否相同:" + (singleton1 == singleton3));
}
}··