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

完整教程:【设计模式】适配器模式

概念

结构型模式


类图

Adapter


代码

#include <iostream>#include <math.h>using namespace std;class RoundPeg{public:RoundPeg() = default;explicit RoundPeg(int r) : radius(r) {}virtual ~RoundPeg() = default;virtual int GetRadius() const {return radius;}private:int radius;};class RoundHole{public:explicit RoundHole(int r) : radius(r) {}virtual ~RoundHole() = default;virtual int GetRadius() const {return radius;}string Fits(const RoundPeg* roundPeg) const {if (this->GetRadius() >= roundPeg->GetRadius()) {return "True\n";} else {return "False\n";}}private:int radius;};class SquarePeg{public:explicit SquarePeg(int w) : width(w) {}~SquarePeg() = default;virtual int GetWidth() const {return width;}private:int width;};class SquarePegAdapter: public RoundPeg {public:explicit SquarePegAdapter(SquarePeg* squarePeg) {this->squarePeg = squarePeg;}int GetRadius() const override {return squarePeg->GetWidth() * sqrt(2) / 2;}private:SquarePeg *squarePeg;};int main(int argc, char *argv[]) {auto roundHole = new RoundHole(5);auto roundPeg = new RoundPeg(5);cout << roundHole->Fits(roundPeg);delete roundPeg;auto sSquarePeg = new SquarePeg(5);auto lSquarePeg = new SquarePeg(10);// cout << roundHole->Fits(sSquarePeg); // type is diffauto sSquarePegAdapter = new SquarePegAdapter(sSquarePeg);auto lSquarePegAdapter = new SquarePegAdapter(lSquarePeg);cout << roundHole->Fits(sSquarePegAdapter);cout << roundHole->Fits(lSquarePegAdapter);delete sSquarePegAdapter;delete lSquarePegAdapter;delete sSquarePeg;delete lSquarePeg;delete roundHole;return 0;}```
http://www.gsyq.cn/news/11493.html

相关文章:

  • 原核表达可溶性蛋白难题破解
  • Torch中的tensor size
  • Codeforces 1053 (Div.2)
  • spring boot方案利用Torna生成在线接口文档
  • C#关键字 unchecked与checked - 教程
  • 详细介绍:微服务的适用边界:从金融科技到量子计算的架构哲学
  • 前台部分数据不显示
  • 指针定义以及二维数组内存地址(java/c++/python)
  • 解码数据结构线性表之顺序表
  • 中电金信:源启数据集成平台全新升级,实现便捷与性能双飞跃
  • 国产适配 + AI 一键生成!亿图图示 14.5 全平台绘图指南:260 种图表 + Visio 兼容,开发者 / 办公党速藏
  • 【2025-09-24】连岳摘抄
  • 详细介绍:基于STM32F103C8T6与HC-08蓝牙模块实现手机连接方案
  • Qwen 发布高精度实时音视频同传模型;AirPods 实时翻译功能新增中文丨日报
  • ESP8266+CH340+SG90舵机远程控制开关
  • VisionPro学习笔记- PMAlignTOOL
  • FeignClient提示No subject alternative DNS name matching配置SSL
  • mvnd 安装和配置
  • 第五届IEEE能源工程与电力系统国际学术会议(IEEE-EEPS 2025)
  • C#开源组件
  • 626. 换座位
  • 时序大模型/时序小模型
  • Gitee PPM:数据驱动的软件工厂项目管理新范式
  • c语言经典课程资料
  • 探秘圆周率 π:圆周率计算在线工具
  • 注意力机制下的位置编码的理解和梳理
  • 以史为鉴【长期置顶】
  • 【笔记】Prfer 序列
  • 完整教程:服务器磁盘空间满了怎么办?阿里云ECS清理与云盘扩容教程
  • c++输入输出详解