單例模式是23中設計模式中最簡單的設計模式,在企業開發中也應用的特別多。單例模式的優點是:項目中有且僅有一個執行個體。
特點:構造器私人化,對象私人化,只提供一個對外訪問的介面。
應用情境:
1、系統需要共用資源:比如日誌系統,spring的資源管理員等
2、為了控制資源的使用:比如線程池
企業級開發和常見架構中的常見應用:
J2EE中的servlet,Spring中的資源管理員(即beans),資料庫連接池,線程池,日誌系統,網站計數器等
單例模式分類:
1、餓漢模式:餓漢模式是代碼最簡單的單例模式,但執行個體在類初始化的時候就載入了,在不是即時使用的情況下,會加慢系統的載入速度,具體代碼如下:
public class Singleton{ private static Singleton instance = new Singleton(); private Singleton(){} public static Singleton getInstance(){ return instance; }}
2、懶漢模式:懶漢模式相比於餓漢模式,就是在執行個體化的放在了唯一的對外介面中處理,實現了消極式載入,節省了系統初始化時間,但存線上程不安全的情況。
public class Singleton{ private static Singleton instance = null; private Singleton(){} public static Singleton getInstance(){ if(instance == null){ return new Singleton(); } return instance; }}
3、雙重校正鎖:雙重校正鎖模式其實就是懶漢模式的升級,讓懶漢模式變得安全執行緒。注意:雙重校正鎖存在記憶體問題,可能讓雙重校正鎖失效。
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、靜態內部類模式:靜態內部類兼具了懶漢模式和惡漢模式的有點:安全執行緒,消極式載入。
public class Singleton{ private static class SingletonFactory{ private static Singleton INSTANCE = new Singleton(); } private Singleton(){} public static Singleton getInstance(){ return SingletonFactory.INSTANCE; }}
5、枚舉類模式:應該是最完美的單利模式,不僅安全執行緒,而且還能防止反序列和反射問題。
enum Singleton{ INSTANCE; public void doSomething(){ ... }}
單例模式細節化問題:
1、反射打破單例模式:通過反射可以破壞單例模式的實現(枚舉類模式除外)
/** *通過反射破壞單例模式 */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、還原序列化打破單例模式(枚舉類模式除外)
/** * 還原序列化打破單例模式 */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;// // }}
這兩種情況僅限於瞭解,在實際開發過程中用的不多。
至此,單例模式完整。
以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援雲棲社區。