单例模式就是一个类只有一个实例。
为了保证一个类只有一个实例。为了保证一个类只有一个实例,要保证一开始这个类只有唯一一个实例,并且不能进行拷贝等赋值和移动操作。
因此有两种实现方式,第一个是懒汉实现,在需要的时候实例化,另一种是饿汉实现,一开始就实例化。
//懒汉实现 /* **注意C++11以后加入了移动操作,也有了新语法=delete, **但是为了格式整齐,统一让他们权限为private */ #ifndef __SINGLETON1__ #define __SINGLETON1__ class Singleton1 { public: static Singleton1* getInstance(); ~Singleton1() { if(instance) delete instance; } private: Singleton1(){} Singleton1(const Singleton1 &&); Singleton1(const Singleton1 & ); Singleton1& operator=(const Singleton1 &&); Singleton1& operator=(const Singleton1 & ); private: static Singleton1 *instance; }; Singleton1* Singleton1::instance = nullptr; Singleton1* Singleton1::getInstance() { if(instance == nullptr) instance = new Singleton1(); return instance; } #endif // __SINGLETON1__ /*---------------------------------------------*/
//饿汉实现 #ifndef __SINGELTON2__ #define __SINGELTON2__ class Singleton2 { public: static Singleton2* getInstance(); ~Singleton2() { if(instance) delete instance; } private: Singleton2() {} Singleton2(const Singleton2 &&); Singleton2(const Singleton2 & ); Singleton2& operator=(const Singleton2 &&); Singleton2& operator=(const Singleton2 & ); private: static Singleton2* instance; }; Singleton2* Singleton2::instance = new Singleton2(); Singleton2* Singleton2::getInstance(){ return instance; } #endif // !__SINGELTON2__
实例化时如下:
int main() { Singleton1::getInstance();
Singleton2::getInstance(); return 0; }
以上懒汉代码只适合在单线程,懒汉实现在多线程是不安全的,因为有可能两个线程同时运行到实例化初,所以需要加锁,饿汉一直就是线程安全,因为一开始初始化只会保证有一个实例。多线程代码如下:
#ifndef __SINGLETOM_THREAD1__ #define __SINGLETOM_THREAD1__ #include <thread> #include <mutex> //加锁 class Singleton3 { public: static Singleton3* getInstance(); ~Singleton3() { if(instance) delete instance; } private: Singleton3() {}; Singleton3(const Singleton3 &&); Singleton3(const Singleton3 & ); Singleton3 &operator=(const Singleton3 &&); Singleton3 &operator=(const Singleton3 & ); private: static std::mutex mutex_s; static Singleton3* instance; }; std::mutex Singleton3::mutex_s; Singleton3* Singleton3::instance = nullptr; Singleton3* Singleton3::getInstance() { if (instance == nullptr) { mutex_s.lock(); if (instance == nullptr) instance = new Singleton3(); mutex_s.unlock(); } return instance; } #endif // !__SINGLETOM_THREAD1__