第六章 单例模式

第六章 单例模式,第1张

第六章 单例模式 1. 单例模式核心概念
  • 要求
    对象实例化只允许一次
  • 条件
    构造函数、拷贝构造函数、赋值拷贝拷贝设置为private
2. 单例模式实现 2.1 懒汉模式 代码
#include 

#include          // std::thread
#include           // 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 lock(m_Mutex);
        if (m_SingleInstance == nullptr)
        {
            m_SingleInstance = new (std::nothrow) SingleInstance();
        }
        return m_SingleInstance;
}

void SingleInstance::deleteInstance()
{
    std::unique_lock 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 
#include 
#include          // std::thread
#include           // std::mutex
using namespace std;


class SingleInstance
{

public:
    static std::shared_ptr getInstance();
    static void deleteInstance();
    void Print();
    ~SingleInstance();
private:

    SingleInstance();

    SingleInstance(const SingleInstance &signal);
    const SingleInstance &operator=(const SingleInstance &signal);

private:
    static std::shared_ptr m_SingleInstance;
    static std::mutex m_Mutex;
};

SingleInstance::SingleInstance(){cout << "SingleInstance" << endl;}
SingleInstance::~SingleInstance(){cout << "~SingleInstance" << endl;}

std::shared_ptr SingleInstance::getInstance()
{
        std::unique_lock lock(m_Mutex);
        if (m_SingleInstance == nullptr)
        {
            auto temp = std::shared_ptr(new SingleInstance());
            m_SingleInstance = temp;
        }
        return m_SingleInstance;
}

void SingleInstance::Print()
{
    cout << "AAA" << endl;
}

int
main ()
{
   std::shared_ptr obj = SingleInstance::getInstance();
   obj->Print();

  return 0;
}

std::shared_ptr SingleInstance::m_SingleInstance = nullptr;

std::mutex SingleInstance::m_Mutex;

2.2 饿汉模式
#include 

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

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5690785.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-12-17
下一篇2022-12-17

发表评论

登录后才能评论

评论列表(0条)

    保存