当前位置: 首页 > news >正文

C++中的适配器模式

目录

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

总结

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

相关文章:

  • 欢乐打地鼠小游戏html源码
  • 面试题:callable与runable的区别?
  • python中字典的创建
  • Spring AI 第二讲 之 Chat Model API 第九节 watsonx.ai Chat
  • 微生物实验室建设公司独家分享:从平面布局到高效设备的全流程设计技巧
  • YOLOV5总结
  • C++基础编程100题-009 OpenJudge-1.3-07 计算多项式的值
  • 协程-在单个线程内部执行
  • 为什么要分析电商用户数据?详解两大用户数据分析维度
  • 华为云DDoS攻击下的应对策略
  • Spring Cloud Gateway详解
  • BIO NIO AIO 的区别!!!
  • 【Oracle生产运维】表空间利用率不足处理
  • RabbitMQ-工作模式(Topics模式RPC模式Publisher Confirms模式)
  • docker使用auth登录
  • [笔记] php常见简单功能及函数
  • 【挥舞JS】JS实现继承,封装一个extends方法
  • 07.Android之多媒体问题
  • Cumulo 的 ClojureScript 模块已经成型
  • Js基础知识(一) - 变量
  • React-flux杂记
  • SpringBoot几种定时任务的实现方式
  • WordPress 获取当前文章下的所有附件/获取指定ID文章的附件(图片、文件、视频)...
  • 测试开发系类之接口自动化测试
  • 从零开始的无人驾驶 1
  • 分布式任务队列Celery
  • 浮现式设计
  • 关于Java中分层中遇到的一些问题
  • 检测对象或数组
  • 聊一聊前端的监控
  • 算法系列——算法入门之递归分而治之思想的实现
  • 小程序滚动组件,左边导航栏与右边内容联动效果实现
  • nb
  • 阿里云移动端播放器高级功能介绍
  • 直播平台建设千万不要忘记流媒体服务器的存在 ...
  • ​​​​​​​开发面试“八股文”:助力还是阻力?
  • # .NET Framework中使用命名管道进行进程间通信
  • #Js篇:单线程模式同步任务异步任务任务队列事件循环setTimeout() setInterval()
  • #我与Java虚拟机的故事#连载19:等我技术变强了,我会去看你的 ​
  • (Redis使用系列) Springboot 整合Redisson 实现分布式锁 七
  • (solr系列:一)使用tomcat部署solr服务
  • (动手学习深度学习)第13章 计算机视觉---微调
  • (二)c52学习之旅-简单了解单片机
  • (三十五)大数据实战——Superset可视化平台搭建
  • (五)activiti-modeler 编辑器初步优化
  • (学习日记)2024.03.12:UCOSIII第十四节:时基列表
  • (转)Linq学习笔记
  • (总结)(2)编译ORB_SLAM2遇到的错误
  • .env.development、.env.production、.env.staging
  • .Net Core 微服务之Consul(二)-集群搭建
  • .NET Framework 4.6.2改进了WPF和安全性
  • .NET 中 GetProcess 相关方法的性能
  • .net企业级架构实战之7——Spring.net整合Asp.net mvc
  • .Net中的集合
  • /usr/bin/python: can't decompress data; zlib not available 的异常处理