zoukankan      html  css  js  c++  java
  • 第二十一章-单例模式

    单例模式(Singleton): 保证一个类仅有一个实例,并提供一个访问它的全局访问点。
    通常我们可以让一个全局变量使得一个对象被访问,但它不能防止你实例化多个对象。一个最好的办法就是,让类自身负责保存它的唯一实例。这个类可以保证没有其他实例可以被创建,并且它可以提供一个访问该实例的办法。

    图片

    基本代码

    懒汉模式

    #include<iostream>
    #include<string>
    #include<vector>
    
    using namespace std;
    
    class Singleton
    {
    private:
    	static Singleton* instance;
    
    	Singleton() {}
    
    public:
    	static Singleton* GetInstance()
    	{
    		if (instance == nullptr)
    		{
    			instance = new Singleton();
    		}
    
    		return instance;
    	}
    };
    Singleton* Singleton::instance = nullptr;
    
    
    
    int main()
    {
    	Singleton* s1 = Singleton::GetInstance();
    	Singleton* s2 = Singleton::GetInstance();
    
    	if (s1 == s2)
    	{
    		cout << "两个对象是相同的" << endl;
    	}
    
    
    	system("pause");
    	return 0;
    }
    

    多线程双重锁定版本

    #include<iostream>
    #include<string>
    #include<vector>
    #include<thread>
    #include<mutex>
    
    using namespace std;
    
    #include<iostream>
    #include<string>
    #include<vector>
    
    using namespace std;
    
    class Lock
    {
    private:
    	mutex mut;
    public:
    	void lock() { mut.lock(); }
    	void unlock() { mut.unlock(); }
    };
    class Singleton
    {
    private:
    	
    	static Singleton* instance;
    	static Lock* locker;
    
    	Singleton() {}
    
    public:
    	static Singleton* GetInstance()
    	{
    		if (instance == nullptr)
    		{
    			locker->lock();
    			if(instance == nullptr)
    				instance = new Singleton();
    			locker->unlock();
    		}
    
    		return instance;
    	}
    };
    Singleton* Singleton::instance = nullptr;
    Lock* Singleton::locker = new Lock();
    
    
    
    int main()
    {
    	Singleton* s1 = Singleton::GetInstance();
    	Singleton* s2 = Singleton::GetInstance();
    
    	if (s1 == s2)
    	{
    		cout << "两个对象是相同的" << endl;
    	}
    
    
    	system("pause");
    	return 0;
    }
    

    饿汉模式

    饿汉模式与懒汉模式相反,是程序一开始就生成唯一实例。这样就不用检查是否存在实例,而且也无需考虑产生实例时的线程安全。

    #include <iostream>
    
    using namespace std;
    
    class Singleton {
    public:
    	~Singleton();
    	//提供单例对象访问
    	static Singleton* getInstance();
    	void doSomething();
    protected:
    	//构造函数声明为 保护方法
    	Singleton();
    	//单例对象指针
    	static Singleton* theSingleton;
    };
    
    //提供单例类对象访问
    Singleton* Singleton::getInstance() {
    	return theSingleton;
    }
    
    void Singleton::doSomething()
    {
    	cout << "饿懒汉模式" << "
    " << "单例模式" << endl;
    }
    
    Singleton::Singleton()
    {
    }
    Singleton::~Singleton()
    {}
    
    Singleton* Singleton::theSingleton = new Singleton();
    
    int main()
    {
    	Singleton* s1 = Singleton::getInstance();
    	Singleton* s2 = Singleton::getInstance();
    
    	if (s1 == s2)
    	{
    		cout << "两个对象是相同的" << endl;
    	}
    
    	system("pause");
    	return 0;
    }
    
    

    Meyers Singleton(目前最推荐的C++单例写法)

    #include <iostream>
    
    using namespace std;
    
    class Singleton
    {
    public:
    	static Singleton& Instance()
    	{
    		static Singleton theSingleton;
    		return theSingleton;
    	}
    	void doSomeThong();
    private:
    	Singleton();
    	~Singleton();
    };
    
    Singleton::Singleton()
    {
    }
    
    Singleton::~Singleton()
    {
    }
    void Singleton::doSomeThong()
    {
    	cout << "单例类" << endl;
    	cout << "C++最推荐的单例类写法" << endl;
    }
    
    int main()
    {
    	Singleton::Instance().doSomeThong();
    
    	system("pause");
    	return 0;
    }
    
  • 相关阅读:
    人民币大小写转换
    window.parent与window.openner区别
    jquery.honver(),toggle()
    ADO.NET Entity Framework 之“无法加载指定的元数据资源。”
    window.external 的用法(收藏夹功能)
    空链接的技巧
    IIS自动开通组件编程
    在Asp.net中操作IIS的虚拟目录(C#)
    最佳 .NET 网站推荐
    边框 top、clientTop、scrollTop、offsetTop
  • 原文地址:https://www.cnblogs.com/wfcg165/p/12040567.html
Copyright © 2011-2022 走看看