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

C++ 适配器模式详解

适配器模式(Adapter Pattern)是一种结构型设计模式,它允许不兼容的接口之间能够协同工作。

概念解析

适配器模式的核心思想是:

  1. 接口转换:将一个类的接口转换成客户希望的另一个接口

  2. 兼容性:使原本由于接口不兼容而不能一起工作的类可以一起工作

  3. 包装:通过包装的方式实现接口转换

主要组成部分

  1. 目标接口(Target):客户端期望的接口

  2. 适配者(Adaptee):需要被适配的现有接口

  3. 适配器(Adapter):将适配者接口转换为目标接口的类

代码示例

下面是一个完整的适配器模式示例,包含详细注释:

#include <iostream>
#include <memory>
#include <string>
#include <cmath>// ==================== 目标接口 ====================
// 客户端期望的几何图形接口
class Shape {
public:virtual void draw(int x1, int y1, int x2, int y2) = 0;virtual ~Shape() = default;
};// ==================== 适配者 ====================
// 现有的直线绘制类(不兼容的接口)
class LegacyLine {
public:void draw(int x1, int y1, int x2, int y2) {std::cout << "绘制直线: 从(" << x1 << "," << y1 << ")到(" << x2 << "," << y2 << ")" << std::endl;}
};// 现有的矩形绘制类(不兼容的接口)
class LegacyRectangle {
public:void draw(int x, int y, int w, int h) {std::cout << "绘制矩形: 左上角(" << x << "," << y << "), 宽" << w << ", 高" << h << std::endl;}
};// ==================== 适配器 ====================
// 直线适配器(对象适配器)
class LineAdapter : public Shape {
private:std::unique_ptr<LegacyLine> adaptee_;public:LineAdapter() : adaptee_(std::make_unique<LegacyLine>()) {}void draw(int x1, int y1, int x2, int y2) override {adaptee_->draw(x1, y1, x2, y2);}
};// 矩形适配器(对象适配器)
class RectangleAdapter : public Shape {
private:std::unique_ptr<LegacyRectangle> adaptee_;public:RectangleAdapter() : adaptee_(std::make_unique<LegacyRectangle>()) {}void draw(int x1, int y1, int x2, int y2) override {int x = std::min(x1, x2);int y = std::min(y1, y2);int width = std::abs(x2 - x1);int height = std::abs(y2 - y1);adaptee_->draw(x, y, width, height);}
};// ==================== 类适配器(通过多重继承)====================
class ClassAdapter : public Shape, private LegacyRectangle {
public:void draw(int x1, int y1, int x2, int y2) override {int x = std::min(x1, x2);int y = std::min(y1, y2);int width = std::abs(x2 - x1);int height = std::abs(y2 - y1);LegacyRectangle::draw(x, y, width, height);}
};// ==================== 客户端代码 ====================
void drawShape(Shape& shape, int x1, int y1, int x2, int y2) {shape.draw(x1, y1, x2, y2);
}int main() {std::cout << "=== 适配器模式演示 ===" << std::endl;// 使用对象适配器std::cout << "\n使用对象适配器:" << std::endl;LineAdapter lineAdapter;drawShape(lineAdapter, 10, 20, 30, 40);RectangleAdapter rectAdapter;drawShape(rectAdapter, 10, 20, 30, 40);// 使用类适配器std::cout << "\n使用类适配器:" << std::endl;ClassAdapter classAdapter;drawShape(classAdapter, 15, 25, 35, 45);// 直接使用Legacy类(不兼容)std::cout << "\n直接使用Legacy类:" << std::endl;LegacyLine line;line.draw(5, 5, 25, 25);LegacyRectangle rect;rect.draw(5, 5, 20, 20); // 参数含义不同return 0;
}

模式优势

  1. 兼容性:使不兼容的接口能够协同工作

  2. 复用性:可以复用现有的类,无需修改其源代码

  3. 灵活性:可以同时适配多个适配者类

  4. 开闭原则:引入适配器而不改变现有代码

  5. 透明性:对客户端隐藏了适配的细节

适用场景

  1. 当需要使用现有的类,但其接口与你的需求不匹配时

  2. 当想要创建一个可复用的类,该类与不相关或不可预见的类协同工作

  3. 当需要统一多个现有子类的接口时(适配器可以统一这些接口)

  4. 在遗留代码集成、第三方库适配等场景中

相关文章:

  • uniapp 云开发全集 云数据库
  • 11.施工监测
  • 【项目】基于ArkTS的网吧会员应用开发(1)
  • NHANES指标推荐:ZJU index
  • 作者新游戏1.1
  • 什么是外联模板(extern template)?
  • 解锁现代健康密码:科学养生新主张
  • Qt中的UIC
  • 【机器学习-线性回归-5】多元线性回归:概念、原理与实现详解
  • 数据库的二级索引
  • 2022年全国青少年信息素养大赛 Python编程挑战赛 小学/初中组 初赛真题答案详细解析
  • 直方图比较
  • Web前端开发:Grid 布局(网格布局)
  • RGB三原色
  • Notebook.ai 开源程序是一套工具,供作家、游戏设计师和角色扮演者创建宏伟的宇宙 - 以及其中的一切
  • 多线程-探索
  • volatile 关键字应用大全
  • 民主与民族主义作为暴力时代的财政策略
  • 基于SRS实现流媒体服务器(最简单的流媒体服务器)
  • Vite 的工作流程
  • 干细胞从科研到市场应用有多远?发展还面临何挑战?
  • 2025五一档电影票房破6亿
  • AI把野史当信史?警惕公共认知的滑坡
  • 龙翔被撤销南京市人大常委会主任职务,此前已被查
  • 美国季度GDP时隔三年再现负增长,特朗普政府关税政策对美国经济负面影响或将持续
  • 中国金茂向滨江集团提供11.21亿元诚意金借款,拟合作开发3月获取的地块