c++学习笔记(3):单例模式

单例模式简单来说就是一个类只能创建一个对象的设计模式。
设计步骤:
1.生成对象的接口必须屏蔽起来,包括构造函数以及拷贝构造函数。
2.生成唯一一个对象的接口,返回值为类*或者类&(保证了不会产生临时对象)
3.摆脱构造函数对对象的依赖,设置为静态的。
4.对象存放在堆上。(静态方法访问静态成员变量)
(1)想让一个类只能构建一个对象,则Signleton的构造方法只能为私有的。
(2)instance是Signleton类的静态成员,也是单例对象。
(3)getInstance是获取单例对象的方法。

//单例模式的代码实现,此时只适合于在单线程中使用
public class Singleton
{
	public:
 		static Singleton getInstance()
		{
			if(instance == NULL)
			{
				instance = new Singleton();
			}
			return instance;
		}
	private:
		Singleton();
	private:
		static Singleton instance = NULL;
}

分析上述代码:
如果Singleton类刚被初始化,instance对象还为空这时候如果同时去访问getInstance,因为instance是空的,所以两个线程同时通过了条件判断,开始执行new操作。
在这里插入图片描述
要想实现一个线程安全的单例模式:
(1)为了防止 new Singleton()执行多次,则需要在new之前加上锁。需要加上一个同步锁。
(2)进入临界区应再做一次判空,当两个线程同时访问的时候,线程a构建完成对象,线程b也已经通过了最初的判空验证,如果不做第二次判空,线程b将会依旧去创建新对象。

//================剑指offer面试题一==============
//题目:设计一个类,只能生成这个类的一个实例
# include <iostream>
class Singleton
{
	public:
 		static Singleton getInstance()
		{
			if(instance == NULL)
			{
				lock(syncObj);
				{
					if(instance == NULL)
					{
						instance = new Singleton();
					}
				}
			return instance;
		}
	private:
		Singleton();
	private:
		static Singleton instance = NULL;
}

在这里插入图片描述
利用静态内部类实现单例模式
利用外部无法访问静态内部类,只有当调用Singleton .getInstance时才可以得到单例对象。

public class Singleton
{
	public static class Nested{
	
 	public static Singleton getInstance()
	{
		return Nested.instance;
	}
	private:
		Singleton(){};
	private static final Siglenton instance;
	}
}

猜你喜欢

转载自blog.csdn.net/qq_42111463/article/details/84178128