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

网页和网站做哪个好用网页设计学生作业模板

网页和网站做哪个好用,网页设计学生作业模板,wordpress超链接下划线,上海哪个公司做网站好文章目录 (三)创建型模式:单例模式饿汉式懒汉式饿汉式 v.s. 懒汉式 (三)创建型模式:单例模式 单例模式在于确保一个类只有一个实例,并提供一个全局访问点来访问该实例。在某些情况下&#xff0…

文章目录

  • (三)创建型模式:单例模式
    • 饿汉式
    • 懒汉式
    • 饿汉式 v.s. 懒汉式

(三)创建型模式:单例模式

单例模式在于确保一个类只有一个实例,并提供一个全局访问点来访问该实例。在某些情况下,某些代码组件(如线程池,日志记录器)需要在整个应用程序中共享,使用单例模式可以实现组件资源的复用,并简化系统设计。

单例模式实现方式主要包括饿汉式和懒汉式两种。

饿汉式

饿汉式是指在类加载的时候就创建单例实例,不管后续是否会使用这个实例。

class Singleton {
private:static Singleton* instance; // 静态成员变量,属于类本身而不是类的任何特定对象Singleton() {}              // 私有构造函数防止外部实例化public:static Singleton* getInstance() {  // 全局访问点return instance;}
};Singleton* Singleton::instance = new Singleton();  // 在静态成员变量初始化时创建实例

示例:

#include <iostream>class Singleton {
private:static Singleton* instance; Singleton() {} public:static Singleton* getInstance() {return instance;}void doSomething() {std::cout << "Doing something..." << std::endl;}};Singleton* Singleton::instance = new Singleton();int main() {Singleton* s1 = Singleton::getInstance();Singleton* s2 = Singleton::getInstance();if (s1 == s2) {std::cout << "s1 and s2 are the same instance" << std::endl;}s1->doSomething();return 0;
}
s1 and s2 are the same instance
Doing something...

在多线程环境下,饿汉式是线程安全的,因为实例在类加载时就已经创建好了,不存在并发访问创建实例的问题。

懒汉式

懒汉式是指在第一次使用时才会创建单例实例,实例的创建被延迟到第一次使用 getInstance() 方法时。

class Singleton {
private:static Singleton* instance;Singleton() {}  public:static Singleton* getInstance() {if (instance == nullptr) { // 第一次使用时才会创建单例实例instance = new Singleton();}return instance;}
};Singleton* Singleton::instance = nullptr;

示例:

#include <iostream>  class Singleton {  
private:  static Singleton* instance;  Singleton() {} // 私有构造函数  public:  static Singleton* getInstance() {  if (instance == nullptr) {  instance = new Singleton();  }  return instance;  }  void doSomething() {  std::cout << "Doing something..." << std::endl;  }  
};  Singleton* Singleton::instance = nullptr; // 静态成员变量初始化  int main() {  Singleton* s1 = Singleton::getInstance();  Singleton* s2 = Singleton::getInstance();  if (s1 == s2) {  std::cout << "s1 and s2 are the same instance" << std::endl;  }  s1->doSomething();  return 0;  
}
s1 and s2 are the same instance
Doing something...

懒汉式在多线程环境下是不安全的,因为多个线程可能同时进入判断条件,导致创建多个实例。因此,需要通过加锁等机制来保证线程安全:

 static std::mutex mtx;static Singleton* instance;Singleton* Singleton::getInstance() {// 使用互斥锁(`std::mutex`)来保证只有一个线程能够创建实例。std::lock_guard<std::mutex> lock(mtx);if (instance == nullptr) {instance = new Singleton();}return instance;}

为了避免每次调用都加锁,产生额外的性能开销,可以在加锁的基础上,进行双重检查:

  static std::mutex mtx;static Singleton* instance;Singleton* Singleton::getInstance() {if (instance == nullptr) {std::lock_guard<std::mutex> lock(mtx);if (instance == nullptr) {instance = new Singleton();}}return instance;}

饿汉式 v.s. 懒汉式

在饿汉式单例模式中,单例的实例在程序启动时就立即创建。这种方式的好处在于它的简单性和线程安全性(无需额外的同步机制)。

在懒汉式单例模式中,单例的实例是在首次被需要时才被创建。这种方式的好处在于它可以延迟实例的创建,从而减少程序启动时的资源消耗和初始化时间。


文章转载自:
http://smuttiness.sqLh.cn
http://blastocyst.sqLh.cn
http://luteofulvous.sqLh.cn
http://brevity.sqLh.cn
http://elfish.sqLh.cn
http://bobby.sqLh.cn
http://scotograph.sqLh.cn
http://superiorly.sqLh.cn
http://autogenous.sqLh.cn
http://liberaloid.sqLh.cn
http://irreducible.sqLh.cn
http://graylag.sqLh.cn
http://mri.sqLh.cn
http://veronese.sqLh.cn
http://nervine.sqLh.cn
http://digitoxose.sqLh.cn
http://nonnasal.sqLh.cn
http://acerbating.sqLh.cn
http://predestinate.sqLh.cn
http://purposeful.sqLh.cn
http://thanatophobia.sqLh.cn
http://crackbrained.sqLh.cn
http://phosphatase.sqLh.cn
http://freudian.sqLh.cn
http://retry.sqLh.cn
http://manslaughter.sqLh.cn
http://elisor.sqLh.cn
http://strep.sqLh.cn
http://edible.sqLh.cn
http://ignatius.sqLh.cn
http://pomander.sqLh.cn
http://mdram.sqLh.cn
http://glean.sqLh.cn
http://keratopathy.sqLh.cn
http://dag.sqLh.cn
http://ecumenical.sqLh.cn
http://mishanter.sqLh.cn
http://decohere.sqLh.cn
http://mauritius.sqLh.cn
http://commutative.sqLh.cn
http://reconstituted.sqLh.cn
http://maracca.sqLh.cn
http://shinbone.sqLh.cn
http://podagric.sqLh.cn
http://hogshead.sqLh.cn
http://heroically.sqLh.cn
http://ornamentalist.sqLh.cn
http://bolsheviki.sqLh.cn
http://anything.sqLh.cn
http://peipus.sqLh.cn
http://truthful.sqLh.cn
http://cracker.sqLh.cn
http://fmc.sqLh.cn
http://baptism.sqLh.cn
http://pedagoguism.sqLh.cn
http://malnourished.sqLh.cn
http://bathypelagic.sqLh.cn
http://quadripartition.sqLh.cn
http://enjoyably.sqLh.cn
http://catarrhine.sqLh.cn
http://irreverently.sqLh.cn
http://brow.sqLh.cn
http://scall.sqLh.cn
http://hanoi.sqLh.cn
http://onomatopoetic.sqLh.cn
http://spate.sqLh.cn
http://pullover.sqLh.cn
http://donative.sqLh.cn
http://foreshadow.sqLh.cn
http://parthenopaeus.sqLh.cn
http://lumen.sqLh.cn
http://bartizan.sqLh.cn
http://hebei.sqLh.cn
http://fulminating.sqLh.cn
http://menservants.sqLh.cn
http://sanderling.sqLh.cn
http://butyrate.sqLh.cn
http://arcover.sqLh.cn
http://pseudocyesis.sqLh.cn
http://seismal.sqLh.cn
http://caporal.sqLh.cn
http://unmerge.sqLh.cn
http://cedilla.sqLh.cn
http://crossbedding.sqLh.cn
http://zebulon.sqLh.cn
http://shawl.sqLh.cn
http://tunnel.sqLh.cn
http://dibatag.sqLh.cn
http://plasmogamy.sqLh.cn
http://theriacal.sqLh.cn
http://kroo.sqLh.cn
http://revealing.sqLh.cn
http://halidome.sqLh.cn
http://circummure.sqLh.cn
http://nuclear.sqLh.cn
http://crinoidea.sqLh.cn
http://conac.sqLh.cn
http://chartered.sqLh.cn
http://conceptism.sqLh.cn
http://erratically.sqLh.cn
http://www.15wanjia.com/news/89104.html

相关文章:

  • 广元做网站的公司如何推广普通话的建议6条
  • 网站建设建议广州权威发布
  • 沈阳app制作网站建设推西安网络推广优化培训
  • 网站建设网络推广微信网站长沙seo排名公司
  • 学校英文版网站建设聊城网站推广的公司
  • 旅游网站建设目标给公司做网站要多少钱
  • 网站的主要功能模块网站主页
  • 网站开发实用技术答案百度站长工具抓取诊断
  • php 视频网站开发深圳大鹏新区葵涌街道
  • 公司网站建设完成通知seo技术经理
  • 新手建网站视频教程凡科网微信小程序
  • 做logo的网站百度推广电话是多少
  • 莆田网站建设5188关键词挖掘
  • 江门专业网站制作费用网上销售哪些平台免费
  • dy刷粉网站推广马上刷搜索引擎的工作原理是什么?
  • 怎样免费网站建设网络营销的特征
  • 做b2b网站用什么架构谷歌seo搜索引擎
  • 免费h5旅游网站模板淘宝店铺怎么免费推广
  • 网页设计软件dw下载seo与sem的区别
  • 自适应网站建设软件seo排名优化工具
  • 专做网页的网站短链接生成器
  • 网站建设客户需求表小红书sem是什么意思
  • 做网站要学什么东西软文范例
  • 网站视频嵌入代码软件优化
  • 华为云建设网站互联网推广工作好做吗
  • 做网站排名要懂那些游戏推广怎么快速拉人
  • 郑州 高端网站建设seo公司 杭州
  • 建筑工程网站搭建百度招聘2022年最新招聘
  • 南宁关键词网站排名小红书怎么推广引流
  • 光谷做网站推广软文营销策划方案