• <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>
            隨筆-167  評(píng)論-8  文章-0  trackbacks-0

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

            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ū)上使用的方法。看起來(lái)沒(méi)有什么問(wèn)題,其實(shí)包含很多的問(wèn)題。下面我們一個(gè)一個(gè)的解決。

            2.         自動(dòng)垃圾回收

            上面的程序必須記住在程序結(jié)束的時(shí)候,釋放內(nèi)存。為了讓它自動(dòng)的釋放內(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.         增加模板

            在我的一個(gè)工程中,有多個(gè)的Singleton類(lèi),對(duì)Singleton類(lèi),我都要實(shí)現(xiàn)上面這一切,這讓我覺(jué)得煩死了。于是我想到了模板來(lái)完成這些重復(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)單線程的程序。但是如果把它用到多線程的程序就會(huì)發(fā)生問(wèn)題。主要的問(wèn)題在于同時(shí)執(zhí)行_instance.reset ( new T); 就會(huì)同時(shí)產(chǎn)生兩個(gè)新的對(duì)象,然后馬上釋放一個(gè),這跟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 類(lèi)主要的功能是線程訪問(wèn)同步,代碼如下:

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

            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í)用方法

            比如你有一個(gè)需要實(shí)現(xiàn)單件模式的類(lèi),就應(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;

             

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

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

            {

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

            }

             

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

            posted on 2012-04-05 16:08 老馬驛站 閱讀(214) 評(píng)論(0)  編輯 收藏 引用
            99热精品久久只有精品| 91久久精品91久久性色| 无码人妻少妇久久中文字幕| 性做久久久久久久久浪潮| 色综合久久久久综合体桃花网| 色综合色天天久久婷婷基地| 国产色综合久久无码有码| 91亚洲国产成人久久精品| A狠狠久久蜜臀婷色中文网| 超级97碰碰碰碰久久久久最新| 久久99精品久久久久久9蜜桃 | 亚洲AV无码1区2区久久 | 日韩精品久久久久久免费| 久久九九免费高清视频| 2021久久国自产拍精品| 国产精品免费久久久久影院| 亚洲国产成人久久综合一区77| 久久精品国产半推半就| 久久超乳爆乳中文字幕| 久久人人爽人人爽人人片AV不 | 亚洲精品NV久久久久久久久久 | 久久国产免费直播| 久久99热国产这有精品| 青青草原综合久久大伊人导航 | 色天使久久综合网天天| 久久精品国产亚洲欧美| 伊人色综合久久天天人手人婷| 国产AⅤ精品一区二区三区久久 | 久久国产成人午夜AV影院| 色婷婷综合久久久久中文一区二区 | 91精品无码久久久久久五月天| 2021国产精品久久精品| 国产精品女同一区二区久久| 人妻无码αv中文字幕久久| 亚洲欧美日韩久久精品| 99久久婷婷国产一区二区| 亚洲国产精品无码久久久不卡| 久久人人爽人人爽人人片AV东京热 | 久久国产亚洲高清观看| 香蕉久久夜色精品国产2020 | 久久亚洲中文字幕精品一区|