• <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>
            Impossible is nothing  
              愛過知情重醉過知酒濃   花開花謝終是空   緣份不停留像春風(fēng)來又走   女人如花花似夢
            公告
            日歷
            <2025年5月>
            27282930123
            45678910
            11121314151617
            18192021222324
            25262728293031
            1234567
            統(tǒng)計
            • 隨筆 - 8
            • 文章 - 91
            • 評論 - 16
            • 引用 - 0

            導(dǎo)航

            常用鏈接

            留言簿(4)

            隨筆分類(4)

            隨筆檔案(8)

            文章分類(77)

            文章檔案(91)

            相冊

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

             

            Singleton模式是常用的設(shè)計模式之一,但是要實(shí)現(xiàn)一個真正實(shí)用的設(shè)計模式卻也不是件容易的事情。

            1.         標(biāo)準(zhǔn)的實(shí)現(xiàn)

            class Singleton

            {

            public:

                   static Singleton * Instance()

                   {

                          if( 0== _instance)

                          {

                                 _instance = new Singleton;

                          }

                          return _instance;

                   }

            protected:

                   Singleton(void)

                   {

                   }

                   virtual ~Singleton(void)

                   {

                   }

                   static Singleton* _instance;

            };

                   這是教科書上使用的方法。看起來沒有什么問題,其實(shí)包含很多的問題。下面我們一個一個的解決。

            2.         自動垃圾回收

            上面的程序必須記住在程序結(jié)束的時候,釋放內(nèi)存。為了讓它自動的釋放內(nèi)存,我們引入auto_ptr改變它。

            #include <memory>

            #include <iostream>

            using namespace std;

            class Singleton

            {

            public:

                   static Singleton * Instance()

                   {

                          if( 0== _instance.get())

                          {

                                 _instance.reset( new Singleton);

                          }

                          return _instance.get();

                   }

            protected:

                   Singleton(void)

                   {

                          cout <<"Create Singleton"<<endl;

                   }

                   virtual ~Singleton(void)

                   {

                          cout << "Destroy Singleton"<<endl;

                   }

                   friend class auto_ptr<Singleton>;

                   static auto_ptr<Singleton> _instance;

            };

            //Singleton.cpp

            auto_ptr<Singleton> Singleton::_instance;

            3.         增加模板

            在我的一個工程中,有多個的Singleton類,對Singleton類,我都要實(shí)現(xiàn)上面這一切,這讓我覺得煩死了。于是我想到了模板來完成這些重復(fù)的工作。

            現(xiàn)在我們要添加本文中最吸引人單件實(shí)現(xiàn):

            /********************************************************************

                (c) 2003-2005 C2217 Studio

                Module:    Singleton.h

                Author:     Yangjun D.

                Created:    9/3/2005   23:17

                Purpose:    Implement singleton pattern

                History:

            *********************************************************************/

            #pragma once

             

            #include <memory>

            using namespace std;

            using namespace C2217::Win32;

             

            namespace C2217

            {

            namespace Pattern

            {

            template <class T>

            class Singleton

            {

            public:

                   static inline T* instance();

                  

            private:

                   Singleton(void){}

                   ~Singleton(void){}

                   Singleton(const Singleton&){}

                   Singleton & operator= (const Singleton &){}

             

                   static auto_ptr<T> _instance;

            };

             

            template <class T>

            auto_ptr<T> Singleton<T>::_instance;

             

            template <class T>

             inline T* Singleton<T>::instance()

            {

                   if( 0== _instance.get())

                   {

                          _instance.reset ( new T);

                   }

                  

                   return _instance.get();

            }

             

            //Class that will implement the singleton mode,

            //must use the macro in it's delare file

            #define DECLARE_SINGLETON_CLASS( type ) \

                   friend class auto_ptr< type >;\

                   friend class Singleton< type >;

            }

            }

             

            4.         線程安全

            上面的程序可以適應(yīng)單線程的程序。但是如果把它用到多線程的程序就會發(fā)生問題。主要的問題在于同時執(zhí)行_instance.reset ( new T); 就會同時產(chǎn)生兩個新的對象,然后馬上釋放一個,這跟Singleton模式的本意不符。所以,你需要更加安全的版本:

            /********************************************************************

                (c) 2003-2005 C2217 Studio

                Module:    Singleton.h

                Author:     Yangjun D.

                Created:    9/3/2005   23:17

                Purpose:    Implement singleton pattern

                History:

            *********************************************************************/

            #pragma once

             

            #include <memory>

            using namespace std;

            #include "Interlocked.h"

            using namespace C2217::Win32;

             

            namespace C2217

            {

            namespace Pattern

            {

            template <class T>

            class Singleton

            {

            public:

                   static inline T* instance();

                  

            private:

                   Singleton(void){}

                   ~Singleton(void){}

                   Singleton(const Singleton&){}

                   Singleton & operator= (const Singleton &){}

             

                   static auto_ptr<T> _instance;

                   static CResGuard _rs;

            };

             

            template <class T>

            auto_ptr<T> Singleton<T>::_instance;

             

            template <class T>

            CResGuard Singleton<T>::_rs;

             

            template <class T>

             inline T* Singleton<T>::instance()

            {

                   if( 0 == _instance.get() )

                   {

                          CResGuard::CGuard gd(_rs);

                          if( 0== _instance.get())

                          {

                                 _instance.reset ( new T);

                          }

                   }

                   return _instance.get();

            }

             

            //Class that will implement the singleton mode,

            //must use the macro in it's delare file

            #define DECLARE_SINGLETON_CLASS( type ) \

                   friend class auto_ptr< type >;\

                   friend class Singleton< type >;

            }

            }

                   CresGuard 類主要的功能是線程訪問同步,代碼如下:

            /******************************************************************************

            Module:  Interlocked.h

            Notices: Copyright (c) 2000 Jeffrey Richter

            ******************************************************************************/

             

             

            #pragma once

            ///////////////////////////////////////////////////////////////////////////////

             

            // Instances of this class will be accessed by multiple threads. So,

            // all members of this class (except the constructor and destructor)

            // must be thread-safe.

            class CResGuard {

            public:

               CResGuard()  { m_lGrdCnt = 0; InitializeCriticalSection(&m_cs); }

               ~CResGuard() { DeleteCriticalSection(&m_cs); }

             

               // IsGuarded is used for debugging

               BOOL IsGuarded() const { return(m_lGrdCnt > 0); }

             

            public:

               class CGuard {

               public:

                  CGuard(CResGuard& rg) : m_rg(rg) { m_rg.Guard(); };

                  ~CGuard() { m_rg.Unguard(); }

             

               private:

                  CResGuard& m_rg;

               };

             

            private:

               void Guard()   { EnterCriticalSection(&m_cs); m_lGrdCnt++; }

               void Unguard() { m_lGrdCnt--; LeaveCriticalSection(&m_cs); }

             

               // Guard/Unguard can only be accessed by the nested CGuard class.

               friend class CResGuard::CGuard;

             

            private:

               CRITICAL_SECTION m_cs;

               long m_lGrdCnt;   // # of EnterCriticalSection calls

            };

             

             

            ///////////////////////////////////////////////////////////////////////////////

            5.         實(shí)用方法

            比如你有一個需要實(shí)現(xiàn)單件模式的類,就應(yīng)該這樣實(shí)現(xiàn):

            #pragma once

            #include "singleton.h"

            using namespace C2217::Pattern;

             

            class ServiceManger

            {

            public:

                   void Run()

                   {

                   }

            private:

                   ServiceManger(void)

                   {

                   }

                   virtual ~ServiceManger(void)

                   {

                   }

                   DECLARE_SINGLETON_CLASS(ServiceManger);

            };

             

            typedef Singleton<ServiceManger> SSManger;

             

            在使用的時候很簡單,跟一般的Singleton實(shí)現(xiàn)的方法沒有什么不同。

            int _tmain(int argc, _TCHAR* argv[])

            {

                    SSManger::instance()->Run();

            }

             

            一個簡單的Singleton模式的實(shí)現(xiàn),可以看到C++語言背后隱藏的豐富的語意,我希望有人能實(shí)現(xiàn)一個更好的Singleton讓大家學(xué)習(xí)。我從一開始實(shí)現(xiàn)Singleton類的過程,其實(shí)就是我學(xué)習(xí)C++的過程,越是深入越覺得C++了不起。

            posted on 2006-03-08 23:14 笑笑生 閱讀(487) 評論(1)  編輯 收藏 引用 所屬分類: C++語言
            評論:
            • # re: Singleton模式的演化[未登錄]  galaxy Posted @ 2007-05-16 15:08
              也準(zhǔn)備設(shè)計一個可以復(fù)用的單件模式的代碼

              不過要是能夠把單線程和多線程的單件分開就好了
              要是單線程中多次用到你的單件, 那么同步開銷就不值得了
              呵呵,準(zhǔn)備好好看看ace中的設(shè)計模式  回復(fù)  更多評論   

             
            Copyright © 笑笑生 Powered by: 博客園 模板提供:滬江博客
            无码人妻久久一区二区三区| 国产一区二区精品久久| 久久婷婷国产麻豆91天堂| 伊人久久大香线蕉AV一区二区| 久久国产精品成人免费| 久久精品无码专区免费东京热| 亚洲综合伊人久久大杳蕉| 无码国内精品久久综合88| 久久伊人中文无码| 亚洲乱码日产精品a级毛片久久| 欧美精品丝袜久久久中文字幕| 久久久久人妻精品一区三寸蜜桃| 久久se精品一区精品二区国产| 国内精品免费久久影院| 久久久久国产精品麻豆AR影院| 免费一级做a爰片久久毛片潮| 热久久视久久精品18| 亚洲成av人片不卡无码久久| 久久久久人妻一区精品果冻| 久久午夜免费视频| 欧美精品乱码99久久蜜桃| 国产毛片欧美毛片久久久| 亚洲国产精品无码久久久蜜芽| 无码精品久久久天天影视| 久久99国内精品自在现线| 色偷偷888欧美精品久久久| 久久精品国产一区二区| 综合久久久久久中文字幕亚洲国产国产综合一区首 | 国产成人无码精品久久久久免费 | 国产高潮国产高潮久久久| 国产精品久久久久久久久免费| 18岁日韩内射颜射午夜久久成人 | 久久久久婷婷| 亚洲精品无码久久千人斩| 99久久无色码中文字幕| 久久久国产精华液| 无码人妻精品一区二区三区久久| 2021国产成人精品久久| 久久国产精品无| 99久久国产综合精品五月天喷水| 久久久国产视频|