1. 单例模式核心概念
- 要求
对象实例化只允许一次 - 条件
构造函数、拷贝构造函数、赋值拷贝拷贝设置为private
2. 单例模式实现
2.1 懒汉模式 代码
#include <iostream>
#include <thread> // std::thread
#include <mutex> // std::mutex
using namespace std;
class SingleInstance
{
public:
//
static SingleInstance* getInstance();
static void deleteInstance();
void Print();
private:
SingleInstance();
~SingleInstance();
SingleInstance(const SingleInstance &signal);
const SingleInstance &operator=(const SingleInstance &signal);
private:
static SingleInstance *m_SingleInstance;
static std::mutex m_Mutex;
};
SingleInstance::SingleInstance(){cout << "SingleInstance" << endl;}
SingleInstance::~SingleInstance(){cout << "~SingleInstance" << endl;}
SingleInstance* SingleInstance::getInstance()
{
std::unique_lock<std::mutex> lock(m_Mutex);
if (m_SingleInstance == nullptr)
{
m_SingleInstance = new (std::nothrow) SingleInstance();
}
return m_SingleInstance;
}
void SingleInstance::deleteInstance()
{
std::unique_lock<std::mutex> lock(m_Mutex);
if (m_SingleInstance)
{
delete m_SingleInstance;
m_SingleInstance = nullptr;
}
}
void SingleInstance::Print()
{
cout << "AAA" << endl;
}
int
main ()
{
SingleInstance* obj = SingleInstance::getInstance();
obj->Print();
obj->deleteInstance();
return 0;
}
SingleInstance * SingleInstance::m_SingleInstance = nullptr;
std::mutex SingleInstance::m_Mutex;
输出:
SingleInstance
AAA
~SingleInstance
2.2 懒汉模式 优化
- 智能指针自动完成析构
std::shared_ptr SingleInstance::m_SingleInstance = nullptr;
#include <iostream>
#include <memory>
#include <thread> // std::thread
#include <mutex> // std::mutex
using namespace std;
class SingleInstance
{
public:
static std::shared_ptr<SingleInstance> getInstance();
static void deleteInstance();
void Print();
~SingleInstance();
private:
SingleInstance();
SingleInstance(const SingleInstance &signal);
const SingleInstance &operator=(const SingleInstance &signal);
private:
static std::shared_ptr<SingleInstance> m_SingleInstance;
static std::mutex m_Mutex;
};
SingleInstance::SingleInstance(){cout << "SingleInstance" << endl;}
SingleInstance::~SingleInstance(){cout << "~SingleInstance" << endl;}
std::shared_ptr<SingleInstance> SingleInstance::getInstance()
{
std::unique_lock<std::mutex> lock(m_Mutex);
if (m_SingleInstance == nullptr)
{
auto temp = std::shared_ptr<SingleInstance>(new SingleInstance());
m_SingleInstance = temp;
}
return m_SingleInstance;
}
void SingleInstance::Print()
{
cout << "AAA" << endl;
}
int
main ()
{
std::shared_ptr<SingleInstance> obj = SingleInstance::getInstance();
obj->Print();
return 0;
}
std::shared_ptr<SingleInstance> SingleInstance::m_SingleInstance = nullptr;
std::mutex SingleInstance::m_Mutex;
2.2 饿汉模式
#include <iostream>
using namespace std;
class Singleton
{
public:
static Singleton* GetInstance()
{
return &m_instance;
}
private:
// 构造函数私有
Singleton(){cout << "Singleton" << endl;}
~Singleton(){cout << "~Singleton" << endl;}
Singleton(Singleton const&);
Singleton& operator=(Singleton const&);
static Singleton m_instance;
};
Singleton Singleton::m_instance; // 在程序入口之前就完成单例对象的初始化
int
main ()
{
cout << "main" << endl;
return 0;
}
输出:
Singleton
main
~Singleton

被折叠的 条评论
为什么被折叠?



