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

海报模板网站有哪些景泰做网站

海报模板网站有哪些,景泰做网站,蓝盾信息做网站吗,免费挣钱最快的游戏定义 单例设计模式#xff08;Singleton Pattern#xff09;是一种创建型设计模式#xff0c;它确保一个类只有一个实例#xff0c;并提供一个全局访问点来获取该实例。这种模式常用于需要控制对某些资源的访问的场景#xff0c;例如数据库连接、日志记录等。 单例模式涉…定义 单例设计模式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_guardstd::mutex lock(mutex_);if (singleton_ nullptr) {singleton_ new Singleton(value);}return singleton_;}std::string value() const { return value_; }private:/*** The Singletons 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://www.sczhlp.com/news/227344/

相关文章:

  • 戴尔公司网站建设成功的关键是什么单页网站如何制作
  • 网站建设所需要的东西做网站 公司有哪些
  • react可以做门户网站么漳浦网站设计
  • 白云免费网站建设网站建设360元起全包
  • 怎样免费个人网站建设十大网络科技公司
  • 昆明大型网站建设费用ps免费素材网站有哪些
  • 安徽省高速公路建设指挥部网站网站如何做网页查询
  • 伍佰亿门户网站设计图片logo免费
  • 沂南网站设计景观设计师如何做网站
  • 义乌制作网站公司网站建设公司无锡
  • 视频网站建设 知乎湖南企业网站制作
  • wordpress站内搜索tag() wordpress
  • 合肥 网站制作怎么看网站是谁家做的
  • 哈尔滨市营商环境建设监督局网站网站单页面可以做302跳转吗
  • 装饰网站建设的背景网站模板怎么编辑
  • 广安发展建设集团公司网站鹤山市城乡住房建设部网站
  • 震天建设集团网站开发个app的公司在哪
  • 建设通网站是什么性质郴州网站建设公司
  • 公司网站建设费怎么入账商城网站wordpress
  • 临桂区住房和城乡建设局门户网站煤炭建设行业协会网站
  • 老城网站建设外贸推广具体是做什么
  • 南京触屏网站开发网页对于网站有多重要
  • 商城型网站建设代理加盟推荐聊城做网站的公司
  • 局网站建设方案网站界面设计试题
  • destoon 网站后台赶集网官网首页
  • wordpress 企业网站 授权费wordpress友情链接分类显示
  • 佛山网站开发哪家专业石家庄369招聘信息网
  • 北京通州网站建设系统开发软件
  • 卖建材的网站有哪些丰镇网络推广
  • 上虞做网站公司莘县建设局网站