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

关于加强公司网站建设的通知顺义建站好的公司

关于加强公司网站建设的通知,顺义建站好的公司,房地产基础知识,ss免费服务器1. 模式说明 单例模式保证类只有一个实例#xff1b;创建一个对象#xff0c;当你创建第二个对象的时候#xff0c;此时你获取到的是已经创建过的对象#xff0c;而不是一个新的对象#xff1b; 1.1 使用场景 共享资源的访问权限#xff1b;任务的管理类#xff1b;数…1. 模式说明 单例模式保证类只有一个实例创建一个对象当你创建第二个对象的时候此时你获取到的是已经创建过的对象而不是一个新的对象 1.1 使用场景 共享资源的访问权限任务的管理类数据库的操作等 1.2 要素 私有化类的构造函数使用类的私有静态指针变量指向类的唯一实例公有静态方法获取该实例 1.3 类型 懒汉模式饿汉模式 2. 懒汉模式 单例实例在第一次被使用时才进行初始化延迟初始化 2.1 基本 示例 /** brief : design patterns singleton* author : your name* compile : g -g singleton_main.cc -o d -stdc11* date : 2023/04/18* lastEditorDate: */#include iostream #include string//懒汉模式 class Singleton { public:static Singleton* getSingletonInstance() //访问函数{if(instance nullptr){instance new Singleton;}return instance;}public:void addCount() {m_count;}int32_t getCount() {return m_count;}private:static Singleton* instance; //静态指针变量Singleton(){} //私有化类的构造函数int32_t m_count{0}; };Singleton* Singleton::instance nullptr; //静态成员类外初始化int main(int argc, char* argv[]) {Singleton* single_0 Singleton::getSingletonInstance();single_0-addCount();std::coutsingle_0 value is: single_0-getCount()std::endlstd::endl;Singleton* single_1 Singleton::getSingletonInstance();single_1-addCount();std::coutsingle_0 value is: single_0-getCount()std::endl;std::coutsingle_1 value is: single_1-getCount()std::endlstd::endl; return 0; }输出 single_0 value is: 1single_0 value is: 2 single_1 value is: 22.2 线程安全 2.1中的示例是线程不安全的假如有多个线程同时调用getSingletonInstance()此时检测到instance是nullptr就会导致单例的崩溃造成内存泄漏 通过加锁来实现 示例 /** brief : design patterns singleton* author : your name* compile : g -g singleton_main.cc -o d -stdc11* date : 2023/04/18* lastEditorDate: */#include iostream #include string #include mutex//懒汉模式 std::mutex mtx; //全局的锁 class Singleton { public:static Singleton* getSingletonInstance() //访问函数{if(instance nullptr){std::lock_guardstd::mutex guard(mtx); if(instance nullptr){instance new Singleton;} }return instance;}public:void addCount() {m_count;}int32_t getCount() {return m_count;}private: static Singleton* instance; //静态指针变量Singleton(){} //私有化类的构造函数int32_t m_count{0}; }; Singleton* Singleton::instance nullptr; //静态成员类外初始化int main(int argc, char* argv[]) {Singleton* single_0 Singleton::getSingletonInstance();single_0-addCount();std::coutsingle_0 value is: single_0-getCount()std::endlstd::endl;Singleton* single_1 Singleton::getSingletonInstance();single_1-addCount();std::coutsingle_0 value is: single_0-getCount()std::endl;std::coutsingle_1 value is: single_1-getCount()std::endlstd::endl; return 0; }输出 single_0 value is: 1single_0 value is: 2 single_1 value is: 2使用 c11 中局部静态变量是线程安全的性质 示例 /** brief : design patterns singleton* author : your name* compile : g -g singleton_main.cc -o d -stdc11* date : 2023/04/18* lastEditorDate: */#include iostream #include string #include mutex//懒汉模式 class Singleton { public:static Singleton* getSingletonInstance() //访问函数{static Singleton instance; //静态指针变量return instance;}public:void addCount() {m_count;}int32_t getCount() {return m_count;}private: Singleton(){} //私有化类的构造函数int32_t m_count{0}; };int main(int argc, char* argv[]) {Singleton* single_0 Singleton::getSingletonInstance();single_0-addCount();std::coutsingle_0 value is: single_0-getCount()std::endlstd::endl;Singleton* single_1 Singleton::getSingletonInstance();single_1-addCount();std::coutsingle_0 value is: single_0-getCount()std::endl;std::coutsingle_1 value is: single_1-getCount()std::endlstd::endl; return 0; }输出 single_0 value is: 1single_0 value is: 2 single_1 value is: 23. 饿汉模式 指单例实例在程序运行时被立即执行初始化 示例 /** brief : design patterns singleton* author : your name* compile : g -g singleton_main.cc -o d -stdc11* date : 2023/04/18* lastEditorDate: */#include iostream #include string #include mutex//饿汉模式 class Singleton { public:static Singleton* getSingletonInstance() //访问函数{ return instance;}public:void addCount() {m_count;}int32_t getCount() {return m_count;}private:static Singleton* instance; //静态指针变量Singleton(){} //私有化类的构造函数int32_t m_count{0}; };Singleton* Singleton::instance new Singleton; //静态成员类外初始化int main(int argc, char* argv[]) {Singleton* single_0 Singleton::getSingletonInstance();single_0-addCount();std::coutsingle_0 value is: single_0-getCount()std::endlstd::endl;Singleton* single_1 Singleton::getSingletonInstance();single_1-addCount();std::coutsingle_0 value is: single_0-getCount()std::endl;std::coutsingle_1 value is: single_1-getCount()std::endlstd::endl; return 0; }输出 single_0 value is: 1single_0 value is: 2 single_1 value is: 24. 在开发过程中的使用 在项目的使用过程中可以将单例设计成为类模板这样其它类在设计的时候不用考虑是否需要将其设计成为单例 其它类如果需要单例属性只需要通过单例模板对其进行赋能即可 示例 /** brief : design patterns singleton* author : your name* compile : g -g singleton_main.cc -o d -stdc11* date : 2023/04/18* lastEditorDate: */#include iostream #include string #include mutextemplateclass T class Singleton { public:static T* getSingletonInstance() //访问函数{static T instance; //静态指针变量return instance;} private: Singleton(){} //私有化类的构造函数 };class C1 { public:void addCount() {m_count;}int32_t getCount() {return m_count;} private: int32_t m_count{0}; };int main(int argc, char* argv[]) {auto re1 SingletonC1::getSingletonInstance();re1-addCount();std::coutre1-getCount() value is: re1-getCount()std::endl;auto re2 SingletonC1::getSingletonInstance();re2-addCount();std::coutre1-getCount() value is: re1-getCount()std::endl;std::coutre2-getCount() value is: re2-getCount()std::endl;return 0; }输出 re1-getCount() value is: 1 re1-getCount() value is: 2 re2-getCount() value is: 2
http://www.hkea.cn/news/14359425/

相关文章:

  • 那些开店的网站是自己做的吗做网站需要什么技术
  • 网站做付款页面嘉定网站设计制作优化排名
  • 东营企业自助建站宁波seo公司排名榜
  • 高端公司网站设计建个微网站多少钱
  • 公司部门有哪些下载优化大师安装桌面
  • 做网站都需要买什么产品推广方案
  • 郑州建网站多少免费网站建设模版云盘
  • 深圳罗湖区网站有教做桥梁质检资料的网站吗
  • wordpress无法加载图片大小如何看网站是否优化
  • 湖州集团网站建设兴国做网站
  • 做设计想接外单去哪个网站好买什么样的主机(用来建网站的)支持下载
  • 有些网站开发人员工具无反应网店运营推广
  • 化隆县公司网站建设优秀网格员事迹材料
  • 制作网站建设规划书的结构为天津做网站找津坤科技
  • 甘肃省建设监理协会网站google怎么推广
  • 建设个人博客网站wordpress xss跨站脚本漏洞
  • WordPress轻量企业主题福州搜索优化行业
  • 在线建设网站 源代码游戏网游戏平台
  • 网站建设服务费记账分录wordpress 记录访问ip
  • 做网站建设工资多少创意装修公司
  • 北京 手机网站建设wordpress php 文件上传
  • 湖南省网站备案登记网站后台地址修改
  • 网站自然排名怎么深圳外贸网站定制
  • 蓝色网站素材定制制作网站价格表
  • 门户网站怎样做百度seo优
  • 爱站长用户管理系统admin
  • 如何建一个免费网站潍坊广告设计公司
  • 公司网站建设的方案网站系统 外贸
  • 做网站办公室图片淘宝客返利网站建设
  • 公司网站定制大兴建设网站