适配器模式、代理模式(C++)

适配器模式:

**定义:**适配器模式是一种结构型设计模式,它允许接口不兼容的类一起工作。它通过将一个类的接口转换成客户端期望的另一个接口,使原本由于接口不兼容而不能一起工作的那些类可以一起工作。

代理模式:

**定义:**代理模式也是一种结构型设计模式,为其他对象提供一种代理以控制对这个对象的访问。代理对象在客户端和目标对象之间起到中介的作用,可以添加额外的控制或处理逻辑。

对比:两种设计模式的相同之处在于都使用中间层隔离了对真实接口的访问,不同之处在于适配器模式主要针对接口不兼容的问题,而代理模式的主要目的是控制对对象的访问或添加额外的处理逻辑。

代码:

cpp 复制代码
// 抽象组件类
class Component {
public:
    virtual ~Component() = default;
    virtual void add(std::shared_ptr<Component> component) = 0;
    virtual void display(int depth = 0) const = 0;
};

// 叶子节点:文件类
class File : public Component {
private:
    std::string name;
public:
    File(const std::string& name) : name(name) {}

    void add(std::shared_ptr<Component>) override {
        std::cerr << "File cannot have subcomponents!" << std::endl;
    }

    void display(int depth = 0) const override {
        for (int i = 0; i < depth; ++i) {
            std::cout << "--";
        }
        std::cout << name << std::endl;
    }
};

// 容器节点:文件夹类
class Directory : public Component {
private:
    std::string name;
    std::vector<std::shared_ptr<Component>> components;
public:
    Directory(const std::string& name) : name(name) {}

    void add(std::shared_ptr<Component> component) override {
        components.push_back(component);
    }

    void display(int depth = 0) const override {
        for (int i = 0; i < depth; ++i) {
            std::cout << "--";
        }
        std::cout << name << "/" << std::endl;
        for (const auto& component : components) {
            component->display(depth + 1);
        }
    }
};

int main() {
    // 创建文件和文件夹
    auto file1 = std::make_shared<File>("file1.txt");
    auto file2 = std::make_shared<File>("file2.txt");
    auto dir1 = std::make_shared<Directory>("dir1");
    auto dir2 = std::make_shared<Directory>("dir2");

    // 构建文件系统树
    dir1->add(file1);
    dir1->add(file2);
    dir2->add(dir1);

    // 显示文件系统树
    dir2->display();

    return 0;
}
cpp 复制代码
// 抽象主题角色
class Subject {
public:
    virtual void request() = 0;
    virtual ~Subject() = default;
};

// 真实主题角色
class RealSubject : public Subject {
public:
    void request() override {
        std::cout << "RealSubject: Handling request." << std::endl;
    }
};

// 代理角色
class Proxy : public Subject {
private:
    std::shared_ptr<RealSubject> realSubject;
    bool isRealSubjectLoaded = false;

public:
    void request() override {
        if (!isRealSubjectLoaded) {
            loadRealSubject();
        }
        realSubject->request();
    }

    void loadRealSubject() {
        if (!isRealSubjectLoaded) {
            realSubject = std::make_shared<RealSubject>();
            isRealSubjectLoaded = true;
        }
    }
};

int main() {
    Subject* proxy = new Proxy();
    proxy->request();
    delete proxy;
    return 0;
}
相关推荐
不想写代码的星星3 小时前
虚函数表:C++ 多态背后的那个男人
c++
端平入洛2 天前
delete又未完全delete
c++
端平入洛3 天前
auto有时不auto
c++
哇哈哈20214 天前
信号量和信号
linux·c++
多恩Stone4 天前
【C++入门扫盲1】C++ 与 Python:类型、编译器/解释器与 CPU 的关系
开发语言·c++·人工智能·python·算法·3d·aigc
蜡笔小马4 天前
21.Boost.Geometry disjoint、distance、envelope、equals、expand和for_each算法接口详解
c++·算法·boost
超级大福宝4 天前
N皇后问题:经典回溯算法的一些分析
数据结构·c++·算法·leetcode
weiabc4 天前
printf(“%lf“, ys) 和 cout << ys 输出的浮点数格式存在细微差异
数据结构·c++·算法
问好眼4 天前
《算法竞赛进阶指南》0x01 位运算-3.64位整数乘法
c++·算法·位运算·信息学奥赛
yyjtx4 天前
DHU上机打卡D31
开发语言·c++·算法