C++ 单例模式 代码详解

本文介绍了单例模式的概念,强调其在创建唯一对象时的重要性,并通过两个C++代码示例展示了单例模式的实现,包括静态成员变量和RAII技术的应用,确保了对象的唯一性和内存管理。在服务器程序中,如日志模块,单例模式尤为适用。
摘要由CSDN通过智能技术生成

单例模式

单例模式(Singleton Pattern)是 最简单的设计模式之一。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。

这种模式涉及到一个单一的类,该类负责创建自己的对象,同时确保只有单个对象被创建。这个类提供了一种访问其唯一的对象的方式,可以直接访问,不需要实例化该类的对象。

有以下注意的点:

1、单例类只能有一个实例。

2、单例类必须自己创建自己的唯一实例。

3、单例类必须给所有其他对象提供这一实例。

比如我们在写服务器程序时,日志对象实例就应该是单例模式,下面介绍两种实现手法。

代码实现一

#include <iostream>
#include<ctime>
#include<chrono>
#include<iomanip>
using namespace std;

class Log
{
private:
	Log(int n = 0) :id(n)  //私有构造函数,单例模式只能有一个实例
	{

	};
	int id;
public:
	static Log& Instance()
	{
		static Log slog(1);    //第一次Instance()函数调用会构造静态实例slog,后面每次调用会跳过这句,直接返回slog实例
		return slog;
	}
	void Info()
	{
		//打印日期
		auto t = std::chrono::system_clock::now();
		auto tNow = std::chrono::system_clock::to_time_t(t);
		//std::tm* now = localtime(&tNow);     vs2019会报警告
		tm* now = new tm;
		localtime_s(now, &tNow);
		printf("log_id=<%d>\n", id);
		//日期打印1
		printf("[%d-%d-%d %d:%d:%d]\n", now->tm_year + 1900, now->tm_mon + 1, now->tm_mday
			, now->tm_hour, now->tm_min, now->tm_sec);
		//日期打印2
		char time[80] = { 0 };
		strftime(time, 80, "%Y-%m-%d %H:%M:%S", now);
		cout << time << endl;
		//日期打印3
		std::cout << std::put_time(now, "%Y-%m-%d %H:%M:%S") << endl;
	}
};

int main()
{
	Log* slog1 = &Log::Instance();
	Log* slog2 = &Log::Instance();
	//以下三条都是调用一个实例对象来打印日期
	slog1->Info();
	slog2->Info();
	Log::Instance().Info();

}


测试结果:

在这里插入图片描述

代码实现二

#include <iostream>
#include<ctime>
#include<chrono>
#include<iomanip>
using namespace std;

class Log
{
private:
	Log(int n = 0) :id(n)  //私有构造函数,单例模式只能有一个实例
	{

	};
	int id;
public:
	static Log* Instance()
	{
		static Log* slog = new Log(1);    //第一次Instance()函数调用会构造静态实例slog,后面每次调用会跳过这句,直接返回slog指针
		static RAII_Log sRaii;     //静态对象来保证程序结束时new出来的Log能被delete掉
		return slog;
	}

	class RAII_Log
	{
	public:
		~RAII_Log()    //程序结束时调用
		{
			if (Log::Instance())
			{
				delete Log::Instance();
				cout << "delete Log::Instance();" << endl;
			}
		}

	};
	void Info()
	{
		//打印日期
		auto t = std::chrono::system_clock::now();
		auto tNow = std::chrono::system_clock::to_time_t(t);
		//std::tm* now = localtime(&tNow);     vs2019会报警告
		tm* now = new tm;
		localtime_s(now, &tNow);
		printf("log_id=<%d>\n", id);
		//日期打印1
		printf("[%d-%d-%d %d:%d:%d]\n", now->tm_year + 1900, now->tm_mon + 1, now->tm_mday
			, now->tm_hour, now->tm_min, now->tm_sec);
		//日期打印2
		char time[80] = { 0 };
		strftime(time, 80, "%Y-%m-%d %H:%M:%S", now);
		cout << time << endl;
		//日期打印3
		std::cout << std::put_time(now, "%Y-%m-%d %H:%M:%S") << endl;
	}
};

int main()
{
	Log* slog1 = Log::Instance();
	Log* slog2 = Log::Instance();
	//以下三条都是调用一个实例对象来打印日期
	slog1->Info();
	slog2->Info();
	Log::Instance()->Info();

}

测试结果如下:
在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值