• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            SmartPtr
            本博客已搬至:http://www.cnblogs.com/baiyanhuang/
            posts - 29,comments - 176,trackbacks - 0
            By SmartPtr(http://www.shnenglu.com/SmartPtr/)

                 Singleton應(yīng)該可以算是GOF23個(gè)模式中最簡(jiǎn)單的一個(gè)模式了,它有兩個(gè)要求:一是保證一個(gè)類僅有一個(gè)實(shí)例;二是提供一個(gè)訪問(wèn)它的全局訪問(wèn)點(diǎn)。這在實(shí)現(xiàn)中分別對(duì)應(yīng)為:一是構(gòu)造函數(shù)非public;二是提供一個(gè)靜態(tài)函數(shù)作為全局訪問(wèn)點(diǎn)。
              在
            C#中,我們可以這么寫:
            public class ExampleSingleton
            {
                
            // code to support Singleton
                protected ExampleSingleton(){}
                
            protected static ExampleSingleton instance = new ExampleSingleton();

                
            public static ExampleSingleton Instance
                {
                    
            get{return instance;}
                }

                
            // This class's real functionalities
                public void Write(){Console.WriteLine("Hello, World!");}
            }

            // use this singleton class
            ExampleSingleton.Instance.Write();
                類似的,C++中實(shí)現(xiàn)如下:
            class ExampleSingleton
            {
                
            // code to support Singleton
            protected:
                    ExampleSingleton(){}
            public:
                
            static ExampleSingleton& Instance()
                {
                    
            static ExampleSingleton instance;
                    
            return instance;
                }

                
            // This class's real functionalities
                void Write(){printf("Hello, World!");}
            };

            // use this singleton class
            ExampleSingleton::Instance().Write();

                這樣寫的確符合了singleton的兩個(gè)要求,但是如果我們的系統(tǒng)中有許多個(gè)Singleton類,而對(duì)于每一個(gè)類,我們都要寫那些固定的,重復(fù)的代碼去支持其singleton的屬性。這不是一個(gè)好現(xiàn)象,我們希望可以把這些固定的代碼提取出來(lái),使我們的Singleton類只需要專注于實(shí)現(xiàn)其真正的功能,相信很多人都聽說(shuō)過(guò)這個(gè)做法:Singleton模板基類。
            對(duì)于C#:
            // Singleton base class, each class need to be a singleton should 
            // derived from this class
            public class Singleton<T> where T: new()
            {
                
                
            // Instead of compile time check, we provide a run time check
                
            // to make sure there is only one instance.
                protected Singleton(){Debug.Assert(null == instance);}
                
            protected static T instance = new T();
                
            public static T Instance
                {
                    
            get{return instance;}
                }
            }

            // Concrete singleton class, derived from Singleton<T>
            public class ExampleSingleton: Singleton<ExampleSingleton>
            {
                
            // since there is no "freind class" in C#, we have to make
                
            // this contructor public to support the new constraint.
                public ExampleSingleton(){}

                
            // This class's real functionalities
                public void Write(){Console.WriteLine("Hello, World!");}
            }

            // use this singleton class
            ExampleSingleton.Instance.Write();

            這里,我們把為了支持Singleton功能的代碼提到一個(gè)Singleton<T>的類模板當(dāng)中,任何需要成為Singlton的類,只需從其派生便自然獲得Singleton功能。這里的一個(gè)問(wèn)題是:為了支持模板類中的new()constraint,我們不得不把作為具體singleton類的派生類的構(gòu)造函數(shù)作為public,這就導(dǎo)致我們無(wú)法在編譯期阻止用戶自行new出第二個(gè),第三個(gè)實(shí)例來(lái),但我們可以在運(yùn)行期來(lái)進(jìn)行檢查進(jìn)而保證其實(shí)例的單一性,這就是這singleton基類構(gòu)造函數(shù)的作用:
            protected Singleton(){Debug.Assert(null == instance);}

                當(dāng)然,有另外一種實(shí)現(xiàn)方法,那就是singleton基類不提供new() constraint, 這樣我們就可以讓ExampleSingleton的構(gòu)造函數(shù)為非public,在要?jiǎng)?chuàng)建T實(shí)例的時(shí)候,由于不能使用new, 我們用reflection反射出類型T的非public構(gòu)造函數(shù)并調(diào)用之。這樣,我們的確能保證編譯期實(shí)例的唯一性,但是由于用了反射,感覺代碼不是那么的簡(jiǎn)單優(yōu)雅,并且對(duì)其性能持保留態(tài)度,所以不太傾向與這種方法。
                但畢竟是越早檢查出錯(cuò)誤越好,所以大家如果有好的解決方案,不妨提出來(lái)一起討論討論。

             

            而C++中由于提供了友元這個(gè)特性,實(shí)現(xiàn)起來(lái)要好一些:
            // Singleton base class, each class need to be a singleton should 
            // derived from this class
            template <class T> class  Singleton
            {
            protected:
                Singleton(){}
            public:
                
            static T& Instance()
                {
                    
            static T instance;
                    
            return instance;
                }
            };

            // Concrete singleton class, derived from Singleton<T>
            class ExampleSingleton: public Singleton<ExampleSingleton>
            {
                
            // so that Singleton<ExampleSingleton> can access the 
                
            // protected constructor
                friend class Singleton<ExampleSingleton>;

            protected:
                    ExampleSingleton(){}
            public:
                
            // This class's real functionalities
                void Write(){printf("Hello, World!");}
            };

            // use this singleton class
            ExampleSingleton::Instance().Write();

            在C++友元的幫助下,我們成功實(shí)現(xiàn)了在編譯期保證實(shí)例的唯一性。(當(dāng)然,前提是你不要"亂交朋友")。

                有人可能會(huì)問(wèn),實(shí)現(xiàn)singleton的代碼并不多,我們沒必要搞這么一個(gè)機(jī)制來(lái)做代碼復(fù)用吧? 的確,我們復(fù)用的代碼并不是很多,但是,我想代碼復(fù)用的目的不僅僅是減少代碼量,其最重要的目的還是在于保持行為的一致性,以便于使用與維護(hù)。(用函數(shù)替換代碼段便是一個(gè)很好的例子)。
            對(duì)于這里的singleton類來(lái)講,如果不做這個(gè)設(shè)計(jì),我們?cè)诿總€(gè)具體的singleton類內(nèi)部實(shí)現(xiàn)其singleton機(jī)制,那么可能出現(xiàn)的問(wèn)題是
            1. 很難保證其接口的一致性
            張三寫了一個(gè)singleton類,全局訪問(wèn)函數(shù)是Instance, 李四也寫了一個(gè)Singleton類,全局訪問(wèn)函數(shù)可能就是GetInstance了。。。。。我們并沒有一個(gè)健壯的機(jī)制來(lái)保證接口的一致性,從而導(dǎo)致了使用的混亂性。

            2. 不易維護(hù)
            Singleton創(chuàng)建實(shí)例有兩種:一種為lazy initialization, 一種就是early initialization, 假如開始的實(shí)現(xiàn)是所有的singleton都用lazy initialization, 突然某種需求要求你用early initialization,你的噩夢(mèng)就開始了,你不得不重寫每一個(gè)singleton類。

            而用了singleton模板基類這種機(jī)制,以上問(wèn)題就不會(huì)存在,我們得到的不僅僅是節(jié)約幾行代碼:)

            posted on 2007-07-17 23:54 SmartPtr 閱讀(1828) 評(píng)論(7)  編輯 收藏 引用

            FeedBack:
            # re: Singleton模式在C#與C++中的實(shí)現(xiàn)
            2007-07-18 12:16 | ChenA
            呵呵,這個(gè)模式叫CRTP。
            local static不是線程安全的,哎,而且在c++ 08標(biāo)準(zhǔn)出來(lái)之前,singleton不可能是線程安全的。
            singleton只能在單線程里用用,基本就是雞肋。
              回復(fù)  更多評(píng)論
              
            # re: Singleton模式在C#與C++中的實(shí)現(xiàn)
            2007-07-18 12:50 | SmartPtr
            CRTP = the Curiously Recurring Template Pattern, 這里只是其應(yīng)用之一, 它還可以用于模擬虛函數(shù)等.
            關(guān)于Singleton的線程安全,雖然我們可以寫一些代碼來(lái)做到(或看起來(lái)做到)線程安全,但是由于現(xiàn)在C++內(nèi)存模型的先天不足, 事實(shí)是無(wú)法做到。
              回復(fù)  更多評(píng)論
              
            # re: Singleton模式在C#與C++中的實(shí)現(xiàn)
            2007-07-18 14:22 | Afreet
            @ChenA
            小心的問(wèn)一下,雙檢查加鎖模式呢……  回復(fù)  更多評(píng)論
              
            # re: Singleton模式在C#與C++中的實(shí)現(xiàn)
            2007-07-18 16:28 | eXile
            雙重鎖同樣是不安全的,所以盡量不要采用lazy initialization,
            對(duì)于early initialization, 一種安全的辦法可以參見:
            http://www.shnenglu.com/eXile/archive/2006/09/27/13034.html  回復(fù)  更多評(píng)論
              
            # re: Singleton模式在C#與C++中的實(shí)現(xiàn)
            2007-07-18 17:07 | anders06
            @ eXile
            你是學(xué)Java的? 雙重鎖在C#里是安全的.
            在Java里有人已經(jīng)寫了Singleton is evil  回復(fù)  更多評(píng)論
              
            # re: Singleton模式在C#與C++中的實(shí)現(xiàn)
            2007-07-18 17:26 | anders06
            >>不得不把作為具體singleton類的派生類的構(gòu)造函數(shù)作為public,這就導(dǎo)致我們無(wú)法在編譯期阻止用戶自行new出第二個(gè)

            你都說(shuō)了你的弊端了,讓我怎么說(shuō)你好呢:),還是老老實(shí)實(shí)寫吧,到目前為止我沒有找到個(gè)完美的方案.
            他們說(shuō)NGeneric中已經(jīng)有該功能的實(shí)現(xiàn),有空找來(lái)看看


              回復(fù)  更多評(píng)論
              
            # re: Singleton模式在C#與C++中的實(shí)現(xiàn)
            2007-07-18 18:34 | Afreet
            eXile
            回頭看看,確實(shí),DCLP由于編譯器,其匯編代碼是不確定一定能夠按序執(zhí)行的,所以同樣隱藏了線程安全問(wèn)題;當(dāng)然在高并發(fā)情況下的瓶頸也是一個(gè)重要因素。

            所以俺覺得類似于boost的early initialization可能更可取些。

            至于Java中的Singleton,問(wèn)題在于采用lazy initialization策略時(shí),如果沒有合理的同步,各個(gè)線程得到的實(shí)例可能不是同一個(gè)——而且這是由于JVM導(dǎo)致的,這就沒啥好法子了。C++可以用匯編寫線程庫(kù)控制代碼執(zhí)行順序,JVM呢?  回復(fù)  更多評(píng)論
              

            只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問(wèn)   Chat2DB   管理


            久久久噜噜噜久久中文字幕色伊伊 | 久久综合综合久久97色| 久久国产欧美日韩精品免费| 狠狠色丁香婷婷综合久久来| 精品国际久久久久999波多野| 久久人人爽人人爽人人av东京热 | 久久亚洲美女精品国产精品| 国产精品亚洲综合久久| 亚洲天堂久久久| 久久久久久午夜精品| 国产精品成人久久久| 久久无码国产专区精品| 久久精品国产亚洲AV忘忧草18| 伊人久久大香线蕉综合5g| 色综合合久久天天给综看| 久久精品国产亚洲AV无码偷窥| 午夜久久久久久禁播电影| 久久夜色精品国产亚洲| 色欲久久久天天天综合网精品| 天天爽天天狠久久久综合麻豆| 久久精品国产亚洲AV电影| 99久久免费国产精精品| 精品国产青草久久久久福利| 国产高清美女一级a毛片久久w| 日本精品一区二区久久久| 2021最新久久久视精品爱 | 久久久久国产一区二区三区| 四虎亚洲国产成人久久精品| 久久亚洲sm情趣捆绑调教| 99精品国产在热久久无毒不卡| 国产AV影片久久久久久| 久久九九久精品国产免费直播| 国产三级久久久精品麻豆三级| 久久久久久久久久久免费精品| 久久久久国产精品人妻| 99精品久久久久久久婷婷| 2021国产精品久久精品| 国产一区二区精品久久凹凸| 久久精品成人欧美大片| 久久人人爽人人爽人人片AV东京热| 久久AV无码精品人妻糸列|