国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術文章
文章詳情頁

JAVA破壞單例模式的方式以及避免方法

瀏覽:7日期:2022-08-31 08:51:36

單例模式,大家恐怕再熟悉不過了,其作用與實現方式有多種,這里就不??鋁恕5?牽?勖竊謔褂謎廡┓絞絞迪值ダ?J絞保?絳蛑芯駝嫻幕嶂揮幸桓鍪道?穡?/p>

聰明的你看到這樣的問話,一定猜到了答案是NO。這里筆者就不賣關子了,開門見山吧!實際上,在有些場景下,如果程序處理不當,會無情地破壞掉單例模式,導致程序中出現多個實例對象。

下面筆者介紹筆者已知的三種破壞單例模式的方式以及避免方法。

1、反射對單例模式的破壞

我們先通過一個例子,來直觀感受一下

(1)案例

DCL實現的單例模式:

public class Singleton{ private static volatile Singleton mInstance; private Singleton(){} public static Singleton getInstance(){ if(mInstance == null){ synchronized (Singleton.class) {if(mInstance == null){ mInstance = new Singleton();} } } return mInstance; }}

測試代碼:

public class SingletonDemo { public static void main(String[] args){ Singleton singleton = Singleton.getInstance(); try { Constructor<Singleton> constructor = Singleton.class.getDeclaredConstructor(); constructor.setAccessible(true); Singleton reflectSingleton = constructor.newInstance(); System.out.println(reflectSingleton == singleton); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }}

執行結果:

false

運行結果說明,采用反射的方式另辟蹊徑實例了該類,導致程序中會存在不止一個實例。

(2)解決方案

其思想就是采用一個全局變量,來標記是否已經實例化過了,如果已經實例化過了,第二次實例化的時候,拋出異常。實現代碼如下:

public class Singleton{ private static volatile Singleton mInstance; private static volatile boolean mIsInstantiated = false; private Singleton(){ if (mIsInstantiated){ throw new RuntimeException('Has been instantiated, can not do it again!'); } mIsInstantiated = true; } public static Singleton getInstance(){ if(mInstance == null){ synchronized (Singleton.class) {if(mInstance == null){ mInstance = new Singleton();} } } return mInstance; }}

執行結果:

JAVA破壞單例模式的方式以及避免方法

2、clone()對單例模式的破壞

當需要實現單例的類允許clone()時,如果處理不當,也會導致程序中出現不止一個實例。

(1)案例

一個實現了Cloneable接口單例類:

public class Singleton implements Cloneable{ private static volatile Singleton mInstance; private Singleton(){ } public static Singleton getInstance(){ if(mInstance == null){ synchronized (Singleton.class) {if(mInstance == null){ mInstance = new Singleton();} } } return mInstance; } @Override protected Object clone() throws CloneNotSupportedException { // TODO Auto-generated method stub return super.clone(); }}

測試代碼:

public class SingletonDemo { public static void main(String[] args){ try { Singleton singleton = Singleton.getInstance(); Singleton cloneSingleton; cloneSingleton = (Singleton) Singleton.getInstance().clone(); System.out.println(cloneSingleton == singleton); } catch (CloneNotSupportedException e) { e.printStackTrace(); } }}

執行結果:

false

(2)解決方案:

解決思想是,重寫clone()方法,調clone()時直接返回已經實例的對象

public class Singleton implements Cloneable{ private static volatile Singleton mInstance; private Singleton(){ } public static Singleton getInstance(){ if(mInstance == null){ synchronized (Singleton.class) {if(mInstance == null){ mInstance = new Singleton();} } } return mInstance; } @Override protected Object clone() throws CloneNotSupportedException { return mInstance; }}

執行結果:

true

3、序列化對單例模式的破壞

在使用序列化/反序列化時,也會出現產生新實例對象的情況。

(1)案例

一個實現了序列化接口的單例類:

public class Singleton implements Serializable{ private static volatile Singleton mInstance; private Singleton(){ } public static Singleton getInstance(){ if(mInstance == null){ synchronized (Singleton.class) {if(mInstance == null){ mInstance = new Singleton();} } } return mInstance; }}

測試代碼:

public class SingletonDemo { public static void main(String[] args){ try { Singleton singleton = Singleton.getInstance(); FileOutputStream fos = new FileOutputStream('singleton.txt'); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(singleton); oos.close(); fos.close(); FileInputStream fis = new FileInputStream('singleton.txt'); ObjectInputStream ois = new ObjectInputStream(fis); Singleton serializedSingleton = (Singleton) ois.readObject(); fis.close(); ois.close(); System.out.println(serializedSingleton==singleton); } catch (Exception e) { e.printStackTrace(); } }}

運行結果:

false

(2)解決方案

在反序列化時的回調方法 readResolve()中返回單例對象。

public class Singleton implements Serializable{ private static volatile Singleton mInstance; private Singleton(){ } public static Singleton getInstance(){ if(mInstance == null){ synchronized (Singleton.class) {if(mInstance == null){ mInstance = new Singleton();} } } return mInstance; } protected Object readResolve() throws ObjectStreamException{ return mInstance; }}

結果:

true

以上就是筆者目前已知的三種可以破壞單例模式的場景以及對應的解決辦法,讀者如果知道還有其他的場景,記得一定要分享出來噢,正所謂“獨樂樂不如眾樂樂”!!!

單例模式看起來是設計模式中最簡單的一個,但“麻雀雖小,五臟俱全”,其中有很多細節都是值得深究的。即便是本篇介紹的這幾個場景,也只是介紹了一些梗概而已,很多細節還需要讀者自己去試驗和推敲的,比如:通過枚舉方式實現單例模式,就不存在上述問題,而其它的實現方式似乎都存在上述問題!

后記

本篇參(剽)考(竊)了如下資料:https://www.jb51.net/article/143047.htm

以上就是JAVA破壞單例模式的方式以及避免方法的詳細內容,更多關于JAVA 單例模式的資料請關注好吧啦網其它相關文章!

標簽: Java
相關文章:
主站蜘蛛池模板: 亚洲精品www久久久久久久软件 | 色夜视频 | 欧美一区二区视频 | 99re热精品这里精品 | 久草在在线视频 | 91香蕉国产线观看免 | 人人草97| 国产成人18黄网站免费网站 | 国产91会所洗浴女技师按摩 | 成人18网站 | 精品国产看高清国产毛片 | 亚洲精品一区二区观看 | 精品综合久久久久久88小说 | 久久久久久久国产精品 | 欧美一区二区三区免费不卡 | av在线亚洲男人的天堂 | 日韩在线视频中文字幕 | 日本成人三级 | 99视频在线永久免费观看 | 久草小视频 | 免看一级a一片成人123 | 国产免费播放一区二区 | 秘书高跟黑色丝袜国产91在线 | 久久久综合结合狠狠狠97色 | avtt亚洲一区中文字幕 | 亚洲成人偷拍自拍 | 洋老外米糕国产一区二区 | 国内精品小视频福利网址 | 成人性色生活片全黄 | 一级黄片一级毛片 | 黄页网址免费观看18网站 | 亚州三级 | 免费一级毛片在线播放不收费 | 黄色影视频 | 日本免费特黄aa毛片 | 麻豆69堂免费视频 | 三级理论手机在线观看视频 | 草草视频手机在线观看视频 | 精品无人区一区二区三区a 精品午夜国产在线观看不卡 | 欧美精品亚洲一区二区在线播放 | 97免费视频观看 |