單例模式是23中設計模式中最簡單的設計模式,在企業開發中也應用的特別多。單例模式的優點是:項目中有且僅有一個實例。
特點:構造器私有化,對象私有化,只提供一個對外訪問的接口。
應用場景:
1、系統需要共享資源:比如日志系統,spring的資源管理器等
2、為了控制資源的使用:比如線程池
企業級開發和常見框架中的常見應用:
J2EE中的servlet,Spring中的資源管理器(即beans),數據庫連接池,線程池,日志系統,網站計數器等
單例模式分類:
1、餓漢模式:餓漢模式是代碼最簡單的單例模式,但實例在類初始化的時候就加載了,在不是即時使用的情況下,會加慢系統的加載速度,具體代碼如下:
1
2
3
4
5
6
7
8
9
|
public class Singleton{ private static Singleton instance = new Singleton(); private Singleton(){} public static Singleton getInstance(){ return instance; } } |
2、懶漢模式:懶漢模式相比于餓漢模式,就是在實例化的放在了唯一的對外接口中處理,實現了延遲加載,節省了系統初始化時間,但存在線程不安全的情況。
1
2
3
4
5
6
7
8
9
10
11
12
|
public class Singleton{ private static Singleton instance = null ; private Singleton(){} public static Singleton getInstance(){ if (instance == null ){ return new Singleton(); } return instance; } } |
3、雙重校驗鎖:雙重校驗鎖模式其實就是懶漢模式的升級,讓懶漢模式變得線程安全。注意:雙重校驗鎖存在內存問題,可能讓雙重校驗鎖失效。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
public class Singleton{ private static Singleton instance = null ; private Singleton(){} public static Singleton getInstance(){ if (instance == null ){ synchronized (Singleton. class ){ if (instance == null ){ return new Singleton(); } } } return instance; } } |
4、靜態內部類模式:靜態內部類兼具了懶漢模式和惡漢模式的有點:線程安全,延遲加載。
1
2
3
4
5
6
7
8
9
10
11
|
public class Singleton{ private static class SingletonFactory{ private static Singleton INSTANCE = new Singleton(); } private Singleton(){} public static Singleton getInstance(){ return SingletonFactory.INSTANCE; } } |
5、枚舉類模式:應該是最完美的單利模式,不僅線程安全,而且還能防止反序列和反射問題。
1
2
3
4
5
6
7
|
enum Singleton{ INSTANCE; public void doSomething(){ ... } } |
單例模式細節化問題:
1、反射打破單例模式:通過反射可以破壞單例模式的實現(枚舉類模式除外)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
/** *通過反射破壞單例模式 */ public class Demo01 { public static void main(String[] args) throws Exception { Singleton s1 = Singleton.getInstance(); Singleton s2 = Singleton.getInstance(); System.out.println(s1 == s2); Class<Singleton> clazz = (Class<Singleton>) Class.forName( "com.singleton.Singleton" ); Constructor<Singleton> constructor = clazz.getDeclaredConstructor( null ); constructor.setAccessible( true ); Singleton s3 = constructor.newInstance(); System.out.println(s1 == s3); } } class Singleton{ private static Singleton instance = new Singleton(); private Singleton(){ //防止反射破壞單利模式的方法,打開注釋部分 // if(instance != null){ // throw new RuntimeException(); // } } public static Singleton getInstance(){ return instance; } } |
其實所謂的防止也就是讓其不能通過反射創建。
2、反序列化打破單例模式(枚舉類模式除外)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
/** * 反序列化打破單例模式 */ public class Demo02 { public static void main(String[] args) throws Exception { Singleton s1 = Singleton.getInstance(); Singleton s2 = Singleton.getInstance(); System.out.println(s1 == s2); FileOutputStream fos = new FileOutputStream( "d://test.txt" ); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(s1); oos.close(); fos.close(); ObjectInputStream ois = new ObjectInputStream( new FileInputStream( "d://test.txt" )); Singleton s3 = (Singleton) ois.readObject(); System.out.println(s1 == s3); } } class Singleton implements Serializable{ private static Singleton instance = new Singleton(); public static Singleton getInstance(){ return instance; } //反序列化時,如果對象已經存在,將調用這個方法 // private Object readResolve() throws ObjectStreamException{ // return instance; // // } } |
這兩種情況僅限于了解,在實際開發過程中用的不多。
至此,單例模式完整。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。