当前位置: 首页 > 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.jsqmd.com/news/3466/

相关文章:

  • 原核表达可溶性蛋白难题破解
  • 深入解析:Adobe Fresco下载教程Adobe Fresco 2025保姆级安装步骤(附安装包)
  • Torch中的tensor size
  • 深入解析:贪心算法之船舶装载问题
  • Codeforces 1053 (Div.2)
  • 抗体药物偶联物(ADCs)生物分析:拆解 “靶向导弹” 体内轨迹的核心技术
  • 深入解析:文献阅读 | iMetaMed | FigureYa:一个标准化可视化框架,用于增强生物医学数据解释和研究效率
  • spring boot方案利用Torna生成在线接口文档
  • C#关键字 unchecked与checked - 教程
  • 详细介绍:微服务的适用边界:从金融科技到量子计算的架构哲学
  • 使用IOT-Tree整合复杂计算模型(含AI模型),并对接现场设备优化控制(节能提效)技能方案
  • 单独
  • 为什么应该测试无JavaScript的页面体验
  • 完整教程:UE5小游戏开发 - 武士决斗
  • 经典三层架构项目(3-tier architecture)中应用依赖倒置原则(DIP)的理性权衡
  • PolarFire SOC Auto Update 和 IAP 文档阅读(三) AUTO UPDATE
  • 前台部分数据不显示
  • 指针定义以及二维数组内存地址(java/c++/python)
  • 一款不错的PDF工具,吾爱出品 - 教程
  • 解码数据结构线性表之顺序表
  • 中电金信:源启数据集成平台全新升级,实现便捷与性能双飞跃
  • Jupyter NoteBook / Jupyter Lab的安装与使用
  • 完整教程:科技的温情——挽救鼠鼠/兔兔的生命
  • 易基因:Nat Rev Drug Discov/IF101.8:何川团队顶刊综述RNA修饰系统作为疾病治疗靶点的研究进展
  • 国产适配 + AI 一键生成!亿图图示 14.5 全平台绘图指南:260 种图表 + Visio 兼容,开发者 / 办公党速藏
  • 关闭Cadence Allegro Design Entry CIS(OrCAD Capture)的Start Page
  • Mini L-CTF 2025 WP
  • K8S APIServer压力高,导致控制器Leader续约失败而重启问题
  • 【2025-09-24】连岳摘抄
  • 详细介绍:基于STM32F103C8T6与HC-08蓝牙模块实现手机连接方案