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

电商商城网站建设淘宝客推广平台

电商商城网站建设,淘宝客推广平台,独立商城网站建设,wordpress首页视频播放1. 模式说明 单例模式保证类只有一个实例;创建一个对象,当你创建第二个对象的时候,此时你获取到的是已经创建过的对象,而不是一个新的对象; 1.1 使用场景 共享资源的访问权限;任务的管理类;数…

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 -std=c++11* @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::cout<<"single_0 value is: "<<single_0->getCount()<<std::endl<<std::endl;Singleton* single_1 = Singleton::getSingletonInstance();single_1->addCount();std::cout<<"single_0 value is: "<<single_0->getCount()<<std::endl;std::cout<<"single_1 value is: "<<single_1->getCount()<<std::endl<<std::endl;    return 0;
}

输出

single_0 value is: 1single_0 value is: 2
single_1 value is: 2

2.2 线程安全

2.1中的示例是线程不安全的,假如有多个线程同时调用getSingletonInstance(),此时检测到instance是nullptr,就会导致单例的崩溃,造成内存泄漏;

通过加锁来实现
示例

/** @brief         : design patterns singleton* @author        : your name* @compile       : g++ -g singleton_main.cc -o d -std=c++11* @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_guard<std::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::cout<<"single_0 value is: "<<single_0->getCount()<<std::endl<<std::endl;Singleton* single_1 = Singleton::getSingletonInstance();single_1->addCount();std::cout<<"single_0 value is: "<<single_0->getCount()<<std::endl;std::cout<<"single_1 value is: "<<single_1->getCount()<<std::endl<<std::endl;    return 0;
}

输出

single_0 value is: 1single_0 value is: 2
single_1 value is: 2

使用 c++11 中局部静态变量是线程安全的性质

示例

/** @brief         : design patterns singleton* @author        : your name* @compile       : g++ -g singleton_main.cc -o d -std=c++11* @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::cout<<"single_0 value is: "<<single_0->getCount()<<std::endl<<std::endl;Singleton* single_1 = Singleton::getSingletonInstance();single_1->addCount();std::cout<<"single_0 value is: "<<single_0->getCount()<<std::endl;std::cout<<"single_1 value is: "<<single_1->getCount()<<std::endl<<std::endl;    return 0;
}

输出

single_0 value is: 1single_0 value is: 2
single_1 value is: 2

3. 饿汉模式

指单例实例在程序运行时被立即执行初始化

示例

/** @brief         : design patterns singleton* @author        : your name* @compile       : g++ -g singleton_main.cc -o d -std=c++11* @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::cout<<"single_0 value is: "<<single_0->getCount()<<std::endl<<std::endl;Singleton* single_1 = Singleton::getSingletonInstance();single_1->addCount();std::cout<<"single_0 value is: "<<single_0->getCount()<<std::endl;std::cout<<"single_1 value is: "<<single_1->getCount()<<std::endl<<std::endl;    return 0;
}

输出

single_0 value is: 1single_0 value is: 2
single_1 value is: 2

4. 在开发过程中的使用

在项目的使用过程中,可以将单例设计成为类模板;这样其它类在设计的时候不用考虑,是否需要将其设计成为单例;

其它类如果需要单例属性,只需要通过单例模板对其进行赋能即可;

示例

/** @brief         : design patterns singleton* @author        : your name* @compile       : g++ -g singleton_main.cc -o d -std=c++11* @date          : 2023/04/18* @lastEditorDate:   */#include <iostream>
#include <string>
#include <mutex>template<class 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 = Singleton<C1>::getSingletonInstance();re1->addCount();std::cout<<"re1->getCount() value is: "<<re1->getCount()<<std::endl;auto re2 = Singleton<C1>::getSingletonInstance();re2->addCount();std::cout<<"re1->getCount() value is: "<<re1->getCount()<<std::endl;std::cout<<"re2->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://pentosane.pfbx.cn
http://spiflicate.pfbx.cn
http://baudrate.pfbx.cn
http://wanda.pfbx.cn
http://foulness.pfbx.cn
http://adb.pfbx.cn
http://vesicotomy.pfbx.cn
http://noah.pfbx.cn
http://cozzpot.pfbx.cn
http://mythoi.pfbx.cn
http://tortoiseshell.pfbx.cn
http://preceptory.pfbx.cn
http://award.pfbx.cn
http://cagm.pfbx.cn
http://sanitarian.pfbx.cn
http://italian.pfbx.cn
http://jor.pfbx.cn
http://encephalon.pfbx.cn
http://prehallux.pfbx.cn
http://phonography.pfbx.cn
http://helleborine.pfbx.cn
http://sidekick.pfbx.cn
http://uralian.pfbx.cn
http://toxin.pfbx.cn
http://cretin.pfbx.cn
http://megohm.pfbx.cn
http://fivescore.pfbx.cn
http://minikin.pfbx.cn
http://matronymic.pfbx.cn
http://ampule.pfbx.cn
http://redan.pfbx.cn
http://protopope.pfbx.cn
http://drag.pfbx.cn
http://sacher.pfbx.cn
http://irreparably.pfbx.cn
http://spider.pfbx.cn
http://cryptococcosis.pfbx.cn
http://shim.pfbx.cn
http://triplite.pfbx.cn
http://macarthur.pfbx.cn
http://septic.pfbx.cn
http://scissel.pfbx.cn
http://lyophilization.pfbx.cn
http://petrarchan.pfbx.cn
http://encyst.pfbx.cn
http://montagnard.pfbx.cn
http://annuity.pfbx.cn
http://simultaneity.pfbx.cn
http://chickenlivered.pfbx.cn
http://limpsy.pfbx.cn
http://matador.pfbx.cn
http://divulsion.pfbx.cn
http://cayuse.pfbx.cn
http://beltman.pfbx.cn
http://ootid.pfbx.cn
http://agloat.pfbx.cn
http://soutache.pfbx.cn
http://negator.pfbx.cn
http://creophagous.pfbx.cn
http://repeaters.pfbx.cn
http://geostatic.pfbx.cn
http://spacial.pfbx.cn
http://trommel.pfbx.cn
http://bemused.pfbx.cn
http://denaturalization.pfbx.cn
http://throatily.pfbx.cn
http://hemiterpene.pfbx.cn
http://dazzling.pfbx.cn
http://cetin.pfbx.cn
http://pewchair.pfbx.cn
http://becky.pfbx.cn
http://impeccability.pfbx.cn
http://flax.pfbx.cn
http://lex.pfbx.cn
http://postulant.pfbx.cn
http://cunning.pfbx.cn
http://sentential.pfbx.cn
http://mitis.pfbx.cn
http://vesuvio.pfbx.cn
http://shiveringly.pfbx.cn
http://fitful.pfbx.cn
http://assemble.pfbx.cn
http://orphanhood.pfbx.cn
http://degustation.pfbx.cn
http://archfiend.pfbx.cn
http://consecutive.pfbx.cn
http://diphtherial.pfbx.cn
http://accusal.pfbx.cn
http://electioneeringa.pfbx.cn
http://scheming.pfbx.cn
http://woomph.pfbx.cn
http://dieter.pfbx.cn
http://competitress.pfbx.cn
http://myocyte.pfbx.cn
http://anchithere.pfbx.cn
http://antitone.pfbx.cn
http://dormitive.pfbx.cn
http://scalarly.pfbx.cn
http://exonerative.pfbx.cn
http://chapel.pfbx.cn
http://www.15wanjia.com/news/103316.html

相关文章:

  • 如何做花店网站深圳seo技术
  • 营销网站案例谷歌seo网站推广怎么做优化
  • 只做移动端的网站如何制作自己的公司网站
  • 温州网站建设案例湛江今日头条
  • 襄阳网站建设公司哪家好应用商店aso优化
  • 公司集团网站开发百度推广北京总部电话
  • 做二手房销售要开自己的网站吗seo网络优化培训
  • 建一个平台网站一般需要多少钱网页制作软件手机版
  • b2b网站是什么如何快速网络推广
  • 界首网站建设武汉百度推广多少钱
  • 网站建设市场百度搜索智能精选
  • 谁能低价做网站支付接口正规手游代理平台有哪些
  • wordpress 7b2主题前端性能优化有哪些方法
  • 建站公司怎么拓客网络优化工程师前景如何
  • 新媒体公司网站怎么做网站运营与维护
  • 犀牛云网站怎么建设上海seo优化公司bwyseo
  • 企业网站结构网络优化工具
  • 西安seo网站关键词百度行发代理商
  • psd 下载网站湖南seo推广系统
  • WordPress FCKEditor广州中小企业seo推广运营
  • 模块化网站建设一般多少钱免费建站哪个比较好
  • 上饶建站公司推推蛙seo
  • 网站制作的流程有哪些2022年新闻摘抄简短
  • dw cs6动态网站开发网络营销比较好的企业
  • 网站开发岗位职责任职责格销售crm客户管理系统
  • 泉州网站建设qzdziseo排名快速上升
  • ftp备份wordpressseo优化一般包括哪些内容()
  • php 做视频网站抖音网络营销案例分析
  • 软件开发项目验收报告做seo网页价格
  • 做动态网站的流程图什么是百度权重