C++中的适配器模式

devtools/2025/3/15 23:04:52/

目录

适配器模式(Adapter Pattern)

实际应用

图形渲染库适配器

日志系统适配器

支付系统适配器

总结


适配器模式(Adapter Pattern)

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

实际应用

图形渲染库适配器

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

#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;
}

日志系统适配器

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

#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;
}

支付系统适配器

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

#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;
}

总结

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


http://www.ppmy.cn/devtools/48427.html

相关文章

格式化数字金额,每三位加逗号

前言 格式化金额&#xff0c;例如把数字 98765432 格式化为&#xff1a;&#xffe5;987,654.32。&#xffe5;符号可以换成别的&#xff0c;也可以去掉 const formatStatistic (text, precision 2, divPrecision true, icon &#xffe5;) > {//precision是保留小数位…

Github 2024-06-12 C开源项目日报 Top10

根据Github Trendings的统计,今日(2024-06-12统计)共有10个项目上榜。根据开发语言中项目的数量,汇总情况如下: 开发语言项目数量C项目10PHP项目1PLpgSQL项目1C++项目1Ventoy: 100%开源的可启动USB解决方案 创建周期:1534 天开发语言:C协议类型:GNU General Public Licen…

html5新增的标签有哪些

HTML5新增的标签主要可以分为几类&#xff0c;以下是按照类别进行分点表示和归纳的HTML5新增标签&#xff1a; 结构性标签&#xff1a; <header>: 定义文档或节的头部。<nav>: 定义导航链接。<section>: 定义文档中的独立节。<article>: 定义文档、页…

Chrome 扩展 background 与content_script 之间通信

content_script 向 background 发消息要用&#xff1a;chrome.runtime.sendMessage chrome.runtime.sendMessage({event: "xhr", data:option }, function (res) { //option.onload(res);//console.log(res);if (res.event "xhr" && !res.err){op…

jmeter性能优化之mysql监控sql慢查询语句分析

接上次博客&#xff1a;基础配置 多用户登录并退出jmx文件&#xff1a;百度网盘 提取码&#xff1a;0000 一、练习jmeter脚本检测mysql慢查询 随意找一个脚本(多用户登录并退出)&#xff0c;并发数设置300、500后分别查看mysql监控平台 启动后查看&#xff0c;主要查看mysql…

基于Python + Flask+ Mysq实现简易留言板

使用Python Flask Mysql实现简易留言板&#xff0c;包括网友编辑留言、修改留言&#xff0c;删除留言、分页显示四大功能。 写出留言板建设过程&#xff0c;包括开发使用工具、留言板模块设计、数据库设计、页面设计、关键技术。 留言板建设过程总结 一&#xff0e;开发使用…

MySQL(5)

聚合函数 GROUP BY 的使用 需求&#xff1a;查询各个部门的平均工资&#xff0c;最高工资SELECT department_id,AVG(salary),SUM(salary)FROM employeesGROUP BY department_id;需求&#xff1a;查询各个job_id的平均工资SELECT job_id,AVG(salary)FROM employeesGROUP BY jo…

使用GPT-soVITS再4060下2小时训练声音模型以及处理断句带来的声音模糊问题

B站UP主视频 感谢UP主“白菜工厂1145号员工”的“熟肉”&#xff0c;我这篇笔记就不展示整一个训练和推理流程&#xff0c;重点写的4060该注意的一些事项。如何解决断句模糊的问题&#xff0c;在本篇笔记的最末尾。 相关连接&#xff1a; 原项目github UP主的说明文档 1、训…