Singleton模式的析构函数

单件模式(singleton)是很简单的设计模式,一般是为了达到只提供一个类的实例的作用。这里侧重谈实现该模式时的析构函数(针对C++的实现,Java中也没有析构函数这个概念吧)

一般单件模式需要有一个静态的指针来持有(hold/save)这个唯一的实例:

static SingleInst * g_Instance = NULL;

class  SingleInst
{
public:
 static SingleInst* GetInstance();
private:
 SingleInst();
public:
 ~SingleInst();
};

首先,为类SingleInst提供析构函数是好习惯,并且在大多数情况(当在实例的初始化中分配"额外"的内存时)下是必须的。

那么如何来实现呢?

  1. SingleInst::~SingleInst()
  2. {
  3.  // First  
  4.  // free the extra memory when call constructor function.
  5.  // Second , anything else
  6. ......
  7.  // Finally, set the g_Instance to NULL
  8.  g_Instance  =  NULL;
  9. }

这是因为 GetInstance()的实现大多如下:

SingleInst*  SingleInst::GetInstance()
{
 if (g_Instance == NULL)
  g_Instance  = new SingleInst();
 return  g_Instance;
}

若在客户程序中有如下的调用顺序:

SingleInst * single = SingleInst::GetInstance();  // 1

/* Do something */
.....

/* Then delete the SingleInst object */              // 2
delete single;
single = NULL;

/* Get the SingleInst object  again */                // 3
single = SingleInst::GetInstance();

如果没有析构函数中g_Instance = NULL,那么以上调用顺序就可能会出现问题。例如:

 

  1. // Compiler: VC 6.0
  2. // My Email: cwgk_zc@163.com
  3. // Date    : 2008-08-07
  4. // If you have any question and suggest about this code
  5. // segment, please give me a mail.
  6. #include <iostream.h>
  7. class SingExm;
  8. static SingExm * g_instance = NULL;
  9. class  SingExm
  10. {
  11. public:
  12.  static SingExm* GetInstance();
  13.  void setValue(int newVal) { *p_iVal = newVal;}
  14.  int getValue() {return *p_iVal;}
  15. private:
  16.  SingExm() { p_iVal = new int(9); }
  17.  int     * p_iVal;
  18. public:
  19.  ~SingExm();
  20. };
  21. SingExm* SingExm::GetInstance()
  22. {
  23.  if(g_instance)
  24.   return g_instance;
  25.  else
  26.   g_instance = new SingExm();
  27.  return g_instance;
  28. }
  29. SingExm::~SingExm()
  30. {
  31.  // Pay attention to the following line
  32.  g_instance = NULL; // If you delete this line, then you will get a runtime error.
  33.         // You would have a try. (General that error is "access violation" on Windows or
  34.         // "segment fault" on Like-Unix.
  35. }
  36. int main()
  37. {
  38.  SingExm * pSe = SingExm::GetInstance();
  39.  cout<<"Value is"<<pSe->getValue()<<endl;
  40.  pSe->setValue(99);
  41.  cout<<"Value is"<<pSe->getValue()<<endl;
  42.  // Now we delete the pSe
  43.  delete pSe;
  44.  pSe = SingExm::GetInstance();
  45.  cout<<"Value is"<<pSe->getValue()<<endl;
  46.  pSe->setValue(25);
  47.  cout<<"Value is"<<pSe->getValue()<<endl;
  48.  return 0;
  49. }

总结,一般来讲在单件模式中,实例在程序结束时进行销毁。可是偏偏有一段程序如以上main函数中使用单件模式恰巧让我碰上了,于是有了我这一段文字。(为单件模式提供一个析构函数,并在析构函数中将g_instance设置为NULL)。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值