有哪些常见的架构设计模式在现代C++中应用

在现代C++中,以下架构设计模式被广泛应用:


1. 工厂模式

核心思想 :封装对象的创建逻辑,通过统一接口生成不同类型对象。
现代实现

  • 使用智能指针(如 std::unique_ptr)管理资源
  • 结合模板或 std::function 实现泛型工厂
cpp 复制代码
class IProduct {
public:
    virtual ~IProduct() = default;
    virtual void operation() = 0;
};

template <typename T>
std::unique_ptr<IProduct> createProduct() {
    return std::make_unique<T>();
}

2. RAII(资源获取即初始化)

核心思想 :通过对象生命周期自动管理资源(内存、文件句柄等)。
现代特性

  • 智能指针(std::unique_ptr, std::shared_ptr
  • 移动语义(避免深拷贝)
cpp 复制代码
class FileHandler {
public:
    FileHandler(const std::string& path) : handle(openFile(path)) {}
    ~FileHandler() { closeFile(handle); }
    // 禁用拷贝,启用移动
    FileHandler(FileHandler&&) = default;
private:
    HandleType handle;
};

3. 观察者模式

核心思想 :对象状态变化时自动通知依赖它的对象。
现代优化

  • 使用 std::function 替代虚函数接口
  • 结合 std::vector 存储观察者
cpp 复制代码
class Subject {
public:
    void addObserver(std::function<void()> obs) {
        observers.push_back(obs);
    }
    void notify() {
        for (auto& obs : observers) obs();
    }
private:
    std::vector<std::function<void()>> observers;
};

4. 策略模式

核心思想 :将算法封装为独立对象,支持运行时切换。
现代实现

  • 通过 std::function 和 Lambda 表达式简化策略定义
cpp 复制代码
class Context {
public:
    void setStrategy(std::function<void()> strategy) {
        this->strategy = strategy;
    }
    void execute() { strategy(); }
private:
    std::function<void()> strategy;
};

// 使用示例
Context ctx;
ctx.setStrategy([] { /* 策略A逻辑 */ });
ctx.execute();

5. 单例模式

核心思想 :确保类仅有一个实例,并提供全局访问点。
线程安全改进

  • C++11 后的 std::call_once 保证初始化原子性
cpp 复制代码
class Singleton {
public:
    static Singleton& getInstance() {
        static std::once_flag flag;
        std::call_once(flag, [] { instance.reset(new Singleton); });
        return *instance;
    }
private:
    static std::unique_ptr<Singleton> instance;
    Singleton() = default;
};

6. 适配器模式

核心思想 :转换不兼容接口为目标接口。
现代应用

  • 结合模板实现泛型适配
  • 使用 std::bind 包装旧接口
cpp 复制代码
class LegacySystem {
public:
    void legacyOperation(int x) { /*...*/ }
};

class Adapter {
public:
    Adapter(LegacySystem& legacy) : adaptee(legacy) {}
    void modernOperation() {
        std::bind(&LegacySystem::legacyOperation, &adaptee, 42)();
    }
private:
    LegacySystem& adaptee;
};

总结

现代C++通过以下特性优化传统设计模式:

  1. 智能指针(自动资源管理)
  2. Lambda 和 std::function(减少虚函数开销)
  3. 移动语义(提升性能)
  4. 模板(增强泛化能力)

选择模式时需结合具体场景,避免过度设计。

相关推荐
心平气和量大福大5 小时前
android-实例-蒲公英-更新与安装-5-签名与生成APK
android·java·开发语言
码哥DFS6 小时前
构造函数、实例对象、对象原型 ----三者关系
开发语言·javascript·原型模式
quantdash_cc6 小时前
告别自建 Requests/BS4 网页爬虫:基于 QuantDash 搭建零维保的高性能量化行情流水线
开发语言·爬虫·python·pandas·量化·quantdash
ydd1001006 小时前
寻找两个正序数组的中位数 Java 题解,二分分割详解
java·开发语言
zh路西法7 小时前
【3D SLAM源码解读系列】(二)Small_gicp——5 个积木搭出最优点云配准
c++·pcl·icp·fastgicp·smallgicp·gicp
半亩码田7 小时前
C#转Python第3.1篇:Python 的 class 没有访问修饰符?面向对象的另一条路
开发语言·python·c#
Wzx1980127 小时前
python沙箱和docker沙箱你选对了吗?
开发语言·python·docker
牧羊人.3338 小时前
Python 办公自动化从入门到入土|09 数据容器之字典
开发语言·python
小僧景贤8 小时前
嵌入式C语言 第二篇:基础语法|嵌入式C与标准C的核心差异
c语言·开发语言·嵌入式c语言
阿pin8 小时前
Android随笔-AIDL
android·开发语言·aidl