漫说单例模式--宝宝成长记 你真的了解了吗?
1. 你就是單例
你呱呱落地到這個世界的時候,這就是單例的產生,你是世界上唯一無二的存在。
?
此時,你是父輩、祖輩的寶貝。他們都想和你親近。
public class Singleton {private final static Singleton INSTANCE = new Singleton(); //嬰兒呱呱落地// Private constructor suppresses private Singleton() {}// default public constructor public static Singleton getInstance() { //每個人都想抱你,和你親近,你也想很多人親近。return INSTANCE;}}親近就有兩種:一種:別人想要抱你;另一種:你呼喚別人抱你。
第一種稱之為:Eager initialization,如上面的代碼所示。另一種稱之為Lazy initialization。代碼如下所示:
public class SingletonDemo {private static SingletonDemo instance = null; //我很懶,你要想報我,你自己動手private SingletonDemo() { }public static SingletonDemo getInstance() {if (instance == null) { instance = new SingletonDemo (); //你得到機會了。}return instance;} }?
2. 競爭的產生
? ? 很多人想報你和你親近,可你只有一個呀,這就產生了矛盾,這就需要你來控制誰可以來抱你了。這就需要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;} }當然,因為怕出現問題,你想出了更巧的辦法:雙重檢查鎖(Double-checked locking)
public static Singleton getInstance() {if(singleton == null) {synchronized(Singleton.class) {if(singleton == null) {singleton = new Singleton();}}}return singleton; }上述方法存在一定的風險,你可以在方法上再加入synchronized。
3. 太復雜了,扛不住了,那就簡單一點吧
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. 上面產生單例的方法都是靜態的,可以使用動態的方式嗎?
? ? 使用register機制來動態完成單例的實例化。動態化我們首先想到了什么?對了,是反射。
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;} }為了更好的復用代碼,我們該怎么做呢?封裝!
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. 如果有很多小孩,醫院是如何管理的呢?
可是有很多護士給寶貝護理,怎么保證我們家的寶寶得到照顧呢?保證一個護士負責一個嬰兒就ok了
護士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. 如何給嬰兒包被褥呢?當脫掉一件后如果被包裹了兩層就,不好了。為了避免這種情況,就需要檢查包裹的效果了。
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.?注意,以上圖片均來自互聯網,不一一標注了。
?
?
轉載于:https://www.cnblogs.com/davidwang456/p/3642696.html
總結
以上是生活随笔為你收集整理的漫说单例模式--宝宝成长记 你真的了解了吗?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 漫说中介者模式--创业公司成长记
- 下一篇: oracle时间格式转换问题 ORA-0