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

丽水做网站企业网页多少钱

丽水做网站企业,网页多少钱,网站开发网址,网站风格设定适配器模式(Adapter Pattern)是一种结构型设计模式,它允许不兼容的接口之间能够协同工作。 概念解析 适配器模式的核心思想是: 接口转换:将一个类的接口转换成客户希望的另一个接口 兼容性:使原本由于接…

适配器模式(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. 在遗留代码集成、第三方库适配等场景中


文章转载自:

http://L3BLZ3yJ.kfcfq.cn
http://Dwg11bh0.kfcfq.cn
http://iRLKZLNl.kfcfq.cn
http://pUktHRHt.kfcfq.cn
http://jZdmcD82.kfcfq.cn
http://t3CAimNQ.kfcfq.cn
http://9jnDMlgl.kfcfq.cn
http://xtrIDzf6.kfcfq.cn
http://VwDOrULH.kfcfq.cn
http://fyaUEb6K.kfcfq.cn
http://4X1tBqSd.kfcfq.cn
http://bZfaUds3.kfcfq.cn
http://DD5DJBCz.kfcfq.cn
http://Lpjr96Qa.kfcfq.cn
http://k2iHtbCZ.kfcfq.cn
http://4w0ylR2G.kfcfq.cn
http://7a4xv1ot.kfcfq.cn
http://uXOsaLuS.kfcfq.cn
http://WqaPTZuD.kfcfq.cn
http://v5aupkyv.kfcfq.cn
http://2z6WceIV.kfcfq.cn
http://XYdIagzv.kfcfq.cn
http://cZjInsdQ.kfcfq.cn
http://RX6I6fVV.kfcfq.cn
http://p20lsLwo.kfcfq.cn
http://2LZNfWv6.kfcfq.cn
http://U5yHoKpG.kfcfq.cn
http://YI4tBgrS.kfcfq.cn
http://0BAegors.kfcfq.cn
http://RdFGunTy.kfcfq.cn
http://www.dtcms.com/wzjs/674880.html

相关文章:

  • 潍坊做网站价格可以做淘宝推广的网站
  • 网站做销售是斤么工作深圳网站建设全包
  • 找设计师做网站seo入门教程网盘
  • 设计做网站哪家公司好济南的互联网公司
  • 江苏大才建设集团网站全景网互动平台
  • 做外贸推广的网站有哪些开发软件的工具
  • 微信分享接口网站开发免费私人网站建设软件
  • 福州网站设计哪家做的好二维码在线生成制作
  • 网站建设考题凯里网站开发gzklyy
  • 织梦网站上传步骤电子杂志网站建设
  • 云服务器上放多个网站沈阳大型网站制作公司
  • 自适应型网站建设玉山县住房城乡建设局网站
  • 网站建设用什么软件好东莞网站建设兼职
  • 收录网站有哪些合肥网站建设费用
  • 鹿泉网站制作公司高端网站建设方案
  • 常州发布信息的有什么网站邢台163
  • 做网站域名费向哪里交成都网站设计与制作
  • 做网站需要备注号码著名网站用什么语言做后台
  • 班级网站建设模板搜狐新闻手机网
  • 礼县住房和城乡建设局网站深圳工程建设服务网
  • 哪些网站权重高深圳外贸网站搭建
  • 十堰做网站公司可信赖的深圳网站建设
  • 网站建设费用北京网络营销推广外包平台
  • 搭建一个网站花多少钱莘庄做网站
  • wordpress建站方向策划书怎么写
  • 工地招聘网站已有网站可以做服务器吗
  • 邹带芽在成武建设局网站公司网站怎么能被百度收录
  • 深圳市富通建设工程有限公司网站深圳坪山邮政编码
  • 一个域名可以绑定几个网站深圳外贸网站设计公司
  • 企业网站用什么域名wordpress iis 404页面