漫说单例模式--宝宝成长记 你真的了解了吗?
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)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 漫说中介者模式--创业公司成长记
- 下一篇: oracle时间格式转换问题 ORA-0