定义
单例模式顾名思义只有一个实例,而且必须是自己创建自己的唯一实例,然后将实例通过其他接口提供给其他对象。
特点
- 单例类只有一个实例对象
- 单例对象只能自行创建,不能被其他类实例化
- 单例类对外提供一个访问该单例的全局访问接口
单例类怎么做才能使其他类实例化时编译报错:
构造函数私有化
优缺点
单例模式的优点:
- 保证只有一个实力,减少内存的开销
- 避免对资源的重复占用
- 共享资源的维护方便
单例模式的缺点:
- 单例模式容易将不同职责的方法封装在一起,违背了单一职责原则
- 单例模式不易扩展
实现
单例模式根据实例创建的时机分为:懒汉式单例和饿汉式单例。
懒汉式单例
懒汉式单例顾名思义就是你不用,我就不实例化,你要用了我才实例化。
class LazySingleton {
private:
LazySingleton() {}
LazySingleton(const LazySingleton&) = delete;
LazySingleton& operator=(const LazySingleton&) = delete;
~LazySingleton() {}
public:
static LazySingleton* getInstance() {
if (!m_pInstance_)
m_pInstance_ = new LazySingleton();
return m_pInstance_;
}
private:
static LazySingleton* m_pInstance_;
};
LazySingleton* LazySingleton::m_pInstance_ = nullptr;
饿汉式单例
饿汉式单例顾名思义就是不管你用不用,开始运行时我就实例化了。
class HungrySingleton {
private:
HungrySingleton() {}
HungrySingleton(const HungrySingleton&) = delete;
HungrySingleton& operator=(const HungrySingleton&) = delete;
~HungrySingleton() {}
public:
static HungrySingleton* getInstance() {
return &m_Instance_;
}
private:
static HungrySingleton m_Instance_;
};
HungrySingleton HungrySingleton::m_Instance_;
单例模式的释放
方法一:
自定义释放接口,手动调用。代码如下:
class LazySingleton {
private:
LazySingleton() {}
LazySingleton(const LazySingleton&) = delete;
LazySingleton& operator=(const LazySingleton&) = delete;
~LazySingleton() {}
public:
static LazySingleton* getInstance() {
if (!m_pInstance_)
m_pInstance_ = new LazySingleton();
return m_pInstance_;
}
// 需要手动调用释放
void destroy() {
if (m_pInstance_) {
delete m_pInstance_;
m_pInstance_ = nullptr;
}
}
private:
static LazySingleton* m_pInstance_;
};
这种方法的缺点是当你在不适宜的地方调用释放后,容易导致程序崩溃。
方法二:
class LazySingleton {
private:
LazySingleton() {}
LazySingleton(const LazySingleton&) = delete;
LazySingleton& operator=(const LazySingleton&) = delete;
~LazySingleton() {}
public:
static LazySingleton* getInstance() {
if (!m_pInstance_)
m_pInstance_ = new LazySingleton();
return m_pInstance_;
}
// 垃圾回收类
class GC
{
private:
static GC m_GC_;
public:
~GC()
{
if (m_pInstance_) {
delete m_pInstance_;
m_pInstance_ = nullptr;
}
}
};
private:
static LazySingleton* m_pInstance_;
};
LazySingleton* LazySingleton::m_pInstance_ = nullptr;
LazySingleton::GC LazySingleton::GC::m_GC_;
因为申明了全局对象,该对象会在程序退出前自动析构,从而实现单例的释放。