class Singleton { public: ~Singleton(); static Singleton* instance() { if (pInstance == nullptr) { pInstance = new Singleton(); } return pInstance; } protected: Singleton* pInstance; Singleton(){cout<<"Singleton"<<endl;} ~Singleton() { delete pInstance; pInstance = NULL; } }; //Obj-c 單例設計模式 #define DECLARE_SINGLETON(cls_name, method_name)\ + (cls_name*)method_name; #define IMPLEMENT_SINGLETON(cls_name, method_name)\ + (cls_name *)method_name {\ static cls_name *method_name;\ static dispatch_once_t onceToken;\ dispatch_once(&onceToken, ^{\ method_name = [[cls_name alloc] init];\ });\ return method_name;\ }