C++中的适配器模式

适配器模式(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;
}

总结

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

相关推荐
小羊没烦恼!几秒前
在.net中读写config文件的各种方法
java·大数据·运维·服务器·前端·数据库·oracle
2601_962885724 分钟前
如何用 Python 获取上证指数、沪深300、中证500 等指数行情?
开发语言·python
余额瞒着我当琳17 分钟前
哈希表底层原理解析,直接定址法,哈希冲突,负载因子,哈希函数,开放定址法
java·c++·哈希算法·散列表
张张123y20 分钟前
后端第六课:Python 语法速通(0 基础入门,AI 应用开发的第一门语言)
开发语言·人工智能·python·ai·后端开发
Want59522 分钟前
Python跳动的爱心①
开发语言·python·数学建模
paopaokaka_luck1 小时前
民宿管理系统 (AI民宿总结、协同过滤推荐算法、WebSocket 在线咨询、ECharts 经营分析、房态与结算联动、 民宿预订业务闭环)
java·人工智能·spring boot·websocket·数据分析·echarts·推荐算法
java资料站1 小时前
十四、Spring AI Alibaba · 案例 · 基于 HumanInTheLoop 的内容审核系统
java·spring·状态模式
ydjs678899-bot1 小时前
您的Next.js中间件匹配器配置可能会跳过您认为它保护的路由
开发语言·javascript·中间件
Chengbei111 小时前
JsOwl|AI 赋能 JS 敏感信息扫描器,AST + 正则、source‑map 源码还原、登录态爬虫输出交互式 HTML 报告
开发语言·javascript·网络·人工智能·爬虫·网络安全·自动化
David猪大卫1 小时前
【C++修炼】string类:string类的模拟实现
c语言·开发语言·数据结构·c++·经验分享·笔记·学习