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

网站建设课程有哪些收获中国排名第一的策划公司

网站建设课程有哪些收获,中国排名第一的策划公司,wordpress结婚模板,库车县建设网站深入解析单例模式:从思想到C实战实现 一、设计模式与单例模式思想 1.1 设计模式的价值 设计模式是软件工程领域的经验结晶,如同建筑领域的经典蓝图。它们提供了经过验证的解决方案模板,能有效解决以下问题: 提高代码复用性提升…

深入解析单例模式:从思想到C++实战实现

一、设计模式与单例模式思想

1.1 设计模式的价值

设计模式是软件工程领域的经验结晶,如同建筑领域的经典蓝图。它们提供了经过验证的解决方案模板,能有效解决以下问题:

  • 提高代码复用性
  • 提升系统可维护性
  • 增强代码可读性
  • 降低模块耦合度

1.2 单例模式核心思想

单例模式(Singleton Pattern)确保一个类:

  1. 仅有一个实例存在
  2. 提供全局访问点
  3. 严格控制实例化过程

适用场景包括:

  • 配置管理器
  • 日志记录器
  • 线程池
  • 数据库连接池
  • 硬件接口访问

二、C++实现方案对比

2.1 基础懒汉式(线程不安全)


#include <iostream>
using namespace std;
// 基础懒汉模式,非线程安全
class Singleton {
public:static Singleton* getInstance() {if (!instance) {instance = new Singleton();}return instance;}void doSomething() {cout << "Doing something..." << endl;}// 删除拷贝构造函数和赋值运算符Singleton(const Singleton&) = delete;Singleton& operator=(const Singleton&) = delete;private:Singleton() = default;~Singleton() = default;static Singleton* instance;
};// 初始化静态成员
Singleton* Singleton::instance = nullptr;#include <thread>
#include <vector>void threadFunction(int i) {Singleton* singleton = Singleton::getInstance();cout << "Thread " << i << " Singleton address: " << singleton << endl;singleton->doSomething();
}int main() {const int numThreads = 10;std::vector<std::thread> threads;for (int i = 0; i < numThreads; ++i) {threads.emplace_back(threadFunction, i);}for (auto& thread : threads) {thread.join();}return 0;
}

 运行结果:可以看出非线程安全的,创建了两个实例。

2.2 线程安全双检锁(C++11+)


 

#include <iostream>
#include <mutex>
#include <thread>
#include <vector>using namespace std;
class ThreadSafeSingleton {
public:static ThreadSafeSingleton* getInstance() {if (!instance) {std::lock_guard<std::mutex> lock(mutex);if (!instance) {instance = new ThreadSafeSingleton();}}return instance;}void doSomething() {cout << "Doing something..." << endl;}private:static ThreadSafeSingleton* instance;static std::mutex mutex;
};// 初始化静态成员
ThreadSafeSingleton* ThreadSafeSingleton::instance = nullptr;
std::mutex ThreadSafeSingleton::mutex;

#include <thread>
#include <vector>
#include "lazy_single_thread_safe.h"void LazySafeThreadFunction(int i) {ThreadSafeSingleton* singleton = ThreadSafeSingleton::getInstance();cout << "Thread Safe" << i << " Singleton address: " << singleton << endl;singleton->doSomething();
}// 线程安全懒汉模式 demo
int main() {const int numThreads = 10;std::vector<std::thread> threads1;for (int i = 0; i < numThreads; ++i) {threads1.emplace_back(LazySafeThreadFunction, i);}for (auto& thread : threads1) {thread.join();}return 0;
}

 运行结果:线程安全。

2.3 现代C++实现(最优方案)


#include <iostream>
using namespace std;/****  饿汉式是否 Lazy 初始化:否是否多线程安全:是描述:这种方式比较常用,但容易产生垃圾对象。优点:没有加锁,执行效率会提高。缺点:类加载时就初始化,浪费内存。它基于 classloader 机制避免了多线程的同步问题,不过,instance在类装载时就实例化,虽然导致类装载的原因有很多种,在单例模式中大多数都是调用 getInstance 方法,但是也不能确定有其他的方式(或者其他的静态方法)导致类装载,这时候初始化 instance 显然没有达到 lazy loading 的效果。
*/
#include <iostream>class EagerSingleton {
public:// 获取单例实例的静态方法static EagerSingleton* getInstance() {static EagerSingleton instance; // 在第一次调用时创建实例return &instance;}// 删除拷贝构造函数和赋值运算符,防止复制EagerSingleton(const EagerSingleton&) = delete;EagerSingleton& operator=(const EagerSingleton&) = delete;// 示例方法void doSomething() {cout << "Doing something..." << endl;}private:// 私有构造函数,防止外部实例化EagerSingleton() {std::cout << "EagerSingleton instance created." << std::endl;}
};

运行结果:

方案对比表:

特性基础懒汉式双检锁现代实现
线程安全✔️✔️
延迟初始化✔️✔️✔️
自动析构✔️
C++标准要求-≥11≥11
实现复杂度简单中等简单

三、关键实现细节解析

3.1 线程安全保证

  • 现代实现方案利用C++11的静态变量初始化特性
  • 编译器保证静态局部变量的线程安全
  • 双检锁方案需要内存屏障支持

3.2 资源管理

  • 现代方案自动处理析构
  • 指针方案需要自定义销毁逻辑
  • 可结合智能指针优化:
#include <memory>class SmartSingleton {
public:static SmartSingleton& getInstance() {static auto instance = std::make_shared<SmartSingleton>();return *instance;}// ...其他成员...
};

3.3 继承扩展方案

template <typename T>
class InheritableSingleton {
public:static T& getInstance() {static T instance;return instance;}protected:InheritableSingleton() = default;virtual ~InheritableSingleton() = default;
};class MyLogger : public InheritableSingleton<MyLogger> {friend class InheritableSingleton<MyLogger>;// 具体实现...
};

 

四、最佳实践与陷阱规避

4.1 使用建议

  1. 优先选择现代实现方案
  2. 明确单例的生命周期
  3. 做好线程安全测试
  4. 考虑依赖注入替代方案

4.2 常见陷阱

  • 循环依赖问题
  • 测试困难(使用虚函数增加可测试性)
  • 多线程环境下的初始化竞争
  • 异常安全性问题

4.3 性能考量

  • 现代方案无锁设计效率最高
  • 双检锁方案需要权衡锁开销
  • 饿汉式初始化可能影响启动速度

五、演进与替代方案

5.1 单例模式演进

  • 多例模式(Multiton)
  • 线程局部单例
  • 集群环境单例

5.2 现代替代方案

  • 依赖注入容器
  • 全局命名空间函数(权衡使用)
  • 服务定位器模式

六、总结

单例模式作为创建型模式的代表,在特定场景下能有效管理系统资源。但需要注意:

  • 不要滥用导致全局状态污染
  • 优先考虑依赖注入等现代方案
  • 关注线程安全和生命周期管理

正确使用单例模式,可以构建出高效、可控的软件系统。但记住,好的架构应该是灵活可扩展的,而不是充满各种全局状态的"单例陷阱"。

示例代码已在GCC 13.1和Clang 16.0测试通过,建议使用C++17及以上标准编译。实际项目中请根据具体需求选择合适的实现方案。


文章转载自:

http://CSm5C1gK.LLyjx.cn
http://QXdKbVqu.LLyjx.cn
http://xfJ41wuL.LLyjx.cn
http://awGpwdrq.LLyjx.cn
http://T64L2KPd.LLyjx.cn
http://OeFWZgQU.LLyjx.cn
http://WbEgerrQ.LLyjx.cn
http://kIqT6pAw.LLyjx.cn
http://3qrBOuMh.LLyjx.cn
http://PAZyIEwq.LLyjx.cn
http://GQlW6LD0.LLyjx.cn
http://SNJ01roy.LLyjx.cn
http://ZiqMJ4NE.LLyjx.cn
http://75mGIiZY.LLyjx.cn
http://5CrShIvX.LLyjx.cn
http://KWA7nHW1.LLyjx.cn
http://mMDf7iqz.LLyjx.cn
http://fOvOF8Cy.LLyjx.cn
http://lCqnmvQm.LLyjx.cn
http://tn0OgPvn.LLyjx.cn
http://UYcma4Uu.LLyjx.cn
http://mHg4iqLV.LLyjx.cn
http://3BDm0kly.LLyjx.cn
http://bdfR3XNn.LLyjx.cn
http://5QAJjLfB.LLyjx.cn
http://l75bn6Xg.LLyjx.cn
http://Rccb1S6Q.LLyjx.cn
http://d6vdBWL8.LLyjx.cn
http://VOK7VLiT.LLyjx.cn
http://MAQSb4U4.LLyjx.cn
http://www.dtcms.com/wzjs/617926.html

相关文章:

  • 效果型网站建设安徽省建设监理网站
  • 基于html5的电商网站开发网站建设的简要任务执行书
  • 北京做养生SPA的网站建设制作企业宣传片拍摄公司
  • 自己的网站打不开了北京通网站建设
  • 郑州网站建设企业推荐怎么做电商运营
  • 上海网站开发定制wordpress标签分级
  • 网站编辑适不适合男生做wordpress 阌栾
  • 手机网站和电脑网站开发wordpress seo
  • 红安建设局官方网站河南小学网站建设
  • 临沂做网站wyjzgzs设计logo怎么设计
  • 企业为什么要建站点呢广州做网站公司电话
  • 商城网站大全做网站改版的
  • 沈阳市建设局网站首页嘉兴网络项目建站公司
  • 免费软件不收费网站WordPress相册插件pro
  • 网站建立策划书郑州设计logo公司
  • 6731官方网站下载it培训学校哪家好
  • 如何做学校的网站设计项目三的设计与制作
  • html5网站开发语言的有点亚马逊电商平台入口
  • 制作外贸网站的公司wordpress能放视频
  • iis网站批量导入如何去掉wordpress版权信息
  • 沈阳酒店企业网站制作温州网站排名团队
  • 沧州网站建设代理价格网站前端建设都需要什么
  • 站长素材网wordpress动态新闻代码
  • 建设银行个人网站打不开自己注册域名
  • 企业站网站公司免费网站
  • 义乌购网站做代销怎么样网盟推广平台
  • 上海网站推广有哪些用安卓手机做网站主机
  • 郑州高新区建设环保局网站wordpress p3
  • 招聘信息网站建设网站建设的实训技术总结
  • 网站建设费可分摊几年台州建设局网站