设为首页 加入收藏

TOP

C++完美实现Singleton模式(一)
2012-12-10 12:49:19 来源: 作者: 【 】 浏览:991
Tags:完美 实现 Singleton 模式

    Singleton模式是常用的设计模式之一,但是要实现一个真正实用的设计模式却也不是件容易的事情.标准的实现

    1 class Singleton

    2 {

    3 public:

    4        static Singleton * Instance()

    5        {

    6               if( 0== _instance)

    7               {

    8                      _instance = new Singleton;

    9               }

    10               return _instance;

    11        }

    12 protected:

    13        Singleton(void)

    14        {

    15        }

    16        virtual ~Singleton(void)

    17        {

    18        }

    19        static Singleton* _instance;

    20 };

    这是教科书上使用的方法.看起来没有什么问题,其实包含很多的问题.下面我们一个一个的解决.

    问题一  自动垃圾回收上面的程序必须记住在程序结束的时候,释放内存.为了让它自动的释放内存,我们引入auto_ptr改变它.

    1 #include <memory>

    2 #include <iostream>

    3 using namespace std;

    4 class Singleton

    5 {

    6 public:

    7        static Singleton * Instance()

    8        {

    9               if( 0== _instance.get())

    10               {

    11                      _instance.reset( new Singleton);

    12               }

    13               return _instance.get();

    14        }

    15 protected:

    16        Singleton(void)

    17        {

    18               cout <<“Create Singleton”<<endl;

    19        }

    20        virtual ~Singleton(void)

    21        {

    22               cout << “Destroy Singleton”<<endl;

    23        }

    24        friend class auto_ptr<Singleton>;

    25        static auto_ptr<Singleton> _instance;

    26 };

    27 //Singleton.cpp

    28 auto_ptr<Singleton> Singleton::_instance;

    问题二  增加模板在我的一个工程中,有多个的Singleton类,对Singleton类,我都要实现上面这一切,这让我觉得烦死了.于是我想到了模板来完成这些重复的工作.现在我们要添加本文中最吸引人单件实现:

    1 #include <memory>

    2 using namespace std;

    3 using namespace C2217::Win32;

    4

    5 namespace C2217

    6 {

    7 namespace Pattern

    8 {

    9 template <class T>

    10 class Singleton

    11 {

    12 public:

    13        static inline T* instance();

    14

    15 private:

    16        Singleton(void){}

    17        ~Singleton(void){}

    18        Singleton(const Singleton&){}

    19        Singleton & operator= (const Singleton &){}

    20

    21        static auto_ptr<T> _instance;

    22 };

    23

    24 template <class T>

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

    26

    27 template <class T>

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

    29 {

    30        if( 0== _instance.get())

    31        {

    32               _instance.reset ( new T);

    33        }

    34

    35        return _instance.get();

    36 }

    37

    38 //Class that will implement the singleton mode,

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

    40 #define DECLARE_SINGLETON_CLASS( type ) \

    41        friend class auto_ptr< type >;\

    42        friend class Singleton< type >;

    43 }

    44 }

    问题三  线程安全上面的程序可以适应单线程的程序.但是如果把它用到多线程的程序就会发生问题.主要的问题在于同时执行_instance.reset ( new T); 就会同时产生两个新的对象,然后马上释放一个,这跟Singleton模式的本意不符.所以,你需要更加安全的版本:

    1 #include <memory>

    2 using namespace std;

    3 #include “Interlocked.h”

    4 using namespace C2217::Win32;

    5

    6 namespace C2217

    7 {

    8 namespace Pattern

    9 {

    10 template <class T>

    11 class Singleton

    12 {

    13 public:

    14        static inline T* instance();

    15

    16 private:

    17        Singleton(void){}

    18        ~Singleton(void){}

    19        Singleton(const Singleton&){}

    20        Singleton & operator= (const Singleton &){}

    21

    22        static auto_ptr<T> _instance;

    23        static CResGuard _rs;

    24 };

    25

    26 template <class T>

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

    28

    29 template <class T>

    30 CResGuard Singleton<T>::_rs;

    31

    32 template <class T>

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

    34 {

    35        if( 0 == _instance.get() )

    36        {

    37               CResGuard::CGuard gd(_rs);

    38               if( 0== _instance.get())

    39               {

    40                      _instance.reset ( new T);

    41               }

    42        }

    43        return _instance.get();

    44 }

    45

    46 //Class that will implement the singleton mode,

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

    48 #define DECLARE_SINGLETON_CLASS( type ) \

    49        friend class auto_ptr< type >;\

    50        friend class Singleton< type >;

    51 }

    52 }

   

首页 上一页 1 2 3 4 下一页 尾页 1/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇C++将字符串转换成数字 下一篇C++拷贝构造函数详解

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: