设计模式-单例模式

本文深入探讨了单例模式的设计理念,包括其应用场景、实现方式,以及在多线程环境下的安全实现策略。提供了懒汉式、饿汉式及多线程安全的代码示例,帮助读者理解并掌握单例模式在不同场景下的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

设计模式通常可以分为三类:创建型模式、行为型模式、结构型模式

创建型模式

单例模式

应用场景:

在多线程之间共享一个对象;

在程序空间上分配全局变量;

考虑性能,需考虑对象的创建事件;

实现方式:

  1. 构造函数私有化
  2. 提供一个全局的静态方法
  3. 在类中定义一个静态指针,指向类的变量的静态变量指针

根据初始化方式可分为懒汉式和饿汉式单例模式

懒汉式代码实现:

#include<iostream>

using namespace std;

class Singleton
{
private:
	Singleton()
	{
		cout << "执行构造函数" << endl;
	}
public:
	static Singleton* GetInstance()
	{
		if (mSingleton == nullptr)
		{
			mSingleton = new Singleton;
		}
		return mSingleton;
	}

	static void FreeInstance()
	{
		if (mSingleton != nullptr)
		{
			delete mSingleton;
			mSingleton = nullptr;
		}
	}

private:
	static Singleton* mSingleton;
};

Singleton* Singleton::mSingleton = nullptr;

int main()
{
	Singleton *m1 = Singleton::GetInstance();
	Singleton *m2 = Singleton::GetInstance();
	m1->FreeInstance();
	m2->FreeInstance();
	return system("pause");
}

饿汉模式代码实现:

#include<iostream>

using namespace std;
class Singleton
{
private:
	Singleton()
	{
		cout << "执行构造函数" << endl;
	}
public:
	static Singleton* GetInstance()
	{
		return msSingleton;
	}
	static void  FreeSingleton()
	{
		if (msSingleton != nullptr)
		{
			delete msSingleton;
			msSingleton = nullptr;
		}
	}
private:
	static Singleton* msSingleton;
};

Singleton* Singleton::msSingleton = new Singleton;

int main()
{
	Singleton *m1 = Singleton::GetInstance();
	Singleton *m2 = Singleton::GetInstance();
	m1->FreeSingleton();
	m2->FreeSingleton();
	return system("pause");

}

多线程下懒汉式安全实现:

#include<iostream>
#include<Windows.h>
#include<process.h>
using namespace std;

CRITICAL_SECTION gSection;

class Singleton
{
private:
	Singleton()
	{
		cout << "执行构造函数" << endl;
		Sleep(20);
		cout << "执行构造函数" << endl;
	}
public:
	static Singleton* GetInstance()
	{
		if (mSingleton == nullptr)
		{
			
			EnterCriticalSection(&gSection);
			if (mSingleton == nullptr)
			{
				mSingleton = new Singleton;
				++Singleton::nCount;
				cout << "Singleton::nCount" << Singleton::nCount << endl;
			}
			LeaveCriticalSection(&gSection);
		}
		
		return mSingleton;
	}

	static void FreeInstance()
	{
		if (mSingleton != nullptr)
		{
			if (mSingleton != nullptr)
			{
				delete mSingleton;
				mSingleton = nullptr;
			}
		}
	}

private:
	
	static Singleton* mSingleton;
	static int nCount;
};
int Singleton::nCount = 0;
Singleton* Singleton::mSingleton = nullptr;
unsigned __stdcall StartAddress(int* i)
{
	Singleton *m1 = Singleton::GetInstance();
	Singleton *m2 = Singleton::GetInstance();
	cout << "我是线程" << *i << endl;
	return 0;
}
HANDLE hThread[200]{};
int main()
{
	InitializeCriticalSection(&gSection);
	for (int i = 0; i < 200; i++)
	{
		hThread[i] = (HANDLE)_beginthreadex(nullptr, 0, (_beginthreadex_proc_type)StartAddress, (void*)(&i), false, nullptr);
	}
	
	for (int i = 0; i < 200; ++i)
	{
		WaitForSingleObject(hThread[i], INFINITE);
	}
	for (int i = 0; i < 200; ++i)
	{
		CloseHandle(hThread[i]);
	}
	cout << "子线程运行结束" << endl;
	Singleton *m1 = Singleton::GetInstance();
	Singleton *m2 = Singleton::GetInstance();
	m1->FreeInstance();
	m2->FreeInstance();
	DeleteCriticalSection(&gSection);
	return system("pause");
}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值