日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

漫说单例模式--宝宝成长记 你真的了解了吗?

發(fā)布時(shí)間:2025/4/5 编程问答 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 漫说单例模式--宝宝成长记 你真的了解了吗? 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

1. 你就是單例

你呱呱落地到這個(gè)世界的時(shí)候,這就是單例的產(chǎn)生,你是世界上唯一無二的存在。

?

此時(shí),你是父輩、祖輩的寶貝。他們都想和你親近。

public class Singleton {private final static Singleton INSTANCE = new Singleton(); //嬰兒呱呱落地// Private constructor suppresses private Singleton() {}// default public constructor public static Singleton getInstance() { //每個(gè)人都想抱你,和你親近,你也想很多人親近。return INSTANCE;}}

親近就有兩種:一種:別人想要抱你;另一種:你呼喚別人抱你。

第一種稱之為:Eager initialization,如上面的代碼所示。另一種稱之為Lazy initialization。代碼如下所示:

public class SingletonDemo {private static SingletonDemo instance = null; //我很懶,你要想報(bào)我,你自己動(dòng)手private SingletonDemo() { }public static SingletonDemo getInstance() {if (instance == null) { instance = new SingletonDemo (); //你得到機(jī)會(huì)了。}return instance;} }

?

2. 競爭的產(chǎn)生

? ? 很多人想報(bào)你和你親近,可你只有一個(gè)呀,這就產(chǎn)生了矛盾,這就需要你來控制誰可以來抱你了。這就需要synchronization來控制。

public class SingletonDemo {private static SingletonDemo instance = null;private SingletonDemo() { }public static synchronized SingletonDemo getInstance() {if (instance == null) {instance = new SingletonDemo ();}return instance;} }

?為了讓更多人可以抱你,你想出了更好的辦法:

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

當(dāng)然,因?yàn)榕鲁霈F(xiàn)問題,你想出了更巧的辦法:雙重檢查鎖(Double-checked locking)

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

上述方法存在一定的風(fēng)險(xiǎn),你可以在方法上再加入synchronized。

3. 太復(fù)雜了,扛不住了,那就簡單一點(diǎn)吧

public class Singleton {public final static Singleton INSTANCE = new Singleton();private Singleton() {// Exists only to defeat instantiation. } }

然后你就可以直接使用了:

Singleton singleton = Singleton.INSTANCE;singleton.dothis();singleton.dothat();...

4. 上面產(chǎn)生單例的方法都是靜態(tài)的,可以使用動(dòng)態(tài)的方式嗎?

? ? 使用register機(jī)制來動(dòng)態(tài)完成單例的實(shí)例化。動(dòng)態(tài)化我們首先想到了什么?對(duì)了,是反射。

import java.util.HashMap; import org.apache.log4j.Logger; public class Singleton {private static HashMap map = new HashMap();private static Logger logger = Logger.getRootLogger();protected Singleton() {// Exists only to thwart instantiation }public static synchronized Singleton getInstance(String classname) {Singleton singleton = (Singleton)map.get(classname);if(singleton != null) {logger.info("got singleton from map: " + singleton);return singleton;}try {singleton = (Singleton)Class.forName(classname).newInstance();}catch(ClassNotFoundException cnf) {logger.fatal("Couldn't find class " + classname); }catch(InstantiationException ie) {logger.fatal("Couldn't instantiate an object of type " + classname); }catch(IllegalAccessException ia) {logger.fatal("Couldn't access class " + classname); }map.put(classname, singleton);logger.info("created singleton: " + singleton);return singleton;} }

為了更好的復(fù)用代碼,我們該怎么做呢?封裝!

import java.util.HashMap; import org.apache.log4j.Logger; public class SingletonRegistry {public static SingletonRegistry REGISTRY = new SingletonRegistry();private static HashMap map = new HashMap();private static Logger logger = Logger.getRootLogger();protected SingletonRegistry() {// Exists to defeat instantiation }public static synchronized Object getInstance(String classname) {Object singleton = map.get(classname);if(singleton != null) {return singleton;}try {singleton = Class.forName(classname).newInstance();logger.info("created singleton: " + singleton);}catch(ClassNotFoundException cnf) {logger.fatal("Couldn't find class " + classname); }catch(InstantiationException ie) {logger.fatal("Couldn't instantiate an object of type " + classname); }catch(IllegalAccessException ia) {logger.fatal("Couldn't access class " + classname); }map.put(classname, singleton);return singleton;} }

封裝完成后,我們可以使用了。

import java.util.HashMap; import org.apache.log4j.Logger; public class Singleton {protected Singleton() {// Exists only to thwart instantiation. }public static Singleton getInstance() {return (Singleton)SingletonRegistry.REGISTRY.getInstance(classname);} }

5. 如果有很多小孩,醫(yī)院是如何管理的呢?

可是有很多護(hù)士給寶貝護(hù)理,怎么保證我們家的寶寶得到照顧呢?保證一個(gè)護(hù)士負(fù)責(zé)一個(gè)嬰兒就ok了

護(hù)士classloader 嬰兒instance

private static Class getClass(String classname) throws ClassNotFoundException {ClassLoader classLoader = Thread.currentThread().getContextClassLoader();if(classLoader == null)classLoader = Singleton.class.getClassLoader();return (classLoader.loadClass(classname));} }

6. 如何給嬰兒包被褥呢?當(dāng)脫掉一件后如果被包裹了兩層就,不好了。為了避免這種情況,就需要檢查包裹的效果了。

import org.apache.log4j.Logger; public class Singleton implements java.io.Serializable {public static Singleton INSTANCE = new Singleton();protected Singleton() {// Exists only to thwart instantiation. }private Object readResolve() { //保證包裹的檢查返回同一種情況。return INSTANCE;} }

?

?參考:

1. http://en.wikipedia.org/wiki/Singleton_pattern

2.?http://www.javaworld.com/article/2073352/core-java/simply-singleton.html

3.?注意,以上圖片均來自互聯(lián)網(wǎng),不一一標(biāo)注了。

?

?

轉(zhuǎn)載于:https://www.cnblogs.com/davidwang456/p/3642696.html

總結(jié)

以上是生活随笔為你收集整理的漫说单例模式--宝宝成长记 你真的了解了吗?的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。