C++设计模式——单例模式
参考这两个文章
https://blog.csdn.net/Acquisition0818/article/details/118079459
https://blog.csdn.net/cjbct/article/details/79266057
单例模式饿汉式.cpp
//饿汉式 即类产生的时候就创建好实例化对象,这是一种空间换时间的方式 #include<iostream> using namespace std; class Singleton { private: Singleton() { cout << "单例对象创建!" << endl; }; Singleton(const Singleton&); Singleton& operator=(const Singleton&); ~Singleton() { cout << "单例对象销毁!" << endl; }; static Singleton myInstance; // 单例对象在这里! public: static Singleton* getInstance() { return &myInstance; } }; Singleton Singleton::myInstance; int main() { Singleton* ct1 = Singleton::getInstance(); Singleton* ct2 = Singleton::getInstance(); Singleton* ct3 = Singleton::getInstance(); return 0; } //输出 //单例对象创建! //单例对象销毁!
单例模式饿汉式内存泄漏手动释放版本版.cpp
#include<iostream> using namespace std; class Singleton { private: Singleton() { cout << "单例对象创建!" << endl; }; Singleton(const Singleton&); Singleton& operator=(const Singleton&); ~Singleton() { cout << "单例对象销毁!" << endl; }; static Singleton* myInstance; // 这里改了! public: static Singleton* getInstance() { return myInstance; // 这里也改了! } static void releaseInstance() { delete myInstance; } }; Singleton* Singleton::myInstance = new Singleton(); int main() { Singleton* ct1 = Singleton::getInstance(); Singleton* ct2 = Singleton::getInstance(); Singleton* ct3 = Singleton::getInstance(); Singleton::releaseInstance();//手动释放 return 0; } //输出 //单例对象创建! //单例对象销毁!
单例模式饿汉式GC机制.cpp
#include<iostream> using namespace std; class Singleton { private: Singleton() { cout << "单例对象创建!" << endl; }; Singleton(const Singleton&); Singleton& operator=(const Singleton&); ~Singleton() { cout << "单例对象销毁!" << endl; }; static Singleton* myInstance; public: static Singleton* getInstance() { return myInstance; } private: // 定义一个内部类 class GC { public: GC() {}; ~GC() { if (nullptr != myInstance) { delete myInstance; myInstance = nullptr; } } }; // 定义一个内部类的静态对象 // 当该对象销毁时,顺带就释放myInstance指向的堆区资源 static GC m_garbo; }; Singleton* Singleton::myInstance = new Singleton(); Singleton::GC Singleton::m_garbo; // 注意这里!! int main() { Singleton* ct1 = Singleton::getInstance(); Singleton* ct2 = Singleton::getInstance(); Singleton* ct3 = Singleton::getInstance(); return 0; } //输出 //单例对象创建! //单例对象销毁!
单例模式饿汉式智能指针.cpp
#include<iostream> #include<memory> using namespace std; class Singleton { private: Singleton() { cout << "单例对象创建!" << endl; }; Singleton(const Singleton&); Singleton& operator=(const Singleton&); ~Singleton() { cout << "单例对象销毁!" << endl; }; static void Destory(Singleton*) { cout << "在这里销毁单例对象!" << endl; };//注意这里 static shared_ptr<Singleton> myInstance; public: static shared_ptr<Singleton> getInstance() { return myInstance; } }; shared_ptr<Singleton> Singleton::myInstance(new Singleton(), Singleton::Destory); int main() { shared_ptr<Singleton> ct1 = Singleton::getInstance(); shared_ptr<Singleton> ct2 = Singleton::getInstance(); shared_ptr<Singleton> ct3 = Singleton::getInstance(); return 0; } //输出 //单例对象创建! //在这里销毁单例对象!
单例模式懒汉式.cpp
//饿汉式 即在需要的时候,才创建对象,这是一种时间换空间的方式 #include<iostream> using namespace std; class Singleton { private: Singleton() { cout << "单例对象创建!" << endl; }; Singleton(const Singleton&); Singleton& operator=(const Singleton&); ~Singleton() { cout << "单例对象销毁!" << endl; }; public: static Singleton* getInstance() { static Singleton myInstance; return &myInstance; } }; int main() { Singleton* ct1 = Singleton::getInstance(); Singleton* ct2 = Singleton::getInstance(); Singleton* ct3 = Singleton::getInstance(); return 0; } //输出 //单例对象创建! //单例对象销毁!
单例模式懒汉式GC机制.cpp
#include<iostream> #include<memory> using namespace std; class Singleton { private: Singleton() { cout << "单例对象创建!" << endl; }; Singleton(const Singleton&); Singleton& operator=(const Singleton&); ~Singleton() { cout << "单例对象销毁!" << endl; }; static Singleton* myInstance; public: static Singleton* getInstance() { if (nullptr == myInstance) { myInstance = new Singleton(); } return myInstance; } private: // 定义一个内部类 class GC { public: GC() {}; ~GC() { if (nullptr != myInstance) { delete myInstance; myInstance = nullptr; } } }; // 定义一个内部类的静态对象 // 当该对象销毁时,顺带就释放myInstance指向的堆区资源 static GC m_garbo; }; Singleton* Singleton::myInstance = nullptr; Singleton::GC Singleton::m_garbo; int main() { Singleton* ct1 = Singleton::getInstance(); Singleton* ct2 = Singleton::getInstance(); Singleton* ct3 = Singleton::getInstance(); return 0; } //输出 //单例对象创建! //单例对象销毁!
单例模式懒汉式GC机制多线程.cpp
#include <iostream> using namespace std; #include <mutex> class Singleton { private: Singleton() { cout << "单例对象创建!" << endl; } //构造函数 static Singleton* m_pSingleton; static mutex m_mutex; //锁 public: static Singleton* GetInstance() { if (m_pSingleton == nullptr) { std::lock_guard<std::mutex> lock(m_mutex); //加锁 if (m_pSingleton == nullptr) { m_pSingleton = new Singleton(); } } return m_pSingleton; } private: //GC机制 class GC { public: GC() {} ~GC() { //可以在这里销毁所有的资源,例如:db连接、文件句柄等 if (m_pSingleton != nullptr) { cout << "在这里销毁单例对象" << endl; delete m_pSingleton; m_pSingleton = nullptr; } } }; static GC gc; }; Singleton* Singleton::m_pSingleton = nullptr; Singleton::GC Singleton::gc; mutex Singleton::m_mutex; //注意这里一定要写 int main() { Singleton* st1 = Singleton::GetInstance(); Singleton* st2 = Singleton::GetInstance(); Singleton* st3 = Singleton::GetInstance(); return 0; } //输出 //单例对象创建! //在这里销毁单例对象