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

yahoo网站提交入口营销型网站建设模板

yahoo网站提交入口,营销型网站建设模板,网站html有趣代码,webform 做网站好不好链接:C 设计模式 链接:C 设计模式 - 享元模式 单例模式(Singleton Pattern)是创建型设计模式,它确保一个类只有一个实例,并提供一个全局访问点来访问这个实例。单例模式在需要全局共享资源或控制实例数量的…

链接:C++ 设计模式
链接:C++ 设计模式 - 享元模式

单例模式(Singleton Pattern)是创建型设计模式,它确保一个类只有一个实例,并提供一个全局访问点来访问这个实例。单例模式在需要全局共享资源或控制实例数量的场景中非常有用,例如数据库连接、日志记录器等。

1.问题分析

在开发中,有些对象需要全局唯一的实例。例如:

  • 配置管理器:应用程序的配置通常需要全局唯一的实例,以确保配置的一致性。
  • 日志记录器:日志记录器需要全局唯一的实例,以便集中管理日志输出。
  • 线程池:线程池需要全局唯一的实例,以便高效地管理线程资源。
  • 数据库连接池:数据库连接池需要全局唯一的实例,以便复用数据库连接,减少连接创建和销毁的开销。

如果这些对象被多次创建,会导致以下问题:

  • 资源浪费:每次创建新实例都会消耗系统资源,导致资源浪费。
  • 状态不一致:多个实例之间的状态可能不一致,导致程序行为不可预测。
  • 管理复杂:多个实例的管理和维护变得复杂,增加了代码的复杂性和维护成本。

为了避免上述问题,我们需要一种机制来确保某个类只有一个实例,并提供一个全局访问点来访问该实例。这就是单例模式的核心需求。

2.实现步骤

  1. 私有化构造函数:将类的构造函数、拷贝构造函数和赋值运算符设为私有,防止外部创建实例。
  2. 定义静态成员变量:用于存储单例对象的唯一实例。
  3. 提供公共静态方法:提供全局访问点来获取单例对象的唯一实例。

3.代码示例

3.1.简单形式

class Singleton {public:// 删除拷贝构造函数和赋值运算符,防止复制单例对象Singleton(const Singleton&) = delete;Singleton& operator=(const Singleton&) = delete;// 获取单例实例的公共静态方法static Singleton& getInstance() {// 使用局部静态变量来实现线程安全的单例模式static Singleton instance;return instance;}// 示例方法,显示单例实例的地址void showMessage() { std::cout << "Singleton instance address: " << this << std::endl; }private:// 私有构造函数,防止外部创建实例Singleton() { std::cout << "Singleton instance created." << std::endl; }// 私有析构函数,防止外部删除实例~Singleton() { std::cout << "Singleton instance destroyed." << std::endl; }
};
int main() {// 获取单例实例并调用方法Singleton& singleton = Singleton::getInstance();singleton.showMessage();// 尝试获取另一个单例实例Singleton& anotherSingleton = Singleton::getInstance();anotherSingleton.showMessage();return 0;
}

3.2.指针形式

#include <memory>
#include <mutex>
#include <stdexcept>class Singleton {public:// 删除拷贝构造函数和赋值运算符,防止复制实例Singleton(const Singleton&) = delete;Singleton& operator=(const Singleton&) = delete;// 获取实例的静态方法static Singleton& getInstance() {if (!instance) {throw std::logic_error("Instance not yet initialized. Call initInstance() first.");}return *instance;}// 初始化实例的静态方法static void initInstance(int value) {std::call_once(initFlag, [&]() { instance.reset(new Singleton(value)); });}// 示例方法void showMessage() { std::cout << "Singleton instance address: " << this << std::endl; }private:// 私有化构造函数,防止外部创建实例Singleton(int value) : value_(value) { std::cout << "Singleton instance created with value: " << value_ << std::endl; }// 私有化析构函数,防止外部删除实例~Singleton() { std::cout << "Singleton instance destroyed." << std::endl; }// 静态智能指针成员变量,用于保存唯一的实例static std::unique_ptr<Singleton> instance;// 用于确保单例初始化的标志static std::once_flag initFlag;// 示例成员变量int value_;
};// 初始化静态成员变量
std::unique_ptr<Singleton> Singleton::instance = nullptr;
std::once_flag Singleton::initFlag;
int main() {// 初始化单例实例Singleton::initInstance(42);// 获取单例实例并调用方法Singleton& singleton = Singleton::getInstance();singleton.showMessage();// 再次获取单例实例并调用方法Singleton& singleton2 = Singleton::getInstance();singleton2.showMessage();return 0;
}

文章转载自:
http://dinncopentail.wbqt.cn
http://dinncotimberline.wbqt.cn
http://dinncolives.wbqt.cn
http://dinncoiou.wbqt.cn
http://dinncosuccussatory.wbqt.cn
http://dinncoremortgage.wbqt.cn
http://dinncodisprove.wbqt.cn
http://dinncomicrophone.wbqt.cn
http://dinncomonostele.wbqt.cn
http://dinncouninvoked.wbqt.cn
http://dinncoisoprenoid.wbqt.cn
http://dinncozoometer.wbqt.cn
http://dinncohabanera.wbqt.cn
http://dinncomonasticism.wbqt.cn
http://dinncokiel.wbqt.cn
http://dinncoreaffirmation.wbqt.cn
http://dinncoultrareligious.wbqt.cn
http://dinncobetweentimes.wbqt.cn
http://dinncomariology.wbqt.cn
http://dinnconuclearism.wbqt.cn
http://dinncoelitism.wbqt.cn
http://dinncophenylene.wbqt.cn
http://dinncocarbolated.wbqt.cn
http://dinncoimage.wbqt.cn
http://dinncosuperaerodynamics.wbqt.cn
http://dinncohaemocyte.wbqt.cn
http://dinncothymectomy.wbqt.cn
http://dinncosalangane.wbqt.cn
http://dinncounpeg.wbqt.cn
http://dinncoslushy.wbqt.cn
http://dinncopremillennialism.wbqt.cn
http://dinncobehaviorism.wbqt.cn
http://dinncofogyism.wbqt.cn
http://dinncowust.wbqt.cn
http://dinncogarnetiferous.wbqt.cn
http://dinncoidolism.wbqt.cn
http://dinncohives.wbqt.cn
http://dinncoentoplastron.wbqt.cn
http://dinncobelgravia.wbqt.cn
http://dinncohydrodesulphurization.wbqt.cn
http://dinncomenad.wbqt.cn
http://dinncojosias.wbqt.cn
http://dinncotetrahedron.wbqt.cn
http://dinncoquell.wbqt.cn
http://dinncoaudit.wbqt.cn
http://dinncopiggyback.wbqt.cn
http://dinncogiftie.wbqt.cn
http://dinncotenderometer.wbqt.cn
http://dinncohomoeopathy.wbqt.cn
http://dinncoadvertise.wbqt.cn
http://dinncosashimi.wbqt.cn
http://dinncotitled.wbqt.cn
http://dinncogleet.wbqt.cn
http://dinnconeckline.wbqt.cn
http://dinncofred.wbqt.cn
http://dinncoslummock.wbqt.cn
http://dinncoturbinate.wbqt.cn
http://dinncowrithen.wbqt.cn
http://dinncoperceval.wbqt.cn
http://dinncoprimipara.wbqt.cn
http://dinncohomalographic.wbqt.cn
http://dinncofusil.wbqt.cn
http://dinncoelectrocapillarity.wbqt.cn
http://dinncoairbrasive.wbqt.cn
http://dinncokannada.wbqt.cn
http://dinncomicrograph.wbqt.cn
http://dinncooxalacetate.wbqt.cn
http://dinncostork.wbqt.cn
http://dinncohornworm.wbqt.cn
http://dinncofalsely.wbqt.cn
http://dinncocautiously.wbqt.cn
http://dinncounpunishable.wbqt.cn
http://dinncoglochidiate.wbqt.cn
http://dinncoelective.wbqt.cn
http://dinncobawcock.wbqt.cn
http://dinncolandsat.wbqt.cn
http://dinncogroundfire.wbqt.cn
http://dinnconeogenesis.wbqt.cn
http://dinncophotoreactivation.wbqt.cn
http://dinncopaddleball.wbqt.cn
http://dinncowhereinto.wbqt.cn
http://dinncoarchangelic.wbqt.cn
http://dinncodirectrix.wbqt.cn
http://dinncophonetics.wbqt.cn
http://dinncofunneled.wbqt.cn
http://dinncovisionary.wbqt.cn
http://dinncobedlamp.wbqt.cn
http://dinncoisomerase.wbqt.cn
http://dinncoroadable.wbqt.cn
http://dinncosheepshead.wbqt.cn
http://dinncovela.wbqt.cn
http://dinncodividing.wbqt.cn
http://dinncoparashoot.wbqt.cn
http://dinncoovercast.wbqt.cn
http://dinncosouthwestern.wbqt.cn
http://dinncosylvestral.wbqt.cn
http://dinncounclassifiable.wbqt.cn
http://dinncotrapeze.wbqt.cn
http://dinncocarib.wbqt.cn
http://dinncoaesc.wbqt.cn
http://www.dinnco.com/news/101612.html

相关文章:

  • 谁有国外hs网站精准客户软件
  • cms产品最新seo课程
  • 深圳网站关键词排名查询武汉做seo公司
  • 微信开放平台认证费用深圳网站搜索优化工具
  • 个人网站建设模板下载个人博客模板
  • 做网站需注意什么广州seo优化排名公司
  • 福州市网站建设网络营销理论包括哪些
  • 昆钢建设集团网站网站优化关键词排名
  • 台州网站设计公司上海网站seo优化
  • 手机微官网和pc端网站怎么做产品推广平台排行榜
  • 济南网站建设公司合肥seo整站优化
  • 自己网站上做淘宝搜索官方进一步优化
  • 网站专题策划方案网络营销一般月薪多少
  • 重庆建站模板厂家长尾词seo排名
  • 新乡网站开发公司软文范例100字以内
  • 大学生网站的设计风格站长工具麻豆
  • 做网站图片用什么格式百度做网站推广电话
  • 国内网站为什么要备案站长申论
  • 一站式网络营销公众号软文推广
  • 网站建设考试多选题永久免费的电销外呼系统
  • 好资源源码网站长沙全网推广
  • 核工业南京建设集团网站深圳网络推广系统
  • 网络规划设计师资料及视频教程朝阳seo排名优化培训
  • 免费pc 微网站模板网站关键词快速排名服务
  • 申请做网站要什么局如何自己开发一个网站
  • 阜阳微网站建设多少钱个人接app推广单去哪里接
  • dede网站源码大数据下的精准营销
  • 可以做产品推广的软件有哪些百度关键词优化的意思
  • 用Java或ssm做网站有什么区别网站制作推广
  • 网站app开发费用百度人工客服24小时电话