C++设计模式之组合模式中适用缓存机制提高遍历与查找速度

在组合设计模式中,为了提高反复遍历和查找的速度,可以引入缓存机制。缓存机制可以通过存储已经遍历过的子组件或计算过的结果来减少重复操作的开销。以下是一个示例,展示了如何在组合模式中使用缓存机制来提高性能。

示例:组合设计模式中的缓存机制

1. 组件接口

定义一个组件接口 Component,所有组件(叶子和组合节点)都实现这个接口。

cpp 复制代码
#include <vector>
#include <unordered_map>
#include <iostream>
#include <string>

class Component {
public:
    virtual void operation() = 0;
    virtual void add(Component* component) {}
    virtual void remove(Component* component) {}
    virtual Component* find(const std::string& name) = 0;
    virtual ~Component() {}
    std::string name;
};
2. 叶子节点

定义一个叶子节点 Leaf,实现 Component 接口。

cpp 复制代码
class Leaf : public Component {
public:
    Leaf(const std::string& name) {
        this->name = name;
    }
    void operation() override {
        std::cout << "Leaf " << name << " operation" << std::endl;
    }
    Component* find(const std::string& name) override {
        return (this->name == name) ? this : nullptr;
    }
};
3. 组合节点

定义一个组合节点 Composite,实现 Component 接口。组合节点管理子组件,并且在查找时使用缓存。

cpp 复制代码
class Composite : public Component {
public:
    Composite(const std::string& name) {
        this->name = name;
    }
    void operation() override {
        std::cout << "Composite " << name << " operation" << std::endl;
        for (auto& component : components) {
            component->operation();
        }
    }
    void add(Component* component) override {
        components.push_back(component);
    }
    void remove(Component* component) override {
        components.erase(std::remove(components.begin(), components.end(), component), components.end());
    }
    Component* find(const std::string& name) override {
        // 检查缓存
        if (cache.find(name) != cache.end()) {
            return cache[name];
        }

        // 遍历子组件查找
        for (auto& component : components) {
            if (component->find(name) != nullptr) {
                cache[name] = component;
                return component;
            }
        }
        return nullptr;
    }

private:
    std::vector<Component*> components;
    std::unordered_map<std::string, Component*> cache;
};
4. 主程序

创建一个组合节点,并添加叶子节点,然后演示如何使用缓存机制提高查找速度。

cpp 复制代码
int main() {
    Composite* root = new Composite("Root");
    root->add(new Leaf("Leaf1"));
    root->add(new Leaf("Leaf2"));

    Composite* subComposite = new Composite("SubComposite");
    subComposite->add(new Leaf("Leaf3"));
    subComposite->add(new Leaf("Leaf4"));
    root->add(subComposite);

    // 第一次查找,缓存未命中
    std::cout << "Searching for 'Leaf3' (first time)..." << std::endl;
    Component* found = root->find("Leaf3");
    if (found) {
        found->operation();
    } else {
        std::cout << "Not found" << std::endl;
    }

    // 第二次查找,缓存命中
    std::cout << "Searching for 'Leaf3' (second time)..." << std::endl;
    found = root->find("Leaf3");
    if (found) {
        found->operation();
    } else {
        std::cout << "Not found" << std::endl;
    }

    delete root;
    return 0;
}

解释

  1. 缓存机制 :在 Composite 类中,我们使用 std::unordered_map 来存储子组件的查找结果。当查找操作发生时,首先检查缓存中是否已经存在该组件。如果存在,直接返回缓存中的结果;如果不存在,则遍历子组件进行查找,并将结果存入缓存。

  2. 性能提升:通过使用缓存机制,可以避免反复遍历子组件,从而显著提高查找操作的速度。

  3. 适用场景:这种缓存机制特别适用于树形结构中频繁进行相同查找操作的场景。通过缓存已经查找过的结果,可以减少不必要的递归遍历,提升系统性能。

通过这种方式,你可以在组合设计模式中有效地利用缓存机制来提高反复遍历和查找的速度。

相关推荐
BestOrNothing_20159 小时前
C++零基础到工程实战(4.3.3):vector数组访问与遍历
c++·迭代器·stl·vector·动态数组
charlie1145141919 小时前
通用GUI编程技术——图形渲染实战(三十三)——Direct2D与Win32/GDI互操作:渐进迁移实战
c++·图形渲染·gui·win32
文祐9 小时前
C++类之虚函数表及其内存布局(一个子类继承一个父类)
开发语言·c++
回忆2012初秋10 小时前
工厂方法模式完整实现:GPS转换
设计模式·工厂方法模式
墨尘笔尖10 小时前
最大最小值降采样算法的优化
c++·算法
YIN_尹12 小时前
【Linux系统编程】进程地址空间
linux·c++
EverestVIP12 小时前
C++中空类通常大小为1的原理
c++
网域小星球13 小时前
C++ 从 0 入门(六)|C++ 面试必知:运算符重载、异常处理、动态内存进阶(终极补充)
开发语言·c++·面试
胡志辉的博客13 小时前
多智能体协作,不是多开几个 Agent:从中介者模式看 OpenClaw 和 Hermes Agent
人工智能·设计模式·ai·agent·中介者模式·openclaw·herman
晚会者荣13 小时前
红黑树的插入(有图)
c++