1.
#ifndef __INFRAY_SINGLETON_H__ #define __INFRAY_SINGLETON_H__ #include <unistd.h> #define PATTERN_SINGLETON_DECLAREY(classname) \ private: \ classname(); \ public: \ static classname * Instance(); \ virtual ~classname(); #define PATTERN_SINGLETON_IMPLEMENTY(classname) \ classname * classname::Instance() \ { \ static classname * _instance = NULL; \ if(NULL == _instance) \ { \ _instance = new classname; \ usleep(10000); \ } \ return _instance; \ } #endif
使用:
在头文件的类中声明
并在C文件中实现