• <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  評論-8  文章-0  trackbacks-0

            Singleton模式是常用的設計模式之一,但是要實現一個真正實用的設計模式卻也不是件容易的事情。

            1.         標準的實現

            class Singleton

            {

            public:

                   static Singleton * Instance()

                   {

                          if( 0== _instance)

                          {

                                 _instance = new Singleton;

                          }

                          return _instance;

                   }

            protected:

                   Singleton(void)

                   {

                   }

                   virtual ~Singleton(void)

                   {

                   }

                   static Singleton* _instance;

            };

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

            2.         自動垃圾回收

            上面的程序必須記住在程序結束的時候,釋放內存。為了讓它自動的釋放內存,我們引入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類,我都要實現上面這一切,這讓我覺得煩死了。于是我想到了模板來完成這些重復的工作。

            現在我們要添加本文中最吸引人單件實現:

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

                (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.         線程安全

            上面的程序可以適應單線程的程序。但是如果把它用到多線程的程序就會發生問題。主要的問題在于同時執行_instance.reset ( new T); 就會同時產生兩個新的對象,然后馬上釋放一個,這跟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.         實用方法

            比如你有一個需要實現單件模式的類,就應該這樣實現:

            #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實現的方法沒有什么不同。

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

            {

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

            }

             

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

            posted on 2012-04-05 16:08 老馬驛站 閱讀(218) 評論(0)  編輯 收藏 引用
            精品熟女少妇av免费久久| 久久香蕉综合色一综合色88| 一级做a爱片久久毛片| 亚洲欧美成人综合久久久| 欧美久久久久久| 久久强奷乱码老熟女网站| 久久99国产亚洲高清观看首页| 亚洲午夜久久久久久噜噜噜| 亚洲精品无码专区久久同性男| 99热成人精品免费久久| 久久精品成人影院| 久久亚洲高清综合| 人妻无码精品久久亚瑟影视| 色老头网站久久网| 亚洲精品乱码久久久久久| 精品久久久噜噜噜久久久 | 国产ww久久久久久久久久| 亚洲国产精品久久久久久| 亚洲综合婷婷久久| 久久99精品久久久久久9蜜桃 | 99久久人人爽亚洲精品美女| 国产精品美女久久久久网| 91精品国产91久久久久久青草| 99热精品久久只有精品| 武侠古典久久婷婷狼人伊人| 精品伊人久久大线蕉色首页| 国内精品久久久久久99蜜桃| 99久久免费国产特黄| 国产亚洲精久久久久久无码AV| 香蕉久久影院| 国产人久久人人人人爽| 久久九色综合九色99伊人| 精品久久久无码人妻中文字幕 | 国内精品九九久久久精品| 青青青青久久精品国产h| 97视频久久久| 一本久久a久久精品综合夜夜| 亚洲精品无码久久毛片| 久久久青草青青亚洲国产免观| 亚洲欧美成人久久综合中文网| 91精品国产色综合久久|