C++进阶学习——单例模式的实现

单例模式也称为单件模式、单子模式,是使用最广泛的设计模式之一。其意图是保证一个类仅有一个实例,并提供一个访问它的全局访问点,该实例被所有程序模块共享。


单例模式通过类本身来管理其唯一实例,这种特性提供了解决问题的方法。唯一的实例是类的一个普通对象,但设计这个类时,让它只能创建一个实例并提供对此实例的全局访问。唯一实例类Singleton在静态成员函数中隐藏创建实例的操作。习惯上把这个成员函数叫做Instance(),它的返回值是唯一实例的指针。


示例代码如下:

[cpp]  view plain copy
  1. #include <iostream>  
  2. #include <string>  
  3. using namespace std;  
  4.   
  5. class Singleton  
  6. {  
  7. public:    
  8.     static Singleton *getInstance()    
  9.     {  
  10.         //判断是否第一次调用   
  11.         if(instance == NULL)  
  12.         {  
  13.             instance = new Singleton();  
  14.         }  
  15.           
  16.         return instance;    
  17.     }  
  18.       
  19.     void init(int num, string str)  
  20.     {  
  21.         a = num;  
  22.         buf = str;  
  23.     }  
  24.       
  25.     void display()  
  26.     {  
  27.         cout << "a = " << a << ", buf = " << buf << endl;   
  28.     }  
  29.       
  30.     //销毁实例,资源回收  
  31.     void destroy()  
  32.     {  
  33.         if(NULL != instance)  
  34.         {  
  35.             delete instance;  
  36.             instance = NULL;  
  37.         }  
  38.     }  
  39.       
  40. private:    
  41.     Singleton()//构造函数为私有    
  42.     {  
  43.             
  44.     }  
  45.       
  46.     ~Singleton()//析构函数为私有        
  47.     {  
  48.             
  49.     }  
  50.       
  51.     //静态数据成员,类中声明,类外必须定义  
  52.     static Singleton *instance;   
  53.       
  54.     int a;  
  55.     string buf;  
  56.       
  57. };  
  58.   
  59. //静态数据成员,类外定义  
  60. Singleton *Singleton::instance = NULL;  
  61.   
  62. int main()  
  63. {  
  64.     Singleton *p1 =  Singleton::getInstance();  
  65.     p1->init(250, "mike");  
  66.     p1->display();  
  67.       
  68.     cout << "\n=============\n";  
  69.     Singleton *p2 = Singleton::getInstance();  
  70.     p2->display();  
  71.       
  72.     p2->destroy(); //销毁实例  
  73.       
  74.     return 0;     
  75. }  


用户访问唯一实例的方法只有getInstance()成员函数。如果不通过这个函数,任何创建实例的尝试都将失败,因为类的构造函数是私有的。getInstance()使用懒惰初始化,也就是说它的返回值是当这个函数首次被访问时被创建的。这是一种防弹设计——所有getInstance()之后的调用都返回相同实例的指针。


运行结果如下:



单例模式类Singleton有以下特征:

  • 它有一个指向唯一实例的静态指针instance,并且是私有的;
  • 它有一个公有的函数,可以获取这个唯一的实例,并且在需要的时候创建该实例;
  • 它的构造函数是私有的,这样就不能从别处创建该类的实例。
我们知道,程序在结束的时候,系统会自动析构所有的全局变量。事实上,系统也会析构所有的类的静态成员变量,就像这些静态成员也是全局变量一样。利用这个特征,我们可以在单例类中定义一个这样的静态成员变量,而它的唯一工作就是在析构函数中删除单例类的实例。如下面的代码中的Garbo类(Garbo意为垃圾工人):
[cpp]  view plain copy
  1. #include <iostream>  
  2. #include <string>  
  3. using namespace std;  
  4.   
  5. class Singleton  
  6. {  
  7. public:    
  8.     static Singleton *getInstance()    
  9.     {  
  10.         //判断是否第一次调用   
  11.         if(instance == NULL)  
  12.         {  
  13.             instance = new Singleton();  
  14.         }  
  15.           
  16.         return instance;    
  17.     }  
  18.       
  19.     void init(int num, string str)  
  20.     {  
  21.         a = num;  
  22.         buf = str;  
  23.     }  
  24.       
  25.     void display()  
  26.     {  
  27.         cout << "a = " << a << ", buf = " << buf << endl;   
  28.     }  
  29.       
  30. private:    
  31.     Singleton()//构造函数为私有    
  32.     {  
  33.             
  34.     }  
  35.       
  36.     ~Singleton()//析构函数为私有    
  37.     {  
  38.             
  39.     }  
  40.       
  41.     //静态数据成员,类中声明,类外必须定义  
  42.     static Singleton *instance;   
  43.       
  44.     int a;  
  45.     string buf;  
  46.       
  47.     //它的唯一工作就是在析构函数中删除Singleton的实例    
  48.     class Garbo   
  49.     {    
  50.     public:    
  51.         ~Garbo()    
  52.         {  
  53.           if(NULL != Singleton::instance){  
  54.             delete Singleton::instance;  
  55.             Singleton::instance = NULL;  
  56.             cout << "instance is detele\n";  
  57.           }   
  58.         }    
  59.     };  
  60.     //定义一个静态成员变量,程序结束时,系统会自动调用它的析构函数   
  61.     //static类的析构函数在main()退出后调用    
  62.     static Garbo temp; //静态数据成员,类中声明,类外定义  
  63.       
  64. };  
  65.   
  66. //静态数据成员,类外定义  
  67. Singleton *Singleton::instance = NULL;  
  68.       
  69. Singleton::Garbo Singleton::temp;   
  70.   
  71. int main()  
  72. {  
  73.       
  74.     Singleton *p1 =  Singleton::getInstance();  
  75.     p1->init(250, "mike");  
  76.     p1->display();  
  77.   
  78.     cout << "\n=============\n";  
  79.     Singleton *p2 = Singleton::getInstance();  
  80.     p2->display();  
  81.   
  82.     return 0;     
  83. }  

运行结果如下:


类Garbo被定义为Singleton的私有内嵌类,以防该类被在其他地方滥用。

程序运行结束时,系统会调用Singleton的静态成员Garbo的析构函数,该析构函数会删除单例的唯一实例。注意:static类的析构函数在main()退出后才会调用。

使用这种方法释放单例对象有以下特征:
  • 在单例类内部定义专有的嵌套类;
  • 在单例类内声明私有的专门用于释放的静态成员(需要在类的外部定义);
  • 利用程序在结束时(main()函数退出)析构全局变量的特性,选择最终的释放时机。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值