1、Singleton.h文件和其使用方法
#ifndef _SINGLETON_H
#define _SINGLETON_H
template <class T>
class Singleton
{
public:
static inline T* instance();
void release();
protected:
Singleton(void){}
~Singleton(void){}
static T* _instance;
};
template <class T>
inline T* Singleton<T>::instance()
{
if(!_instance)
_instance = new T;
return _instance;
}
template <class T>
void Singleton<T>::release()
{
if (!_instance)
return;
delete _instance;
_instance = 0;
}
//在cpp文件中需要先声明静态变量
#define DECLARE_SINGLETON_MEMBER(_Ty) \
template <> _Ty* Singleton<_Ty>::_instance = NULL;
#endif
//Singleton模板类在Setting类中的具体使用步骤
//1、添加Singleton.h引用
//2、继承该类如:,public Singleton<Settings>
//3、在cpp文件中需要先声明静态变量DECLARE_SINGLETON_MEMBER(Settings);
//4、在Singleton.h定义一个在其它类中调用该单例的宏(也可以声明其它变量):#define Instance_Setting Settings::instance()
//5、某个类中添加了Setting.h的引用,使用方法如下:
//Instance_Setting->onDownLoadApp();直接调用即可