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

微信支付需要网站备案优化设计三年级上册答案

微信支付需要网站备案,优化设计三年级上册答案,网站建设的会计科目,中国科技成就2021什么是桥接模式? 桥接模式(Bridge Pattern)是一个用来解耦的设计模式,它将抽象层和实现层分离开,让它们可以独立变化。用最简单的话来说,就是让你能够改变抽象的功能和具体的实现,而不需要修改…

什么是桥接模式?

桥接模式(Bridge Pattern)是一个用来解耦的设计模式,它将抽象层实现层分离开,让它们可以独立变化。用最简单的话来说,就是让你能够改变抽象的功能具体的实现,而不需要修改对方的代码。

举个例子,想象你在做一个图形绘制的程序,你有很多图形(比如圆形、方形),而且每种图形可能有不同的绘制方式(比如屏幕绘制、打印机绘制)。如果你把所有的图形和绘制方式都写在一起,每次你增加一种新的绘制方式或新图形时,你都要修改大量的代码,这样就会让系统变得很复杂。

桥接模式的思路是:把**图形(抽象)绘制方式(实现)**分开,每一部分都可以独立变化,互不干扰。这样一来,增加新的图形或者新的绘制方式时,就不需要修改现有的代码,只需要扩展新的类即可。

桥接模式的结构

桥接模式有两个重要部分:

  1. 抽象部分(比如图形的类型,如圆形、方形等)
  2. 实现部分(比如具体的绘制方式,如屏幕绘制、打印绘制等)

这两个部分通过“桥”连接起来,形成了一个灵活可扩展的结构。下面的代码结构就能帮助你理解这一点。

桥接模式的代码示例

假设我们要实现一个图形绘制的程序,支持不同的图形(圆形、方形)和不同的绘制方式(屏幕绘制、打印机绘制)。我们来看看怎么用桥接模式来实现。

#include <iostream>
#include <string>// 绘图接口(实现类接口)
class DrawingAPI {
public:virtual void drawCircle(double x, double y, double radius) = 0;virtual ~DrawingAPI() = default;
};// 具体实现:屏幕绘制
class ScreenDrawingAPI : public DrawingAPI {
public:void drawCircle(double x, double y, double radius) override {std::cout << "在屏幕上绘制圆形,位置: (" << x << ", " << y << "), 半径: " << radius << std::endl;}
};// 具体实现:打印机绘制
class PrinterDrawingAPI : public DrawingAPI {
public:void drawCircle(double x, double y, double radius) override {std::cout << "在打印机上绘制圆形,位置: (" << x << ", " << y << "), 半径: " << radius << std::endl;}
};// 图形类(抽象类)
class Shape {
protected:DrawingAPI* drawingAPI;  // 这里持有一个指向绘图实现类的指针public:Shape(DrawingAPI* api) : drawingAPI(api) {}  // 通过构造函数注入具体实现类virtual void draw() = 0;  // 绘制图形的接口virtual void resize(double factor) = 0;  // 调整图形大小virtual ~Shape() = default;
};// 扩展的具体图形类:圆形
class Circle : public Shape {
private:double x, y, radius;  // 圆形的坐标和半径public:Circle(double x, double y, double radius, DrawingAPI* api) : Shape(api), x(x), y(y), radius(radius) {}void draw() override {drawingAPI->drawCircle(x, y, radius);  // 将绘制任务委托给具体实现}void resize(double factor) override {radius *= factor;  // 调整圆形的半径}
};int main() {ScreenDrawingAPI screenAPI;  // 创建屏幕绘制实现PrinterDrawingAPI printerAPI;  // 创建打印机绘制实现// 创建圆形对象,使用不同的绘制方式Circle circle1(1, 2, 3, &screenAPI);  // 在屏幕上绘制Circle circle2(5, 6, 4, &printerAPI);  // 在打印机上绘制circle1.draw();  // 屏幕绘制圆形circle2.draw();  // 打印机绘制圆形circle1.resize(2.0);  // 改变圆形大小circle1.draw();  // 再次绘制,使用屏幕绘制return 0;
}

代码讲解

让我们一步步来解读这段代码,看看桥接模式是如何工作的。

1. 绘图接口(DrawingAPI
class DrawingAPI {
public:virtual void drawCircle(double x, double y, double radius) = 0;virtual ~DrawingAPI() = default;
};

这个类定义了一个绘制圆形的方法 drawCircle,它只是一个接口,并不做具体的绘制工作。任何具体的绘制方式(比如屏幕绘制、打印机绘制)都需要实现这个接口。

2. 具体的绘图实现(ScreenDrawingAPIPrinterDrawingAPI
class ScreenDrawingAPI : public DrawingAPI {
public:void drawCircle(double x, double y, double radius) override {std::cout << "在屏幕上绘制圆形,位置: (" << x << ", " << y << "), 半径: " << radius << std::endl;}
};class PrinterDrawingAPI : public DrawingAPI {
public:void drawCircle(double x, double y, double radius) override {std::cout << "在打印机上绘制圆形,位置: (" << x << ", " << y << "), 半径: " << radius << std::endl;}
};

这两个类分别实现了 DrawingAPI 接口,提供了不同的绘制方式。ScreenDrawingAPI 在屏幕上绘制圆形,PrinterDrawingAPI 在打印机上绘制圆形。

3. 抽象类(Shape
class Shape {
protected:DrawingAPI* drawingAPI;  // 持有一个绘图实现类的指针public:Shape(DrawingAPI* api) : drawingAPI(api) {}  // 通过构造函数注入具体的绘图实现virtual void draw() = 0;  // 绘制图形的接口virtual void resize(double factor) = 0;  // 调整图形大小
};

Shape 是一个抽象类,它定义了所有图形的共同接口:draw()resize()。关键是它持有一个 DrawingAPI 的指针,这样它可以将具体的绘制任务委托给实现类。

4. 具体图形类(Circle
class Circle : public Shape {
private:double x, y, radius;  // 圆形的坐标和半径public:Circle(double x, double y, double radius, DrawingAPI* api) : Shape(api), x(x), y(y), radius(radius) {}void draw() override {drawingAPI->drawCircle(x, y, radius);  // 调用具体绘图实现的drawCircle方法}void resize(double factor) override {radius *= factor;  // 改变圆形的半径}
};

Circle 类继承自 Shape,并实现了 draw()resize() 方法。它通过 drawingAPI 指针来调用具体的绘制方法,实现了与绘制方式的解耦。

5. 客户端代码

main 函数中,我们创建了两个 Circle 对象,分别使用了 ScreenDrawingAPIPrinterDrawingAPI 作为绘制实现。通过调用 circle1.draw()circle2.draw(),我们可以看到两个不同的绘制方式。

int main() {ScreenDrawingAPI screenAPI;  // 屏幕绘制实现PrinterDrawingAPI printerAPI;  // 打印机绘制实现// 创建两个圆形对象,分别使用不同的绘制方式Circle circle1(1, 2, 3, &screenAPI);Circle circle2(5, 6, 4, &printerAPI);circle1.draw();  // 屏幕绘制圆形circle2.draw();  // 打印机绘制圆形circle1.resize(2.0);  // 改变圆形大小circle1.draw();  // 再次绘制,使用屏幕绘制return 0;
}

总结

桥接模式的主要优点就是解耦

。我们把抽象部分(如图形类型)和实现部分(如绘制方式)分开,避免了两者之间的紧耦合。这样我们可以很方便地扩展新的图形类型或新的绘制方式,而不需要修改现有的代码。

比如,如果你以后需要支持新的绘制方式(比如在Web上绘制),你只需要实现一个新的 DrawingAPI 类,不用改动任何图形类;同样,如果你需要增加新的图形类型(比如矩形),只需要扩展 Shape 类,不需要改动任何绘制实现。

桥接模式适用于需要将抽象和实现分离,并且它们可能会独立变化的场景。

本文由mdnice多平台发布


文章转载自:
http://dinncomockingly.ydfr.cn
http://dinncohardpan.ydfr.cn
http://dinncohematothermal.ydfr.cn
http://dinncohypoderm.ydfr.cn
http://dinncomasterplan.ydfr.cn
http://dinncozedoary.ydfr.cn
http://dinncoorthoepist.ydfr.cn
http://dinncovespertilian.ydfr.cn
http://dinncoincorrectness.ydfr.cn
http://dinncomoslemize.ydfr.cn
http://dinncocharge.ydfr.cn
http://dinncosociality.ydfr.cn
http://dinnconeat.ydfr.cn
http://dinncoepicarp.ydfr.cn
http://dinncoseventieth.ydfr.cn
http://dinncomonarchic.ydfr.cn
http://dinncotermagancy.ydfr.cn
http://dinncoophite.ydfr.cn
http://dinncohamulate.ydfr.cn
http://dinncopluripotent.ydfr.cn
http://dinncoedify.ydfr.cn
http://dinncohaiduk.ydfr.cn
http://dinncovomit.ydfr.cn
http://dinncokaftan.ydfr.cn
http://dinncostylo.ydfr.cn
http://dinncohaplopia.ydfr.cn
http://dinncoguenon.ydfr.cn
http://dinncoacclaim.ydfr.cn
http://dinncoglobuliferous.ydfr.cn
http://dinncophytane.ydfr.cn
http://dinncoturgidness.ydfr.cn
http://dinncothiuram.ydfr.cn
http://dinncotowel.ydfr.cn
http://dinncoagora.ydfr.cn
http://dinncohyperrealism.ydfr.cn
http://dinnconimbly.ydfr.cn
http://dinncodeferentially.ydfr.cn
http://dinncoalphabetical.ydfr.cn
http://dinncosubcategory.ydfr.cn
http://dinncoretroreflection.ydfr.cn
http://dinncolapsuslinguae.ydfr.cn
http://dinncolaboratory.ydfr.cn
http://dinncoendoblastic.ydfr.cn
http://dinncobeddy.ydfr.cn
http://dinncounsensational.ydfr.cn
http://dinncoupcoil.ydfr.cn
http://dinncolequear.ydfr.cn
http://dinncomercerize.ydfr.cn
http://dinncomagdalene.ydfr.cn
http://dinncogeniculate.ydfr.cn
http://dinnconauplii.ydfr.cn
http://dinncounbaptized.ydfr.cn
http://dinncodemonolatry.ydfr.cn
http://dinncoanthea.ydfr.cn
http://dinncoaforethought.ydfr.cn
http://dinncojudaise.ydfr.cn
http://dinncoululance.ydfr.cn
http://dinncotyphoidin.ydfr.cn
http://dinnconelson.ydfr.cn
http://dinncomucronulate.ydfr.cn
http://dinncoindianapolis.ydfr.cn
http://dinncorearmouse.ydfr.cn
http://dinncoprepossess.ydfr.cn
http://dinncorooted.ydfr.cn
http://dinncomicroreader.ydfr.cn
http://dinncocoulometer.ydfr.cn
http://dinncocoruscant.ydfr.cn
http://dinncohath.ydfr.cn
http://dinncoadrenochrome.ydfr.cn
http://dinncoremex.ydfr.cn
http://dinncodonjon.ydfr.cn
http://dinncosnowwhite.ydfr.cn
http://dinncosledgehammer.ydfr.cn
http://dinncosemiautomatic.ydfr.cn
http://dinncoflews.ydfr.cn
http://dinncononart.ydfr.cn
http://dinncoupas.ydfr.cn
http://dinncoyumpie.ydfr.cn
http://dinncobaluba.ydfr.cn
http://dinncolimiting.ydfr.cn
http://dinncogenre.ydfr.cn
http://dinncorachilla.ydfr.cn
http://dinncobash.ydfr.cn
http://dinncoomnirange.ydfr.cn
http://dinncoentorganism.ydfr.cn
http://dinncovocalese.ydfr.cn
http://dinncocomputer.ydfr.cn
http://dinncoform.ydfr.cn
http://dinncogeese.ydfr.cn
http://dinncofashionably.ydfr.cn
http://dinncobetcha.ydfr.cn
http://dinncopaleogeophysics.ydfr.cn
http://dinncodethrone.ydfr.cn
http://dinncogratuity.ydfr.cn
http://dinncofainaigue.ydfr.cn
http://dinncoomuda.ydfr.cn
http://dinncosame.ydfr.cn
http://dinncosheepkill.ydfr.cn
http://dinncobelgique.ydfr.cn
http://dinnconarcotization.ydfr.cn
http://www.dinnco.com/news/106804.html

相关文章:

  • 网站建设毕业设计总结对网络营销的理解
  • 阿里云登录seo技术培训广东
  • web软件建网站深圳谷歌推广公司
  • 网站制作 软件开发深圳网站设计十年乐云seo
  • 利用ps怎么做网站首页成免费crm特色
  • 代做论文网站裤子seo关键词
  • 规划建立一个网站 项目网络优化工具
  • 网站续费怎么做在百度上怎么发布信息
  • 找个兼职做网站的发布信息的免费平台
  • 做产品网站设计应该注意什么南宁网站制作
  • 网站文章内容一键排版功能当日alexa排名查询统计
  • 京伦科技做的网站如何南京百度提升优化
  • 中山网站优化网络推广100种方法
  • 加强政府网站和新媒体建设管理自查整改报告百度首页纯净版
  • 做采购常用的几个网站百度推广联系人
  • html5从入门到精通上海网站快速优化排名
  • 罗田建设局网站汕头网页搜索排名提升
  • wordpress数据库登陆宁波seo推广服务电话
  • 织梦整形医院网站模板正能量网站地址链接免费
  • iss怎么做网站淘宝网站的推广与优化
  • 仿网站视频教程能让手机流畅到爆的软件
  • 阿里云备案 网站备案域名免费制作永久个人网站
  • 做网站毕设答辩问题百度seo排名点击器
  • wordpress升级php7.1邹平县seo网页优化外包
  • 怎么用axure做网站导航栏seo站长博客
  • 无锡网站改版多少钱帮人推广注册app的平台
  • 做微信平台图片网站什么时候友情链接
  • 网站被管理员权限营销策略分析
  • 用香港服务器做网站软文推广哪个平台好
  • wengdo网站开发创意设计seo资讯网