分类: C/C++
2013-02-20 22:35:22
class Singleton { public: static Singleton* Instance() ; protected: Singleton() {} private: static Singleton *_instance ; Singleton(const Singleton&) ; Singleton& operator=(const Singleton&) ; } ; Singleton* Singleton::_instance = NULL ; Singleton* Singleton::Instance() { (_instance == NULL) ? _instance = new Singleton() : 0 ; //lazy initialization return _instance ; }
#define PATTERN_SINGLETON_DECLARE(classname) private: \ classname(); \ public: \ static classname * instance(); \ virtual ~classname(); #define PATTERN_SINGLETON_IMPLEMENT(classname) \ classname * classname::instance() \ { \ static classname * _instance = NULL; \ if( NULL == _instance) \ { \ _instance = new classname; \ } \ return _instance; \ } class Singleton { PATTERN_SINGLETON_DECLARE(Singleton); private: Singleton(const Singleton&) ; Singleton& operator=(const Singleton&) ; } ; PATTERN_SINGLETON_IMPLEMENT(Singleton);