<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    數(shù)據(jù)加載中……
    設(shè)計(jì)模式之單例模式詳解

    如果說道設(shè)計(jì)模式,我想大家想到最多的應(yīng)該就是單例模式和工廠模式,上一篇文章中詳細(xì)介紹了工廠模式以及簡單工廠模式和抽象工廠模式。相對工廠模式,單例模式要簡單一點(diǎn),但是單例模式也同樣有很多值得思考的問題,下面就來看一下這些問題以及解決的辦法。

    1、靜態(tài)初始化實(shí)例對象(網(wǎng)上也有人叫饑餓實(shí)例化)

    public class EagerSingleton {

        private static final EagerSingleton instance = new EagerSingleton();

        private EagerSingleton() {

        }

        public static EagerSingleton getInstance() {

            return EagerSingleton.instance;

        }

    }

    2、當(dāng)?shù)谝淮涡枰臅r候創(chuàng)建實(shí)例化對象(懶漢實(shí)例化)

    public class LazySingleton {

        private static LazySingleton instance = null;

        private LazySingleton() {

        }

        public synchronized static LazySingleton getInstance() {    

            if(instance == null) {

                instance = new LazySingleton();

            }

            return instance;

        }

    }

    這里一定要注意加synchronized,否則就會引進(jìn)多線程下的不安全的情況了。

    但是,考慮到synchronized加在方法上,鎖的粒度太多,會對性能產(chǎn)生影響。所以引入下面的dcl算法進(jìn)行改進(jìn)。

    3、采用dcl算法改進(jìn)2(關(guān)于dcl請看這個http://en.wikipedia.org/wiki/Double-checked_locking,后面找時間詳細(xì)聊聊這個東西,貌似是jee中一個很常用的東西)

    public class DclLazySingleton {

        private static DclLazySingleton instance = null;

        private DclLazySingleton() {

        }

        public static DclLazySingleton getInstance() {

            if(instance == null) {

                synchronized(DclLazySingleton.class) {

                    if(instance == null) {

                        instance = new DclLazySingleton();

                    }

                }

            }

            return instance;

        }

    }

    ps:按照wiki的說法,這種算法是有問題,這個后面在討論,不過貌似異常出現(xiàn)的概率很小,而且很多成功的開源項(xiàng)目都用到了它,所以,不用擔(dān)心!

    4、看到這里,是不是大家覺得單例模式就該解決了呢?可是有沒有發(fā)現(xiàn)一個問題呢?上面的單例類都不能做父類,因?yàn)闃?gòu)造方法是私有的,ps,子類的構(gòu)造方法默認(rèn)會去調(diào)用父類的構(gòu)造方法,如果是私有的,這個果斷沒法繼承啊。

    所以就引進(jìn)了下面的注冊式的單例實(shí)現(xiàn)方式。看網(wǎng)上很多人都說spring中使用了這種方式,所以開始了解這種方式,其實(shí)經(jīng)典的GoF的書中就有對這個的介紹。

    public class RegisterSingleton {

        private static Map<String, RegisterSingleton> instanceMap = new HashMap<String, RegisterSingleton>();

        static {

            RegisterSingleton instanceThis = new RegisterSingleton();

            instanceMap.put(RegisterSingleton.class.getName(), instanceThis);

        }

        protected RegisterSingleton() {

        }

        public static RegisterSingleton getInstance(String name) {

            if(name == null) {

                name = RegisterSingleton.class.getName();

            }

            if(instanceMap.get(name) == null) {

                try {

                    instanceMap.put(name, (RegisterSingleton)Class.forName(name).newInstance());

                } catch (InstantiationException e) {

                    e.printStackTrace();

                } catch (IllegalAccessException e) {

                    e.printStackTrace();

                } catch (ClassNotFoundException e) {

                    e.printStackTrace();

                }

            }

            return instanceMap.get(name);

        }

        public static RegisterSingleton getInstance() {

            return getInstance(RegisterSingleton.class.getName());

        }

    }

    使用重載來便捷的調(diào)用getInstance()方法。

    子類如果繼承去調(diào)用getInstance方法呢?請看如下代碼:

    public class RegisterSingletonChild extends RegisterSingleton {

        protected RegisterSingletonChild() {}

        public static RegisterSingletonChild getInstance() {

            return (RegisterSingletonChild)RegisterSingleton.getInstance(RegisterSingletonChild.class.getName());

        }

    }

    當(dāng)然,如果子類確定沒有子類的話,則可以考慮用private去替代protected.

    5、總結(jié)

    以上4種方式基本包含了單例模式使用的四種方法,考慮了多線程下單例模式的線程安全性和有繼承情況下的注冊式單例實(shí)現(xiàn)。
    老規(guī)矩上pdf,呵呵。/Files/zhenxuanpan/設(shè)計(jì)模式之單例模式的詳解.pdf

    posted on 2011-08-25 00:03 潘潘.eagle 閱讀(291) 評論(0)  編輯  收藏 所屬分類: Design Pattern

    主站蜘蛛池模板: 亚洲精品白色在线发布| 亚洲黄片手机免费观看| 亚洲成A∨人片天堂网无码| 成人av片无码免费天天看| 亚洲色图古典武侠| 在线观看亚洲免费| 四虎影视成人永久免费观看视频| 国产免费AV片在线观看播放| 亚洲国产精品无码成人片久久| 久久WWW免费人成一看片| 亚洲午夜福利在线视频| 亚洲精品无码mv在线观看网站| 成年女性特黄午夜视频免费看| 一级女人18片毛片免费视频| 亚洲中字慕日产2021| 最近2019中文字幕免费大全5 | 亚洲一区二区三区高清| 女性无套免费网站在线看| 91免费国产视频| 亚洲精品久久无码| 久久亚洲日韩精品一区二区三区| 亚洲国产激情一区二区三区| 无人在线直播免费观看| 中文永久免费观看网站| 亚洲av无码成人影院一区| 亚洲视频在线观看网址| 激情综合色五月丁香六月亚洲| 青青青国产免费一夜七次郎| 国产精品亚洲一区二区三区 | 亚洲女同成av人片在线观看| 日韩精品视频免费网址| 国产2021精品视频免费播放| 国产乱妇高清无乱码免费| 亚洲精品国产第一综合99久久 | 亚洲精品国产手机| 久久亚洲国产精品一区二区| 全部免费毛片在线| 欧洲精品免费一区二区三区| www.黄色免费网站| 91福利视频免费观看| 国产成人一区二区三区视频免费|