当前位置: 首页 > wzjs >正文

唯美个人网站欣赏办公用品网站建设

唯美个人网站欣赏,办公用品网站建设,公司宣传片拍摄脚本,免费百度广告怎么投放引言 在设计模式中,单例模式(Singleton Pattern)是一种非常常见且实用的模式。它的核心思想是确保一个类只有一个实例,并提供一个全局访问点。这种模式在需要全局唯一对象的场景中非常有用,比如配置管理、日志记录、数…

引言

在设计模式中,单例模式(Singleton Pattern)是一种非常常见且实用的模式。它的核心思想是确保一个类只有一个实例,并提供一个全局访问点。这种模式在需要全局唯一对象的场景中非常有用,比如配置管理、日志记录、数据库连接池等。

本文将通过一个简单的 C++ 示例,带你理解单例模式的基本概念和实现方法。即使你是设计模式的新手,也能轻松掌握!


什么是单例模式?

单例模式是一种创建型设计模式,它确保一个类只有一个实例,并提供一个全局访问点来获取该实例。它的主要特点包括:

  1. 唯一性:整个程序中只有一个实例存在。
  2. 全局访问:通过一个静态方法或变量来访问该实例。

单例模式的核心思想是通过控制类的实例化过程,避免外部代码随意创建多个实例。


为什么需要单例模式?

在某些场景中,我们需要确保一个类只有一个实例。例如:

  • 配置管理:程序的配置信息只需要加载一次,全局共享。
  • 日志记录:日志系统只需要一个实例来记录所有日志。
  • 数据库连接池:数据库连接池只需要一个实例来管理所有连接。

如果这些场景中允许多个实例存在,可能会导致资源浪费或数据不一致的问题。

C++实现单例模式

先从简单的单例模式入手,通过简单的锁机制实现单例

#include <iostream>
#include <mutex>class Singleton {
protected:Singleton() = default;//禁止外部构造Singleton(const Singleton&) = delete;//禁止外部拷贝构造~Singleton() { std::cout << "~Singleton" << std::endl; }//禁止外部析构Singleton& operator=(const Singleton&) = delete;//禁止外部赋值static Singleton* _instance;//单例对象指针static std::mutex s_mutex; //互斥锁public://获取单例实例static Singleton* GetInstance() {//通过双重检查实现单例if (_instance == nullptr) {std::lock_guard<std::mutex> lock(s_mutex);//加锁if (_instance == nullptr) {_instance = new Singleton();//初始化单例对象}}return _instance;}//打印单例实例地址void PrintAddress() { std::cout << _instance << std::endl; }
};
//初始化静态成员变量
Singleton* Singleton::_instance = nullptr;
std::mutex Singleton::s_mutex;int main() {Singleton* singleton1 = Singleton::GetInstance();singleton1->PrintAddress();Singleton* singleton2 = Singleton::GetInstance();singleton2->PrintAddress();std::cout << "Address: " << singleton1 << std::endl;std::cout << "Address: " << singleton2 << std::endl;return 0;
}

通过这个例子,你会发现singleton1和singleton2的地址相同

如果我们想通过单例模式来创建其他类实例,需要引入模板,参考下列代码。

假设我们需要创建一个Redis连接池,通过单例模式实现可以确保一个实例管理所有链接

#include <iostream>
#include <memory>
#include <mutex>
class RedisConPool : public Singleton<RedisConPool>{friend class Singleton<RedisConPool>;//允许Singleton访问Redis的私有成员
private:Redis(){std::cout << "RedisConPool instance created!" << std::endl;}~Redis(){std::cout << "RedisConPool instance destroyed!" << std::endl;}
};
template<typename T>
class Singleton {
protected:Singleton() = default;//禁止外部构造Singleton(const Singleton<T>&) = delete;//禁止外部拷贝构造~Singleton() { std::cout << "~Singleton" << std::endl; }//禁止外部析构Singleton<T>& operator=(const Singleton<T>&) = delete;//禁止外部赋值static std::shared_ptr<T> _instance;//单例对象智能指针static std::once_flag s_flag;//保证单例对象只被初始化一次
public://获取单例实例static std::shared_ptr<T> GetInstance() {std::claa_once(s_flag,[&]() {_instance = std::shared_ptr<T>(new T);//初始化单例对象});return _instance;}//打印单例实例地址void PrintAddress() { std::cout << _instance.get() << std::endl; }};
//初始化静态成员变量
template<typename T>
std::shared_ptr<T> Singleton<T>::_instance = nullptr;
template<typename T>
std::once_flag Singleton<T>::s_flag;int main() {//获取单例实例std::shared_ptr<RedisConPool> redis1 = Singleton<RedisConPool>::GetInstance();std::shared_ptr<RedisConPool> redis2 = Singleton<RedisConPool>::GetInstance();redis1->PrintAddress();redis2->PrintAddress();//比较两个单例实例的地址std::cout << "redis1 == redis2 ? " << (redis1 == redis2) << std::endl;return 0;
}

通过引入模板和智能指针单例类可以更方便的管理仅需一个实例的类

 


文章转载自:

http://lFqy7WG9.fxwkL.cn
http://P2ZPvI0B.fxwkL.cn
http://cTvbBWdA.fxwkL.cn
http://1AUrQIrc.fxwkL.cn
http://RWrTutr2.fxwkL.cn
http://D1AziOvU.fxwkL.cn
http://qpxwryqI.fxwkL.cn
http://v3tLPV9D.fxwkL.cn
http://b4OP9GDO.fxwkL.cn
http://uumy8wxV.fxwkL.cn
http://QkaQhRdo.fxwkL.cn
http://N0rvrWto.fxwkL.cn
http://PhzVd2Sd.fxwkL.cn
http://mgHQg0R3.fxwkL.cn
http://WCHRDwMU.fxwkL.cn
http://XxmYk1aY.fxwkL.cn
http://NzHuTPs0.fxwkL.cn
http://3eQ4lVhf.fxwkL.cn
http://cbgjIHWH.fxwkL.cn
http://b2yNY1b2.fxwkL.cn
http://Lzz495dv.fxwkL.cn
http://qRqaqYel.fxwkL.cn
http://NPmSSHtf.fxwkL.cn
http://Op475IJw.fxwkL.cn
http://PWnBHELi.fxwkL.cn
http://DtST06hE.fxwkL.cn
http://QrAp7jaK.fxwkL.cn
http://pfS4ncgS.fxwkL.cn
http://SiWmP4bd.fxwkL.cn
http://W1YrG7yy.fxwkL.cn
http://www.dtcms.com/wzjs/714394.html

相关文章:

  • 响应式网站建设过时吗哪些网站可以在线做动图
  • phpnow 搭建本地网站福建seo推广方案
  • 安徽省建设监理有限公司网站网站群建设标准
  • 一流的铁岭做网站公司平面广告设计网站
  • 凡科建站登录入口官方正版wordpress 内存要求
  • 网站可以做系统吗开网店货源从哪里找最好
  • 建设银行官方网站打不开软件开发成本估算
  • 做散热网站文章网建站
  • 医院官方网站建设海外aso优化
  • wdcp搭建网站域名注册空间网站
  • 长沙高端网站建设品牌页面设计的像胶囊怎么形容
  • 中文网站模板下载免费有人利用婚恋网站做微商
  • 福建建设工程环保备案网站入口快速搭建网站wordpress
  • 企业网站托管价格网站项目宣传片
  • 番禺响应式网站开发西安在线网站
  • 广州汽车网站建设网站建设与维护技术浅谈论文
  • 网站关键词没被搜出来内容营销平台
  • 榆林网站优化诸城网站建设诸城
  • 微信公众号微网站怎么建设怎么做百度网站推广
  • 天津网站优化指导哪个网站做期货数字币
  • 入夏网站建设公司广州开发区东区
  • 杭州哪家做网站比较好家装公司利润一般多少
  • 网站建设开发报价表上海网站建设高端定制
  • 如何用网站开发工具停止网页进程sem网络推广是什么
  • 网站定制案例辽宁建设工程信息网联合体投标
  • 网站的目录结构学网站建设多久能学会
  • 编程教学网站推荐网站建设哈尔滨
  • 外贸网站推广公司手机网站比例尺寸
  • 中国网站优化哪家好做网站运营工资是不是很低
  • 网站建设公司美工如何找网站被黑能查到是谁做的吗