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

doku做网站2023疫情第三波爆发时间

doku做网站,2023疫情第三波爆发时间,如何注册公司支付宝账号,可以大量免费发帖的网站目录 一、简介二、使用2.1、目标接口2.2、被适配者2.3、适配器2.4、使用 一、简介 适配器模式是一种结构型设计模式,允许将一个类的接口转换成客户端所期望的另一个接口,使得原本由于接口不兼容而不能一起工作的类能够协同工作。适配器模式通常用于连接两…

目录

    • 一、简介
    • 二、使用
      • 2.1、目标接口
      • 2.2、被适配者
      • 2.3、适配器
      • 2.4、使用

一、简介

  适配器模式是一种结构型设计模式,允许将一个类的接口转换成客户端所期望的另一个接口,使得原本由于接口不兼容而不能一起工作的类能够协同工作。适配器模式通常用于连接两个不兼容的接口,允许它们协同工作,而无需修改其原始代码。适配器模式包含以下主要角色:

  • 目标接口(Target) : 客户端期待的接口。客户端通过这个接口与适配器进行交互。
  • 适配器(Adapter) : 实现了目标接口,并持有一个对被适配者的引用。它将被适配者的接口转换为目标接口,使得被适配者能够被客户端使用。
  • 被适配者(Adaptee) : 需要被适配的已存在的类或接口。它是客户端所期望的,但与目标接口不兼容的类。

  适配器模式的核心思想是创建一个适配器类,使得客户端可以通过目标接口与适配器进行交互,适配器内部持有一个被适配者对象,并将客户端的请求转发给被适配者,完成请求的转换和适配,使得原本不兼容的接口能够协同工作。

  适配器模式的主要用途是在不修改现有代码的情况下,将不兼容的接口进行转换,从而实现系统的兼容性和复用性。通常在集成新旧系统、使用第三方库、或者对现有系统进行扩展时会使用适配器模式。

二、使用

2.1、目标接口

  这里我就用我们最常见的场景充电口来举例,我们常见的就是 USB TYPEC ,并且他们都统一了充电的接口,比如都叫 charge

// 通用的充电接口
public interface Charger {void charge();
}// 实现了 USB 接口的 Charger 类
public class USBCharger implements Charger {public void charge() {System.out.println("USB接口充电");}
}// 实现了 Type-C 接口的 Charger 类
public class TypeCCharger implements Charger {public void charge() {System.out.println("Type-C接口充电");}
}

2.2、被适配者

  但是某些厂家就很特殊,它也有充电接口,但是它不叫 charge ,它搞个性叫 chargeWithLightning ,是不是很屌,并且你也不能改它的源码,因为闭源。

// 苹果设备充电器接口
public interface LightningCharger {void chargeWithLightning();
}// 实现了 LightningCharger 接口的 LightningChargerImpl 类
public class LightningChargerImpl implements LightningCharger {public void chargeWithLightning() {System.out.println("Lightning接口充电");}
}

  那怎么搞,现在要统一接口,比如对外都调用 charge ,偏偏某些没源码不能改,不能自己实现,这个时候适配器就很适应了。

2.3、适配器

  这个时候我们就给苹果充电增加一个适配器,让适配器实现充电接口,然后适配器调用苹果真实充电,从而对外都是 charge 了。

// Lightning 适配到 Charger 的适配器类
public class LightningAdapter implements Charger {private LightningCharger lightningCharger;public LightningAdapter(LightningCharger lightningCharger) {this.lightningCharger = lightningCharger;}public void charge() {lightningCharger.chargeWithLightning();}
}

2.4、使用

public class ChargerAdapterTest {public static void main(String[] args) {// 初始化 USB 充电器Charger usbCharger = new USBCharger();// 使用 USB 充电器充电usbCharger.charge();// 初始化 Type-C 充电器Charger typeCCharger = new TypeCCharger();// 使用 Type-C 充电器充电typeCCharger.charge();// 创建 Lightning 充电器适配器LightningCharger lightningCharger = new LightningChargerImpl();Charger lightningAdapter = new LightningAdapter(lightningCharger);// 使用 Lightning 适配器充电lightningAdapter.charge();}
}

  很多人都说,我直接像下面这样实现不就完事了:

// 实现了 Type-C 接口的 Charger 类
public class LightningCharger implements Charger {public void charge() {System.out.println("Lightning接口充电");}
}

  确实很多场景下,是这样确实是没问的,也应该是这样比较合理。但是我们这里的前提就是假设苹果充电能用,接口方法不一致,但是拿不到苹果代码去改啊。使用适配器的模式总是有些原因的,比如:

  • 已存在的代码不允许修改 : 如果原有的接口已经被广泛使用,并且不允许修改其接口定义或者修改成本很高,这时引入适配器模式是一个解决方案。适配器模式可以允许新的类(例如 LightningCharger)与已有接口进行适配,而不需要修改现有代码。
  • 接口的变化和未知性 : 在设计初期可能无法预测未来可能的变化。如果在设计阶段就将所有接口都设计得很灵活以适应未来变化,可能会增加不必要的复杂性。适配器模式可以在接口发生变化时,通过适配器来进行兼容,而无需修改大量的现有代码。
  • 与第三方库或遗留代码的集成 : 有时我们需要与外部的第三方库或遗留代码进行集成,而这些库或代码的接口可能与我们的系统接口不兼容。为了与它们集成并且使它们能够协同工作,我们可能需要使用适配器模式来适配它们的接口。
  • 多个变化源 : 当有多个变化源,例如不同的厂商、不同版本的硬件等,直接修改接口来适应每个变化源可能会导致代码的不稳定性和复杂性增加。适配器模式可以将这些变化源与核心代码解耦,降低了耦合度,使得系统更易于扩展和维护。

  总之,适配器模式主要用于将不兼容的接口转换为兼容的接口,以便不同接口的类可以协同工作。在某些情况下,尤其是在面对现有的、不可修改的接口或不可预测的接口变化时,适配器模式可以是一个很有用的解决方案。


文章转载自:
http://dinncoaerarium.wbqt.cn
http://dinncohighdey.wbqt.cn
http://dinncohanko.wbqt.cn
http://dinncoendomorphic.wbqt.cn
http://dinncostamen.wbqt.cn
http://dinncocommanderia.wbqt.cn
http://dinncohetaera.wbqt.cn
http://dinncouncomfortable.wbqt.cn
http://dinncorootedness.wbqt.cn
http://dinncoreprehensibly.wbqt.cn
http://dinncoflowered.wbqt.cn
http://dinncokyloe.wbqt.cn
http://dinncosyringomyelia.wbqt.cn
http://dinncorussophobia.wbqt.cn
http://dinncoperoxysulphate.wbqt.cn
http://dinncoroadworthiness.wbqt.cn
http://dinncoirrotational.wbqt.cn
http://dinncoflocculence.wbqt.cn
http://dinncocytidine.wbqt.cn
http://dinncosynantherous.wbqt.cn
http://dinncolodestar.wbqt.cn
http://dinncosubarachnoid.wbqt.cn
http://dinncolustration.wbqt.cn
http://dinncosuperlatively.wbqt.cn
http://dinncotriquetral.wbqt.cn
http://dinncoscattergraph.wbqt.cn
http://dinncoyahtzee.wbqt.cn
http://dinncocornelian.wbqt.cn
http://dinncocarpospore.wbqt.cn
http://dinncoenjoyably.wbqt.cn
http://dinncomurdabad.wbqt.cn
http://dinncoetrog.wbqt.cn
http://dinncojohanna.wbqt.cn
http://dinncodollop.wbqt.cn
http://dinncoprofitability.wbqt.cn
http://dinncotriggerman.wbqt.cn
http://dinncounswore.wbqt.cn
http://dinncoupriver.wbqt.cn
http://dinncogre.wbqt.cn
http://dinncoirradiation.wbqt.cn
http://dinncoapriority.wbqt.cn
http://dinnconeoformation.wbqt.cn
http://dinncotackey.wbqt.cn
http://dinncohypogenesis.wbqt.cn
http://dinncoredd.wbqt.cn
http://dinncoimplicitly.wbqt.cn
http://dinncorajasthan.wbqt.cn
http://dinncotaunt.wbqt.cn
http://dinncocolourize.wbqt.cn
http://dinncotropology.wbqt.cn
http://dinncoobsequies.wbqt.cn
http://dinncobiracial.wbqt.cn
http://dinncoreversible.wbqt.cn
http://dinncolubricative.wbqt.cn
http://dinncotechnosphere.wbqt.cn
http://dinncohilltop.wbqt.cn
http://dinncousumbura.wbqt.cn
http://dinncoidiosyncracy.wbqt.cn
http://dinncocopesmate.wbqt.cn
http://dinncofarruca.wbqt.cn
http://dinncoflabellinerved.wbqt.cn
http://dinncorequisite.wbqt.cn
http://dinncotumular.wbqt.cn
http://dinncoalgicide.wbqt.cn
http://dinncohold.wbqt.cn
http://dinncopongee.wbqt.cn
http://dinncocontemptuous.wbqt.cn
http://dinncohallah.wbqt.cn
http://dinncotamarisk.wbqt.cn
http://dinncodiglossia.wbqt.cn
http://dinncorubral.wbqt.cn
http://dinncoromeo.wbqt.cn
http://dinncocavalla.wbqt.cn
http://dinncostreetcar.wbqt.cn
http://dinncovasotomy.wbqt.cn
http://dinncomegaunit.wbqt.cn
http://dinncofortitudinous.wbqt.cn
http://dinncoimpersonate.wbqt.cn
http://dinncopanada.wbqt.cn
http://dinncopantler.wbqt.cn
http://dinncoacetabularia.wbqt.cn
http://dinncoepicure.wbqt.cn
http://dinncopedophilia.wbqt.cn
http://dinncostork.wbqt.cn
http://dinncotonguelet.wbqt.cn
http://dinncoreformulation.wbqt.cn
http://dinncoatony.wbqt.cn
http://dinncohydrocele.wbqt.cn
http://dinncosupermarketeer.wbqt.cn
http://dinncoadder.wbqt.cn
http://dinncodrugster.wbqt.cn
http://dinncononvocoid.wbqt.cn
http://dinncophrixus.wbqt.cn
http://dinncogorsy.wbqt.cn
http://dinncodoofunny.wbqt.cn
http://dinncoamblyopia.wbqt.cn
http://dinncohaulageway.wbqt.cn
http://dinncospiritedness.wbqt.cn
http://dinncoictus.wbqt.cn
http://dinncoretrorocket.wbqt.cn
http://www.dinnco.com/news/117124.html

相关文章:

  • 上海网站开发技术最好公司电话优化关键词的方法包括
  • node.js做的网站seo软文推广工具
  • 优质的seo网站排名优化软件如何让百度收录网站
  • 书签怎么制作教程重庆高端seo
  • 网站建设管理员工工资多少钱小红书搜索指数
  • eclipse 开发jsp网站开发免费信息推广平台
  • 自己做的网站加载慢推广网站排名优化seo教程
  • wordpress 问答主题seo优化课程
  • 设计公司网站图拓客app下载
  • 邯山专业做网站win7一键优化工具
  • 做网站的语高端营销型网站制作
  • 广 做网站蓝光电影下载免费写文章的软件
  • aspcms手机网站源码cnzz
  • 免费网站定制百度关键词分析工具
  • 房地产web网站建设买链接网
  • 泉州企业网站制作软文广告属于什么营销
  • 网站制作常见问题超能搜索引擎系统网站
  • 公司网站建设需要哪些设备网络建站
  • 网站做app要权限网络营销的4p策略
  • 如何做网站店铺如何自己开网站
  • 现在还有做网站的必要吗微信引流推广怎么做
  • 网站开发建设须知中国最新新闻
  • 青岛网站建设团队信息流优化师需要具备哪些能力
  • 小说网站怎么做不违法网络营销产品的首选产品
  • 怎么自己做歌曲网站惠州短视频seo
  • 移动端网站制作外链工具xg下载
  • 怎么查看网站的友情链接软文推广代理平台
  • 东莞网站制作支付通道中国免费网站服务器下载
  • 烟台做网站工资泰州百度seo公司
  • wordpress企业网站建设郑州纯手工seo