这个单实例模式有什么问题吗?

freshow 2010-02-02 05:32:23
这样写有什么问题吗?目的就是做单实例模式
指针的创建和销毁都通过函数调用实现

// 头文件
public:
static CBMUService* CreateInstance();
static CBMUService* GetInstance();
static CBMUService* DestroyInstance();

// 源文件
CService* CService::m_pInstance = NULL;

CService* CService::CreateInstance()
{
if (NULL == m_pInstance)
{
m_pInstance = new CService;
if (NULL != m_pInstance)
{
return m_pInstance;
}
else
{
return NULL;
}
}
}

/***********************************************************************
* description: 获取实例指针
* remark:
************************************************************************/
CService* CService::GetInstance()
{
return m_pInstance;
}

/***********************************************************************
* description: 销毁实例
* remark:
************************************************************************/
CService* CService::DestroyInstance()
{
if (NULL != m_pInstance)
{
delete m_pInstance;
m_pInstance = NULL;
}
}
...全文
105 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
macrojj 2010-02-03
  • 打赏
  • 举报
回复
如果你销毁了 然后又有线程不晓得 又调用了你的单件怎么办

参考《设计新思维》 有章对单件的单独探讨。 有个phoenix singleton
taodm 2010-02-03
  • 打赏
  • 举报
回复
呃,讨论单件的销毁问题,请先找本《modern c++ design》看看,免得闭门造车。
另外,没加锁的单件,还不如全局变量了。
freshow 2010-02-03
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 xylicon 的回复:]
引用 5 楼 freshow 的回复:
DestroyInstance如果不用,就是在需要释放的地方写delete了~大家觉得对吧?


DestroyInstance 是必须的。要不你new出来的资源怎么释放。
[/Quote]

谁NEW谁释放
xylicon 2010-02-02
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 freshow 的回复:]
DestroyInstance如果不用,就是在需要释放的地方写delete了~大家觉得对吧?
[/Quote]

DestroyInstance 是必须的。要不你new出来的资源怎么释放。
xylicon 2010-02-02
  • 打赏
  • 举报
回复
CService* CService::CreateInstance() 
{
if (NULL == m_pInstance)
{
m_pInstance = new CService;
// 既然m_pInstance非NULL return m_pInstance;
// m_pInstance NULL 也是return m_pInstance; (NULL)
// 那下面这个if...else...语句直接用 return m_pInstance;就行了
if (NULL != m_pInstance)
{
return m_pInstance;
}
else
{
return NULL;
}
}

// 其实这里应该return m_pInstance的,因为如果 NULL != m_pInstance该怎么办呢
}
cattycat 2010-02-02
  • 打赏
  • 举报
回复
你这个还要考虑singleton 的子类也单件吧。
一种方法是在父类中根据1个变量来选择要单件的singleton子类。
一种方法是在子类中实现instance方法,这种不够灵活。
下面是第一种方法。第二种就不举例了。

CService* CService::Instance()
{
if (NULL == m_pInstance)
{
const char* servicename=getenv("serviceName");
if(strcmp(servicename,"service1")==0)
m_pInstance = new CService1;
else if(strcmp(servicename,"service2")==0)
m_pInstance = new CService2;
//... 下省略
else
m_pInstance = new CServicen;
}

return m_pInstance;
}
freshow 2010-02-02
  • 打赏
  • 举报
回复
DestroyInstance如果不用,就是在需要释放的地方写delete了~大家觉得对吧?
kingstarer 2010-02-02
  • 打赏
  • 举报
回复
ps:

if (NULL != m_pInstance)
{
return m_pInstance;
}
else
{
return NULL;
}

这不就是return m_pInstance;吗 写这么多if else干嘛
kingstarer 2010-02-02
  • 打赏
  • 举报
回复
这个应该是单线程的单例 没有用到锁的

DestroyInstance是为了释放的 其实没多大必要,可以用auto_ptr代替
freshow 2010-02-02
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 chenyu2202863 的回复:]
单线程没问题,多线程中Double Check也不一定行,
具体请Google陈硕的《当析构遇到多线程》文章
[/Quote]

DestroyInstance是不是有点多余?其实没有也可以?
chenyu2202863 2010-02-02
  • 打赏
  • 举报
回复
单线程没问题,多线程中Double Check也不一定行,
具体请Google陈硕的《当析构遇到多线程》文章

5,529

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 模式及实现
社区管理员
  • 模式及实现社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧