您现在的位置:计算机技术学习网 > 技术中心 > 编程开发 > C >

C++中实现Singleton的正确方法

来源:未知 责任编辑:责任编辑 发表时间:2013-08-27 15:59 点击:

如果某个类管理了系统中唯一的某种资源,那么我们只能创建该类的一个实例,此时用到singleton设计模式(后面为了简化将省略“设计模式”四个字)就比较合适了。然而,如果不注意实现方法,就很有可能会让我们碰到一些莫名其妙的错误。图1是经过简化所得到的一个实现错误的例子。

  1. main.c 
  2. 00001: #include <iostream> 
  3. 00002: 
  4. 00003: using namespace std; 
  5. 00004: 
  6. 00005: class singleton1_t 
  7. 00006: { 
  8. 00007: public
  9. 00008:     static singleton1_t *instance () 
  10. 00009:     { 
  11. 00010:         return &instance_; 
  12. 00011:     } 
  13. 00012: 
  14. 00013:     void count_increase () {count_ ++;} 
  15. 00014:     int count () const {return count_;} 
  16. 00015: 
  17. 00016: private
  18. 00017:     singleton1_t (): count_ (0) {} 
  19. 00018:     ~singleton1_t () {} 
  20. 00019: 
  21. 00020:     static singleton1_t instance_; 
  22. 00021:     int count_; 
  23. 00022: }; 
  24. 00023: 
  25. 00024: class singleton2_t 
  26. 00025: { 
  27. 00026: public
  28. 00027:     static singleton2_t *instance () 
  29. 00028:     { 
  30. 00029:         return &instance_; 
  31. 00030:     } 
  32. 00031: 
  33. 00032: private
  34. 00033:     singleton2_t () {singleton1_t::instance ()->count_increase ();} 
  35. 00034:     ~singleton2_t () {} 
  36. 00035: 
  37. 00036:     static singleton2_t instance_; 
  38. 00037: }; 
  39. 00038: 
  40. 00039: singleton2_t singleton2_t::instance_; 
  41. 00040: singleton1_t singleton1_t::instance_; 
  42. 00041: 
  43. 00042: int main () 
  44. 00043: { 
  45. 00044:     (void) singleton2_t::instance (); 
  46. 00045:     cout << "count = " << singleton1_t::instance ()->count () << endl; 
  47. 00046:     return 0; 
  48. 00047: } 

图1 

 

图中的两个类在实现singleton时都将类的构造和析构函数的对外可视性设为private,这是实现singleton首先要注意的一个点。通过这一手段,有助于预防他人粗心地定义类实例。


图中的singleton2_t类在其构造函数中调用singleton1_t类的count_increase ()方法使计数加一。第44行的代码用于代表使用singleton2_t实例。第46行代码则显示singleton1_t类的记数信息。图2示例了该程序的运行结果。

  1. g++ main.cpp -o singleton.exe 
  2. ./singleton.exe 
  3. count = 0 

图2 

 

是不是对于最终的显示计数为0而不是1感到奇怪?错误发生的原因在于,singleton2_t类实例的构造是先于singleton1_t类的,当singleton1_t类的实例在最后构造时会把count_变量置成0,从而覆盖singleton2_t的构造函数所引起的变更。 


尽管这是一个精心设计的错误,但在大型项目中出现这类错误的可能性却并不小。因为在现实项目中,singleton1_t和singleton2_t两个类的实现很可能是在不同的源文件中,这势必造成两个类实例的初始化顺序会因链接顺序不同而不同,《揭示C++中全局类变量的构造与析构顺序 》一文介绍了这是为什么。


在本例中,如果将第39行和第40行的代码进行对调就不会出现这种奇怪的现象,但这不是解决问题的终极方法。更好的方法需要更改singleton的实现方法,图3示例了一种新的实现方法。

  1. main.c 
  2. 00001: #include <iostream> 
  3. 00002: 
  4. 00003: using namespace std; 
  5. 00004: 
  6. 00005: class singleton1_t 
  7. 00006: { 
  8. 00007: public
  9. 00008:     static singleton1_t *instance () 
  10. 00009:     { 
  11. 00010:         if (0 == p_instance_) { 
  12. 00011:             p_instance_ = new singleton1_t; 
  13. 00012:         } 
  14. 00013:         return p_instance_; 
  15. 00014:     } 
  16. 00015: 
  17. 00016:     void count_increase () {count_ ++;} 
  18. 00017:     int count () const {return count_;} 
  19. 00018: 
  20. 00019: private
  21. 00020:     singleton1_t (): count_ (0) {} 
  22. 00021:     ~singleton1_t () {} 
  23. 00022: 
  24. 00023:     static singleton1_t *p_instance_; 
  25. 00024:     int count_; 
  26. 00025: }; 
  27. 00026: 
  28. 00027: class singleton2_t 
  29. 00028: { 
  30. 00029: public
  31. 00030:     static singleton2_t *instance () 
  32. 00031:     { 
  33. 00032:         if (0 == p_instance_) { 
  34. 00033:             p_instance_ = new singleton2_t; 
  35. 00034:         } 
  36. 00035:         return p_instance_; 
  37. 00036:     } 
  38. 00037: 
  39. 00038: private
  40. 00039:     singleton2_t () {singleton1_t::instance ()->count_increase ();} 
  41. 00040:     ~singleton2_t () {} 
  42. 00041: 
  43. 00042:     static singleton2_t *p_instance_; 
  44. 00043: }; 
  45. 00044: 
  46. 00045: singleton2_t *singleton2_t::p_instance_ = 0; 
  47. 00046: singleton1_t *singleton1_t::p_instance_ = 0; 
  48. 00047: 
  49. 00048: int main () 
  50. 00049: { 
  51. 00050:     singleton2_t::instance (); 
  52. 00051:     cout << "count = " << singleton1_t::instance ()->count () << endl; 
  53. 00052:     return 0; 
  54. 00053: } 

图3 

 

新实现最大的变化,在于将以前的类静态变量从类实例变成了类指针,并在instance()函数中需要时通过new操作符创建类实例。指针在C++中仍是当作一种原始数据类型处理的,其初始化与类实例的初始化不同,不需调用类构造函数。在这一实现中,两个类的静态变量p_instance_的初始化都是在程序的.bss段初始化时一次性完成的。

 

这一实现中由于类的实例是通过new操作符获得的,所以需要为类定义释放实例的函数(图中省略了),并由在合适的时机调用。为了省去这类麻烦,作者更推崇图4所示的实现方式。

  1. main.c 
  2. 00005: class singleton1_t 
  3. 00006: { 
  4. 00007: public
  5. 00008:     static singleton1_t *instance () 
  6. 00009:     { 
  7. 00010:         if (0 == p_instance_) { 
  8. 00011:             static singleton1_t instance; 
  9. 00012:             p_instance_ = &instance; 
  10. 00013:         } 
  11. 00014:         return p_instance_; 
  12. 00015:     } 
  13. 00016: 
  14. 00017:     void count_increase () {count_ ++;} 
  15. 00018:     int count () const {return count_;} 
  16. 00019: 
  17. 00020: private
  18. 00021:     singleton1_t (): count_ (0) {} 
  19. 00022:     ~singleton1_t () {} 
  20. 00023: 
  21. 00024:     static singleton1_t *p_instance_; 
  22. 00025:     int count_; 
  23. 00026: }; 

 图4 

通过在函数内部定义静态变量的方法获得类实例,一方面简化了类接口的实现,另一方面又降低了因为忘记调用释放接口函数而导致内存泄漏的可能。需要提醒的是,在这种实现方法中,类实例的构造是发生在各类的instance()函数第一次被调用时,而各实例的析构又是以与构造相反的顺序进行的,且后者是由编程语言环境所保证的。  

 

本文出自 “至简李云” 博客,请务必保留此出处http://yunli.blog.51cto.com/831344/758684

    发表评论
    请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
    用户名: 验证码:点击我更换图片
    最新评论 更多>>
    网站首页 - 友情链接 - 网站地图 - TAG标签 - RSS订阅 - 内容搜索
    Copyright © 2008-2015 计算机技术学习交流网. 版权所有

    豫ICP备11007008号-1