C++中的适配器模式

目录

[适配器模式(Adapter Pattern)](#适配器模式(Adapter Pattern))

实际应用

图形渲染库适配器

日志系统适配器

支付系统适配器

总结


适配器模式(Adapter Pattern)

适配器模式是一种结构型设计模式,它使得原本由于接口不兼容而不能一起工作的类可以协同工作。适配器模式通过将一个类的接口转换成客户端希望的另一种接口,使得原本接口不兼容的类可以一起工作。适配器可以是对象适配器或类适配器,对象适配器使用组合,类适配器使用多重继承。

实际应用

图形渲染库适配器

假设我们有一个旧的图形渲染库和一个新的图形渲染接口,我们需要使旧的库适配新的接口。

cpp 复制代码
#include <iostream>

// 旧的图形渲染库
class OldGraphicsRenderer {
public:
    void drawCircle(float x, float y, float radius) {
        std::cout << "Old Renderer: Drawing Circle at (" << x << ", " << y << ") with radius " << radius << "\n";
    }

    void drawRectangle(float x, float y, float width, float height) {
        std::cout << "Old Renderer: Drawing Rectangle at (" << x << ", " << y << ") with width " << width << " and height " << height << "\n";
    }
};

// 新的图形渲染接口
class NewGraphicsRenderer {
public:
    virtual void renderCircle(float x, float y, float radius) = 0;
    virtual void renderRectangle(float x, float y, float width, float height) = 0;
};

// 适配器类,将旧的渲染库适配到新的接口
class GraphicsRendererAdapter : public NewGraphicsRenderer {
private:
    OldGraphicsRenderer* oldRenderer;
public:
    GraphicsRendererAdapter(OldGraphicsRenderer* renderer) : oldRenderer(renderer) {}

    void renderCircle(float x, float y, float radius) override {
        oldRenderer->drawCircle(x, y, radius);
    }

    void renderRectangle(float x, float y, float width, float height) override {
        oldRenderer->drawRectangle(x, y, width, height);
    }
};

int main() {
    OldGraphicsRenderer oldRenderer;
    GraphicsRendererAdapter adapter(&oldRenderer);

    adapter.renderCircle(10, 10, 5);
    adapter.renderRectangle(20, 20, 10, 5);

    return 0;
}

日志系统适配器

假设我们有一个旧的日志系统和一个新的日志系统接口,我们需要使旧的日志系统适配新的接口。

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

// 旧的日志系统
class OldLogger {
public:
    void logMessage(const std::string& msg) {
        std::cout << "Old Logger: " << msg << "\n";
    }
};

// 新的日志系统接口
class NewLogger {
public:
    virtual void info(const std::string& msg) = 0;
    virtual void error(const std::string& msg) = 0;
};

// 适配器类,将旧的日志系统适配到新的接口
class LoggerAdapter : public NewLogger {
private:
    OldLogger* oldLogger;
public:
    LoggerAdapter(OldLogger* logger) : oldLogger(logger) {}

    void info(const std::string& msg) override {
        oldLogger->logMessage("INFO: " + msg);
    }

    void error(const std::string& msg) override {
        oldLogger->logMessage("ERROR: " + msg);
    }
};

int main() {
    OldLogger oldLogger;
    LoggerAdapter adapter(&oldLogger);

    adapter.info("This is an info message");
    adapter.error("This is an error message");

    return 0;
}

支付系统适配器

假设我们有一个旧的支付系统和一个新的支付接口,我们需要使旧的支付系统适配新的接口。

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

// 旧的支付系统
class OldPaymentSystem {
public:
    void makePayment(double amount, const std::string& currency) {
        std::cout << "Old Payment System: Processing payment of " << amount << " " << currency << "\n";
    }
};

// 新的支付接口
class NewPaymentInterface {
public:
    virtual void pay(double amount) = 0;
};

// 适配器类,将旧的支付系统适配到新的接口
class PaymentAdapter : public NewPaymentInterface {
private:
    OldPaymentSystem* oldPaymentSystem;
public:
    PaymentAdapter(OldPaymentSystem* paymentSystem) : oldPaymentSystem(paymentSystem) {}

    void pay(double amount) override {
        oldPaymentSystem->makePayment(amount, "USD");
    }
};

int main() {
    OldPaymentSystem oldPaymentSystem;
    PaymentAdapter adapter(&oldPaymentSystem);

    adapter.pay(100.0);

    return 0;
}

总结

适配器类通过包含或继承旧系统类,并实现新接口的方法,从而将旧系统的方法适配到新接口上。

相关推荐
惊鸿一博4 分钟前
java_网络服务相关_gateway_nacos_feign区别联系
java·开发语言·gateway
Bruce_Liuxiaowei9 分钟前
深入理解PHP安全漏洞:文件包含与SSRF攻击全解析
开发语言·网络安全·php
成工小白10 分钟前
【C++ 】智能指针:内存管理的 “自动导航仪”
开发语言·c++·智能指针
sc写算法12 分钟前
基于nlohmann/json 实现 从C++对象转换成JSON数据格式
开发语言·c++·json
SunkingYang16 分钟前
C++中如何遍历map?
c++·stl·map·遍历·方法
Andrew_Xzw17 分钟前
数据结构与算法(快速基础C++版)
开发语言·数据结构·c++·python·深度学习·算法
库库的里昂18 分钟前
【C++从练气到飞升】03---构造函数和析构函数
开发语言·c++
momo卡19 分钟前
MinGW-w64的安装详细步骤(c_c++的编译器gcc、g++的windows版,win10、win11真实可用)
c语言·c++·windows
多多*2 小时前
LUA+Reids实现库存秒杀预扣减 记录流水 以及自己的思考
linux·开发语言·redis·python·bootstrap·lua
Wish3D3 小时前
阿里云OSS 上传文件 Python版本
开发语言·python·阿里云