來源:http://www.bjsxt.com/ 一、【GOF23設計模式】_單例模式、應用場景、餓漢式、懶漢式 1、GOF23設計模式 2、單例模式 3、餓漢式 4、懶漢式 二、【GOF23設計模式】_單例模式、雙重檢查鎖式、靜態內部類式、枚舉式、UML類圖 雙重檢測鎖實現 靜態內部類實現 枚舉實 ...
來源:http://www.bjsxt.com/
一、【GOF23設計模式】_單例模式、應用場景、餓漢式、懶漢式
1、GOF23設計模式
2、單例模式
3、餓漢式
1 package com.test.singleton; 2 /** 3 * 測試餓漢式單例模式 4 */ 5 public class SingletonDemo01 { 6 7 //類初始化時,立即載入這個對象(沒有延時載入的優勢)。載入類時,天然的是線程安全的! 8 private static SingletonDemo01 instance = new SingletonDemo01(); 9 10 private SingletonDemo01(){//私有化構造器 11 } 12 13 //方法不用同步,調用效率高! 14 public static SingletonDemo01 getInstance(){ 15 return instance; 16 } 17 }
4、懶漢式
1 package com.test.singleton; 2 /** 3 * 測試懶漢式單例模式 4 */ 5 public class SingletonDemo02 { 6 7 //類初始化時,不初始化這個對象(延時載入,真正用的時候再創建) 8 private static SingletonDemo02 instance; 9 10 private SingletonDemo02(){//私有化構造器 11 } 12 13 //方法同步,調用效率低! 14 public static synchronized SingletonDemo02 getInstance(){ 15 if(null==instance){ 16 instance = new SingletonDemo02(); 17 } 18 return instance; 19 } 20 }
二、【GOF23設計模式】_單例模式、雙重檢查鎖式、靜態內部類式、枚舉式、UML類圖
雙重檢測鎖實現
1 package com.test.singleton; 2 /** 3 * 測試雙重檢測鎖式單例模式 4 */ 5 public class SingletonDemo03 { 6 7 //類初始化時,不初始化這個對象(延時載入,真正用的時候再創建) 8 private static SingletonDemo03 instance; 9 10 private SingletonDemo03(){//私有化構造器 11 } 12 13 //調用效率低! 14 public static SingletonDemo03 getInstance(){ 15 if(null==instance){ 16 SingletonDemo03 sc; 17 synchronized(SingletonDemo03.class){ 18 sc = instance; 19 if(null==sc){ 20 synchronized (SingletonDemo03.class) { 21 if(null==sc){ 22 sc = new SingletonDemo03(); 23 } 24 } 25 instance = sc; 26 } 27 } 28 } 29 return instance; 30 } 31 }
靜態內部類實現
1 package com.test.singleton; 2 /** 3 * 測試靜態內部類實現單例模式 4 * 這種方式:線程安全,調用效率高,並且實現了延時載入! 5 */ 6 public class SingletonDemo04 { 7 8 private static class SingletonClassInstance { 9 private static final SingletonDemo04 instance = new SingletonDemo04(); 10 } 11 12 private SingletonDemo04(){//私有化構造器 13 } 14 15 //方法沒有同步,調用效率高! 16 public static SingletonDemo04 getInstance(){ 17 return SingletonClassInstance.instance; 18 } 19 }
枚舉實現
1 package com.test.singleton; 2 /** 3 * 測試枚舉式實現單例模式(沒有延時載入) 4 */ 5 public enum SingletonDemo05 { 6 7 //這個枚舉元素,本身就是單例對象! 8 INSTANCE; 9 10 //添加自己需要的操作! 11 public void singletonOperation(){ 12 } 13 }
1 package com.test.singleton; 2 3 public class Client { 4 public static void main(String[] args) { 5 SingletonDemo01 s1 = SingletonDemo01.getInstance(); 6 SingletonDemo01 s2 = SingletonDemo01.getInstance(); 7 System.out.println(s1==s2);//true 8 9 SingletonDemo03 s3 = SingletonDemo03.getInstance(); 10 SingletonDemo03 s4 = SingletonDemo03.getInstance(); 11 System.out.println(s3==s4);//true 12 13 System.out.println(SingletonDemo05.INSTANCE==SingletonDemo05.INSTANCE);//true 14 } 15 }
五種單例模式實現
三、【GOF23設計模式】_單例模式、反射和反序列化漏洞和解決方案、多線程環境測試、CountDownLatch同步類的使用
1 package com.test.singleton; 2 3 import java.io.FileInputStream; 4 import java.io.FileOutputStream; 5 import java.io.ObjectInputStream; 6 import java.io.ObjectOutputStream; 7 import java.lang.reflect.Constructor; 8 9 /** 10 * 測試反射和反序列化破解單例模式 11 */ 12 public class Client2 { 13 public static void main(String[] args) throws Exception { 14 SingletonDemo06 s1 = SingletonDemo06.getInstance(); 15 SingletonDemo06 s2 = SingletonDemo06.getInstance(); 16 System.out.println(s1); 17 System.out.println(s2); 18 19 //通過反射的方式直接調用私有構造器 20 /* Class<SingletonDemo06> clazz = (Class<SingletonDemo06>) Class.forName("com.test.singleton.SingletonDemo06"); 21 Constructor<SingletonDemo06> c = clazz.getDeclaredConstructor(null); 22 c.setAccessible(true);//訪問私有,破解單例 23 SingletonDemo06 s3 = c.newInstance(); 24 SingletonDemo06 s4 = c.newInstance(); 25 System.out.println(s3);//SingletonDemo06的私有構造器中加了代碼防止破解 26 System.out.println(s4);*/ 27 28 //通過反序列化的方式構造多個對象 29 FileOutputStream fos = new FileOutputStream("g:/java/test/a.txt"); 30 ObjectOutputStream oos = new ObjectOutputStream(fos); 31 oos.writeObject(s1); 32 oos.close(); 33 fos.close(); 34 35 ObjectInputStream ois = new ObjectInputStream(new FileInputStream("g:/java/test/a.txt")); 36 SingletonDemo06 s3 = (SingletonDemo06) ois.readObject(); 37 System.out.println(s3);//SingletonDemo06定義了readResolve()防止破解 38 } 39 }
1 package com.test.singleton; 2 3 import java.io.ObjectStreamException; 4 import java.io.Serializable; 5 6 /** 7 * 測試懶漢式單例模式(如何防止反射和反序列化漏洞) 8 */ 9 public class SingletonDemo06 implements Serializable{ 10 11 //類初始化時,不初始化這個對象(延時載入,真正用的時候再創建) 12 private static SingletonDemo06 instance; 13 14 private SingletonDemo06(){//私有化構造器 15 if(instance!=null){//防止反射破解單例(對象.setAccessible(true);) 16 throw new RuntimeException(); 17 } 18 } 19 20 //方法同步,調用效率低! 21 public static synchronized SingletonDemo06 getInstance(){ 22 if(null==instance){ 23 instance = new SingletonDemo06(); 24 } 25 return instance; 26 } 27 28 //反序列化時,如果定義了readResolve()則直接返回此方法指定的對象。而不需要單獨再創建新對象! 29 private Object readResolve() throws ObjectStreamException{ 30 return instance; 31 } 32 }
1 package com.test.singleton; 2 3 import java.util.concurrent.CountDownLatch; 4 5 /** 6 * 測試多線程環境下五種創建單例模式的效率 7 */ 8 public class Client3 { 9 public static void main(String[] args) throws Exception{ 10 11 int threadNum = 10; 12 final CountDownLatch countDownLatch = new CountDownLatch(threadNum); 13 14 long start = System.currentTimeMillis(); 15 for(int i=0;i<10;i++){ 16 new Thread(new Runnable(){ 17 @Override 18 public void run() { 19 for (int i = 0; i < 1000000; i++) { 20 Object o = SingletonDemo01.getInstance();//分別測試幾種單例模式的效率 21 //Object o = SingletonDemo05.INSTANCE; 22 } 23 24 countDownLatch.countDown();//計數器減1 25 } 26 }).start(); 27 } 28 29 countDownLatch.await();//main線程阻塞,直到計數器變為0,才會繼續往下執行! 30 31 long end = System.currentTimeMillis(); 32 System.out.println("總耗時:" + (end-start)); 33 } 34 }