王朝网络
分享
 
 
 

C++完美实现Singleton模式

王朝c/c++·作者佚名  2006-02-01
宽屏版  字体: |||超大  

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++了不起。

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
>>返回首页<<
推荐阅读
 
 
频道精选
 
静静地坐在废墟上,四周的荒凉一望无际,忽然觉得,凄凉也很美
© 2005- 王朝网络 版权所有