单例模式 模板类和非模板类实现

来源:互联网 发布:dhcp server 软件 编辑:程序博客网 时间:2024/05/16 18:12

概述:

        singleton模式称作单件模式或单例模式。

    它的作用是确保一个类在整个工程中有且只有一个实例。可以在多个不同的类中很方便的所需要的类的方法。

原理:

它的构造函数是私有的,你不能去new 它。该单例类里面已经实例化好了一个了,并且是static的,并提供一个获取该实例的方法!客户端只能通过该方法获取这个已经实例化好了的,这样就保证了只有一个实例存在!

Singleton模式其实是对全局静态变量的一个取代策略,Singleton模式的两个作用在C++中是通过如下的机制实现的:1)仅有一个实例,提供一个类的静态成员变量,大家知道类的静态成员变量对于一个类的所有对象而言是惟一的 2)提供一个访问它的全局访问点,也就是提供对应的访问这个静态成员变量的静态成员函数,对类的所有对象而言也是惟一的。在C++中,可以直接使用类域进行访问而不必初始化一个类的对象

Singleton.h

class Singleton

{

public:

Singleton(){};

~Singleton(){};

// 静态成员函数,提供全局访问的接口

static Singleton* GetInstancePtr();

static Singleton GetInstance();

void Test();

private:

// 静态成员变量,提供全局惟一的一个实例

static Singleton* m_pStatic;

};

Singleton.cpp

// 类的静态成员变量要在类体外进行定义

Singleton* Singleton::m_pStatic = NULL;

Singleton* Singleton::GetInstancePtr()

{

if (NULL == m_pStatic)

{

m_pStatic = new Singleton();

}

return m_pStatic;

}

Singleton Singleton::GetInstance()

{

return *GetInstancePtr();

}

void Singleton::Test()

{

std::cout << "Test!\n";

}

Main.cpp

#include "Singleton.h"

#include <stdlib.h>

int main()

{

// 不用初始化类对象就可以访问了

Singleton::GetInstancePtr()->Test();

Singleton::GetInstance().Test();

system("pause");

return 0;

}

模板类

1. Singleton.h文件

[cpp] view plaincopy

1.  /* 

2.   * Singleton.h 

3.   */  

4.    

5.  #ifndef SINGLETON_H_  

6.  #define SINGLETON_H_  

7.    

8.  #include <assert.h>  

9.  namespace GSingleton  

10. {  

11. template <typename T> class Singleton  

12. {  

13. protected:  

14.     static T* mSingleton;  

15. private:  

16.     Singleton()  

17.     {  

18.         assert(!mSingleton);  

19.         mSingleton=static_cast< T*>(this);  

20.                 cout<<"create Singleton()"<<endl;//看看什么时候调用构造函数的  

21.     }  

22.     ~Singleton()  

23.     {  

24.         assert(mSingleton);  

25.         delete mSingleton;  

26.         mSingleton=0;  

27.     }  

28. public:  

29.     static T* getSingletonPtr()  

30.     {   

31.         return ((!mSingleton)?(mSingleton=new T):mSingleton);  

32.     }  

33.     static T& getSingleton()  

34.     {   

35.         return ((!mSingleton)?(*(mSingleton=new T)):(*mSingleton));  

36.     }  

37. };  

38. }  

39. #endif /* SINGLETON_H_ */  




2. 类SubClass继承并实例化模板类Singleton

[cpp] view plaincopy

1.  /* 

2.   * SubClass.h 

3.   */  

4.  #ifndef SUBCLASS_H_  

5.  #define SUBCLASS_H_  

6.  #include "Singleton.h"  

7.  #include <iostream>  

8.  using namespace std;  

9.  class SubClass:public GSingleton::Singleton<SubClass>  

10. {  

11. public:  

12.     SubClass();  

13.     virtual ~SubClass();  

14.     inline void Show()  

15.     {  

16.         static int s=0;  

17.         s++;  

18.         cout<<"s===="<<s<<endl;  

19.     }  

20. };  

21. #endif /* SUBCLASS_H_ */  

22.   

23.   

24.   

25. /* 

26.  * SubClass.cpp 

27.  */  

28. #include "SubClass.h"  

29. template<> SubClass* GSingleton::Singleton<SubClass>::mSingleton=0;  

30. SubClass::SubClass()  

31. {  

32. }  

33. SubClass::~SubClass()  

34. {  

35. }  



3. 使用函数SubClass::Show()

#include "SubClass.h"

SubClass::getSingletonPtr()->Show();

 

0 0