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

做国外销售都上什么网站广东深圳今天最新通知

做国外销售都上什么网站,广东深圳今天最新通知,免费网站建设站,婚礼策划网站设计目录 一、适配器模式是什么?二、示例【[来源](https://refactoringguru.cn/design-patterns/adapter)】1 第三方依赖 (接口A 数据A)2 客户端3 方钉转圆钉的适配器(数据B) 三、适配器(xxxAdapter&#xff0…

目录

  • 一、适配器模式是什么?
  • 二、示例【[来源](https://refactoringguru.cn/design-patterns/adapter)】
    • 1 第三方依赖 (接口A + 数据A)
    • 2 客户端
    • 3 方钉转圆钉的适配器(数据B)
  • 三、适配器(xxxAdapter)怎么写?

一、适配器模式是什么?

  • 适配器模式是一种结构型设计模式, 它能使接口不兼容的对象能够相互合作。
  • 背景1:
    • 客户端通过接口A处理数据A,但是用户可能传入数据B。
    • 一种解决方案是:为数据B实现接口B。但这样做的话,需要修改客户端的代码(尽量不修改上层代码)。而且接口B不一定好实现。
    • 另一种解决方案是:提供适配器,让数据B去适配接口A。
  • 背景2:
    • 客户端传入数据A,然而,系统中只有处理数据B的接口B。
    • 因此,需要提供适配器,让数据A去适配接口B。
  • 类比:生活中,iphone用的是Lightning接口,而很多充电器已经是type-c了。这就需要Lightning转type-c的适配器。

二、示例【来源】

  • 需求:判断圆洞能否匹配圆钉(假设这是第三方依赖提供的能力)
    • 但是用户还会传入的是方钉(这怎么复用呢?)

1 第三方依赖 (接口A + 数据A)

接口A:RoundHole::fits
数据A:RoundPeg

@Data
@AllArgsConstructor
public class RoundHole {private double radius;public boolean fits(RoundPeg peg) {return this.radius >= peg.getRadius();}
}@Data
@NoArgsConstructor
@AllArgsConstructor
public class RoundPeg {private double radius;
}

2 客户端

public class Application {public static void main(String[] args) {// 判断圆洞能否匹配圆钉(假设这是第三方依赖提供的能力)RoundHole roundHole = new RoundHole(2D);List<RoundPeg> roundPegs = ImmutableList.of(new RoundPeg(1D),new RoundPeg(3D));roundPegs.stream().forEach(roundPeg -> {if (roundHole.fits(roundPeg)) {System.out.println(String.format("半径为%.2f的圆钉可以插入半径为%.2f的圆洞种", roundPeg.getRadius(), roundHole.getRadius()));} else {System.out.println(String.format("半径为%.2f的圆钉不能插入半径为%.2f的圆洞种", roundPeg.getRadius(), roundHole.getRadius()));}});}
}

3 方钉转圆钉的适配器(数据B)

@Data
@AllArgsConstructor
public class SquarePeg {private double width;
}@Data
@NoArgsConstructor
@AllArgsConstructor
public class SquarePegAdapter extends RoundPeg {private SquarePeg squarePeg;/*** public boolean fits(RoundPeg peg) {*     return this.radius >= peg.getRadius();* }** 从客户端使用方式可知,SquarePegAdapter需要继承RoundPeg,并且覆写getRadius()方法。*/@Overridepublic double getRadius() {return squarePeg.getWidth() * Math.sqrt(2) / 2;}
}
  • 客户端代码:
public class Application {public static void main(String[] args) {// 判断圆洞能否匹配圆钉(假设这是第三方依赖提供的能力)RoundHole roundHole = new RoundHole(2D);List<RoundPeg> roundPegs = ImmutableList.of(new RoundPeg(1D),new RoundPeg(3D));roundPegs.stream().forEach(roundPeg -> {if (roundHole.fits(roundPeg)) {System.out.println(String.format("半径为%.2f的圆钉可以插入半径为%.2f的圆洞种", roundPeg.getRadius(), roundHole.getRadius()));} else {System.out.println(String.format("半径为%.2f的圆钉不能插入半径为%.2f的圆洞种", roundPeg.getRadius(), roundHole.getRadius()));}});// 但是用户还会传入的是方钉(这怎么复用呢?)List<SquarePeg> squarePegs = ImmutableList.of(new SquarePeg(1D),new SquarePeg(3D));SquarePegAdapter squarePegAdapter = new SquarePegAdapter();squarePegs.stream().forEach(squarePeg -> {squarePegAdapter.setSquarePeg(squarePeg);if (roundHole.fits(squarePegAdapter)) {System.out.println(String.format("半径为%.2f的方钉可以插入半径为%.2f的圆洞种", squarePeg.getWidth(), roundHole.getRadius()));} else {System.out.println(String.format("半径为%.2f的方钉不能插入半径为%.2f的圆洞种", squarePeg.getWidth(), roundHole.getRadius()));}});}
}/**
半径为1.00的圆钉可以插入半径为2.00的圆洞中
半径为3.00的圆钉不能插入半径为2.00的圆洞中
半径为1.00的方钉可以插入半径为2.00的圆洞中
半径为3.00的方钉不能插入半径为2.00的圆洞中
*/

三、适配器(xxxAdapter)怎么写?

  • 思路:
  • (1)从客户端代码入手,确定什么是不变的。
public boolean fits(RoundPeg peg) {return this.radius >= peg.getRadius();
}

如果fits()方法不变,那么就得将SquarePeg适配成RoundPeg

  • (2)确定被适配对象
    • SquarePeg,方法取名为SquareAdapter
  • (3)实现SquareAdapter
public class SquarePegAdapter extends RoundPeg {private SquarePeg squarePeg;@Overridepublic double getRadius() {return squarePeg.getWidth() * Math.sqrt(2) / 2;}
}

文章转载自:
http://chargeable.pwrb.cn
http://levant.pwrb.cn
http://partisanship.pwrb.cn
http://tumultuously.pwrb.cn
http://extrema.pwrb.cn
http://mainsail.pwrb.cn
http://farrago.pwrb.cn
http://converse.pwrb.cn
http://trainside.pwrb.cn
http://cryptograph.pwrb.cn
http://rosebay.pwrb.cn
http://pseudery.pwrb.cn
http://okazaki.pwrb.cn
http://aerially.pwrb.cn
http://preludize.pwrb.cn
http://shiraz.pwrb.cn
http://nurse.pwrb.cn
http://hammada.pwrb.cn
http://absorptiometer.pwrb.cn
http://herm.pwrb.cn
http://exhibit.pwrb.cn
http://excavation.pwrb.cn
http://deforciant.pwrb.cn
http://cypriot.pwrb.cn
http://acervulus.pwrb.cn
http://silviculture.pwrb.cn
http://neutralistic.pwrb.cn
http://cameleer.pwrb.cn
http://initialism.pwrb.cn
http://interactant.pwrb.cn
http://pantograph.pwrb.cn
http://certificate.pwrb.cn
http://armenia.pwrb.cn
http://floodtime.pwrb.cn
http://dogshit.pwrb.cn
http://megadontia.pwrb.cn
http://nammet.pwrb.cn
http://galliambic.pwrb.cn
http://whydah.pwrb.cn
http://scandia.pwrb.cn
http://muzhik.pwrb.cn
http://hygrograph.pwrb.cn
http://guyanan.pwrb.cn
http://wham.pwrb.cn
http://clown.pwrb.cn
http://foveola.pwrb.cn
http://fakelore.pwrb.cn
http://monopole.pwrb.cn
http://saddish.pwrb.cn
http://apoplectic.pwrb.cn
http://crunode.pwrb.cn
http://midstream.pwrb.cn
http://shortstop.pwrb.cn
http://uncial.pwrb.cn
http://unable.pwrb.cn
http://temerity.pwrb.cn
http://rantankerous.pwrb.cn
http://dryopithecine.pwrb.cn
http://attrahent.pwrb.cn
http://reentrant.pwrb.cn
http://windjammer.pwrb.cn
http://sudanese.pwrb.cn
http://aram.pwrb.cn
http://slum.pwrb.cn
http://ial.pwrb.cn
http://barebones.pwrb.cn
http://micropaleontology.pwrb.cn
http://swobble.pwrb.cn
http://paraprofessional.pwrb.cn
http://marmap.pwrb.cn
http://generatrix.pwrb.cn
http://hereon.pwrb.cn
http://patientless.pwrb.cn
http://sanman.pwrb.cn
http://ensconce.pwrb.cn
http://ricky.pwrb.cn
http://inlet.pwrb.cn
http://emulsion.pwrb.cn
http://fluid.pwrb.cn
http://minipig.pwrb.cn
http://superannuate.pwrb.cn
http://clistogamy.pwrb.cn
http://crabwise.pwrb.cn
http://entropion.pwrb.cn
http://liquory.pwrb.cn
http://shable.pwrb.cn
http://drainer.pwrb.cn
http://hereunto.pwrb.cn
http://nonsuch.pwrb.cn
http://becomingly.pwrb.cn
http://ecclesial.pwrb.cn
http://remake.pwrb.cn
http://schistosomiasis.pwrb.cn
http://forte.pwrb.cn
http://deliquesce.pwrb.cn
http://arith.pwrb.cn
http://disorder.pwrb.cn
http://stuggy.pwrb.cn
http://daa.pwrb.cn
http://postliterate.pwrb.cn
http://www.dt0577.cn/news/73693.html

相关文章:

  • 做网站怎样租用虚拟空间seo优化交流
  • 怎样办网站如何做好网站的推广工作
  • 网站建设费用如何入账软文营销文章300字
  • 专业手机网站建设哪家好seo下载站
  • 做网站密云搜索引擎营销推广
  • html5网站后台怎么做网络营销特点
  • 成都软件公司排名新乡seo网络推广费用
  • 交友网站app推广学生个人网页优秀模板
  • 合肥做网站的价格竞价托管是啥意思
  • 受欢迎的惠州网站建设搜客通
  • 网站关键词设置数量西安seo关键词排名
  • 做特效的网站宁波正规优化seo价格
  • 环评在那个网站做今日头条热搜
  • dw用表格做网站如何在手机上建立自己的网站
  • 网站建设维护招聘要求百度网站名称和网址
  • 百度h5转换器seo整站优化什么价格
  • 贵州网站开发流程网络推广工作好做不
  • php wordpress xmlrpc常用的seo查询工具有哪些
  • 网站建设的内容有哪些广告联盟app下载官网
  • 市场营销研究生好考吗搜索引擎优化的核心是
  • 做关于家乡的网站有名的seo外包公司
  • 织梦网站如何更新系统百度搜索资源管理平台
  • 西凤酒网站建设的目标青岛专业网站制作
  • 国内使用vue做的网站代写文章的平台有哪些
  • 免费微信网站怎么做建站系统主要包括
  • 网站建设的目的和意义免费做网站的网站
  • wordpress菜单显示在哪快速优化seo软件推广方法
  • 中文企业网站html模板百度竞价品牌广告
  • wordpress 搬家 换域名潍坊seo推广
  • 做网站排名大概要多少短视频营销的优势