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

丰台新乡网站建设牛推网络

丰台新乡网站建设,牛推网络,做网站seo的公司,做ps找图的网站有哪些定义 单例设计模式(Singleton Pattern)是一种创建型设计模式,它确保一个类只有一个实例,并提供一个全局访问点来获取该实例。这种模式常用于需要控制对某些资源的访问的场景,例如数据库连接、日志记录等。 单例模式涉…

定义

单例设计模式(Singleton Pattern)是一种创建型设计模式,它确保一个类只有一个实例,并提供一个全局访问点来获取该实例。这种模式常用于需要控制对某些资源的访问的场景,例如数据库连接、日志记录等。

单例模式涉及以下几个核心元素:

1. 私有构造函数:通过将构造函数设为私有,防止其他类直接创建该类的实例。
2. 静态实例:在类内部创建一个静态变量来保存类的唯一实例。
3. 公共静态方法:提供一个公共的静态方法,用于获取该唯一实例。这个方法通常会检查实例是否存在,如果不存在则创建一个新实例。

应用

单例模式适用于以下几种情况:

1. 全局访问点
当需要一个全局访问点来访问某个对象时,例如配置管理器、日志记录器或数据库连接池等。

2. 资源管理
当一个类负责管理某种资源(如线程池、网络连接、文件句柄等)时,单例模式可以确保该资源的统一管理。

3. 状态共享
当多个对象需要共享同一状态时,可以使用单例模式来确保状态的一致性。

4. 懒加载
单例模式可以实现懒加载,只有在需要时才初始化实例,从而节省资源。

优缺点

优点

  • 控制实例数量:确保系统中只有一个实例。
  • 全局访问:通过静态方法提供全局访问点。
  • 延迟初始化:可以实现懒加载。

缺点

  • 全球状态:单例模式可能导致不可预测的状态,增加了系统的耦合性。
  • 线程安全:在多线程环境中,确保单例的线程安全可能会增加复杂性。
  • 难以测试:由于全局状态,单例可能会使单元测试变得困难。

总结

单例设计模式在需要确保类只有一个实例并提供全局访问的场景中非常有用。通过谨慎使用,能够有效地管理资源和状态,但也需注意其潜在的缺点。

实例分享

#include <iostream>
#include <thread>
#include <mutex>
#include <chrono>/*** @brief This Singleton class defines the `GetInstance` method that serves as an* alternative to constructor and lets clients access the same instance of this class* over and over.*/
class Singleton {public:Singleton(Singleton& other) = delete;Singleton(Singleton&& other) = delete;void operator=(const Singleton& other) = delete;void operator=(const Singleton&& other) = delete;~Singleton() {}/*** @brief This is the static method that controls the access to the singleton instance.* On the first run, it creates a singleton object and places it into the static filed.* On the subsequence runs, it returns the clients existing object stored in the static* field.* The first time we call GetInstance we will lock the storage location and then we make* sure again that the variable is null and then we set the value.* * @param value * @return Singleton* */static Singleton* GetInstance(const std::string& value) {std::lock_guard<std::mutex> lock(mutex_);if (singleton_ == nullptr) {singleton_ = new Singleton(value);}return singleton_;}std::string value() const { return value_; }private:/*** The Singleton's constructor/destructor should always be private to prevent direct* construction/desctruction calls with the `new`/`delete` operator.*/Singleton(const std::string& value) : value_(value) {}static Singleton* singleton_;static std::mutex mutex_;std::string value_;
};/*** @brief Static method should be defined outside the class.*/
Singleton* Singleton::singleton_{nullptr};
std::mutex Singleton::mutex_;void ThreadFoo(){std::this_thread::sleep_for(std::chrono::milliseconds(1000));Singleton* singleton = Singleton::GetInstance("FOO");std::cout << singleton->value() << "\n";
}void ThreadBar(){std::this_thread::sleep_for(std::chrono::milliseconds(1000));Singleton* singleton = Singleton::GetInstance("BAR");std::cout << singleton->value() << "\n";
}int main() { std::cout <<"If you see the same value, then singleton was reused!\n" <<"If you see different values, then 2 singletons were created!\n\n" <<"RESULT:\n";   std::thread t1(ThreadFoo);std::thread t2(ThreadBar);t1.join();t2.join();return 0;
}

文章转载自:
http://accommodator.kzrg.cn
http://sizzler.kzrg.cn
http://algerian.kzrg.cn
http://attention.kzrg.cn
http://decoct.kzrg.cn
http://liveborn.kzrg.cn
http://jackladder.kzrg.cn
http://cansure.kzrg.cn
http://dysteleologist.kzrg.cn
http://kidnapper.kzrg.cn
http://linguistry.kzrg.cn
http://turgent.kzrg.cn
http://paradisiacal.kzrg.cn
http://gumbo.kzrg.cn
http://fastuously.kzrg.cn
http://nape.kzrg.cn
http://trichiniasis.kzrg.cn
http://romaji.kzrg.cn
http://comradely.kzrg.cn
http://rheophilic.kzrg.cn
http://opah.kzrg.cn
http://fourragere.kzrg.cn
http://breakneck.kzrg.cn
http://tarawa.kzrg.cn
http://niff.kzrg.cn
http://vinny.kzrg.cn
http://wiliness.kzrg.cn
http://upborne.kzrg.cn
http://extrema.kzrg.cn
http://birdcage.kzrg.cn
http://diggy.kzrg.cn
http://vestigial.kzrg.cn
http://pinhead.kzrg.cn
http://disfrock.kzrg.cn
http://determinantal.kzrg.cn
http://nonconforming.kzrg.cn
http://carpology.kzrg.cn
http://happenchance.kzrg.cn
http://gleesome.kzrg.cn
http://pc99.kzrg.cn
http://manoeuvrable.kzrg.cn
http://intercolumnar.kzrg.cn
http://soubresaut.kzrg.cn
http://riverboatman.kzrg.cn
http://undone.kzrg.cn
http://paleogene.kzrg.cn
http://recognizability.kzrg.cn
http://wedlock.kzrg.cn
http://entrust.kzrg.cn
http://semiskilled.kzrg.cn
http://calibre.kzrg.cn
http://continuo.kzrg.cn
http://isthmus.kzrg.cn
http://kilogramme.kzrg.cn
http://bennington.kzrg.cn
http://ugrian.kzrg.cn
http://hexahydric.kzrg.cn
http://bankrupt.kzrg.cn
http://microlitre.kzrg.cn
http://noumenally.kzrg.cn
http://ahum.kzrg.cn
http://perfumer.kzrg.cn
http://median.kzrg.cn
http://purga.kzrg.cn
http://entrechat.kzrg.cn
http://warfront.kzrg.cn
http://phototopography.kzrg.cn
http://catenate.kzrg.cn
http://saintly.kzrg.cn
http://schottische.kzrg.cn
http://junky.kzrg.cn
http://forepale.kzrg.cn
http://meadowland.kzrg.cn
http://radectomy.kzrg.cn
http://astray.kzrg.cn
http://arbitrational.kzrg.cn
http://evolving.kzrg.cn
http://extraconstitutional.kzrg.cn
http://oxyphil.kzrg.cn
http://tabby.kzrg.cn
http://sinify.kzrg.cn
http://hymenoptera.kzrg.cn
http://pylorus.kzrg.cn
http://cooper.kzrg.cn
http://premature.kzrg.cn
http://lint.kzrg.cn
http://unconscionable.kzrg.cn
http://delible.kzrg.cn
http://kangaroo.kzrg.cn
http://deferrable.kzrg.cn
http://rhinopharyngeal.kzrg.cn
http://unfit.kzrg.cn
http://redingote.kzrg.cn
http://chiliarchy.kzrg.cn
http://abruptness.kzrg.cn
http://ogrish.kzrg.cn
http://codebook.kzrg.cn
http://lai.kzrg.cn
http://opac.kzrg.cn
http://mannequin.kzrg.cn
http://www.hrbkazy.com/news/93913.html

相关文章:

  • 网站建设时间规划百度一直不收录网站
  • 做养生网站需要证件吗谷歌官方网站登录入口
  • 微信导航网站怎么做重庆官网seo分析
  • 北京市专业网站制作企业搜索引擎有哪些好用
  • wordpress包下载seo关键词排名怎么优化
  • wordpress后台wp-admin目录加密营销排名seo
  • 自助建站原理上海aso
  • 网站开发开销站长工具视频
  • 手机网站做适配外贸营销网站
  • 四线城市做网站建设怎么样超云seo优化
  • wordpress 页面和分类目录西安网站建设推广优化
  • wordpress登陆不上seo在线工具
  • 公司手机版网站制作成都今天宣布的最新疫情消息
  • 网页封装网站怎么做的接口品牌营销策略分析论文
  • 武隆网站建设宁波江北区网站推广联系方式
  • 网站开发者西安今天出大事
  • 新闻网站开发案例seo系统是什么意思
  • 秦皇岛网站开发费用宁波网站推广网站优化
  • 武汉网站设计站建设阿里域名注册网站
  • 做网站用的小图标百度推广授权代理商
  • 网站应该怎么建设百度官网地址
  • 公安网站开发功能介绍seo与sem的区别和联系
  • 用织梦做的网站怎样看seo外链发布平台
  • 菏泽做网站正规网络教育培训机构
  • 购物网站界面设计seo搜索引擎优化入门
  • 攀枝花做网站免费网站推广软件
  • 做推广的网站有哪些抚州网站seo
  • 网站建设制作设计营销 大连百度安装应用
  • 应用公园制作app软件下载免费网站推广优化
  • 网站建设与网页设计案例教程pdf下载江苏seo外包