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

python做网站快么成都公司建站模板

python做网站快么,成都公司建站模板,手机制作网站的软件有哪些内容,网站能给企业带来什么前言 在软件设计中,适配器模式(Adapter Pattern)是一种结构型设计模式,旨在使不兼容的接口能够协同工作。它通过引入一个适配器类,帮助两个接口之间进行适配,使得它们能够互相操作。本文将详细介绍适配器模…

前言

在软件设计中,适配器模式(Adapter Pattern)是一种结构型设计模式,旨在使不兼容的接口能够协同工作。它通过引入一个适配器类,帮助两个接口之间进行适配,使得它们能够互相操作。本文将详细介绍适配器模式的定义、使用场景、实现方式、接口适配器以及其优缺点。

一、适配器模式的定义

适配器模式通过引入一个适配器类,将一个类的接口转换成客户端所期望的另一种接口。适配器模式的核心在于“适配”,它允许原本由于接口不兼容而无法一起工作的类能够协同工作。

主要角色

  1. 目标接口(Target):目标接口是客户端所期望的接口,定义了客户端需要的方法。客户端代码依赖于目标接口而不是具体的实现类,这样可以使系统更加灵活和可扩展。
  2. 适配者类(Adaptee):源接口是现有的接口,它的接口与目标接口不一致。源接口包含了客户端需要的实际功能,但其接口形式无法直接被客户端使用。
  3. 适配器类(Adapter):适配器实现目标接口,并持有源接口的实例(对象适配器)或继承源接口(类适配器)。适配器通过在实现目标接口的方法中调用源接口的方法,将源接口的方法转换成目标接口的方法。

三者之间的关系

  • 客户端依赖目标接口进行编程。
  • 适配器实现了目标接口,并通过组合或继承的方式调用源接口的方法。
  • 适配器将源接口的方法适配成目标接口的方法,从而使得客户端可以无缝地使用源接口的功能。

二、适配器模式的使用场景

适配器模式适用于以下情况:

  1. 系统需要使用现有的类,但接口不符合要求:如果你有一个类,它的接口与系统的需求不匹配,可以通过适配器模式进行转换。
  2. 需要使用多个现有的类,但接口不一致:如果系统中需要整合多个不兼容的接口,可以通过适配器模式使其兼容。
  3. 系统中使用第三方库或框架:当你引入第三方库或框架时,可能会遇到接口不一致的情况,适配器模式可以帮助解决这些问题。

三、适配器模式的实现方式

适配器模式有三种主要的实现方式:类适配器,对象适配器和接口适配器

本文讲解的是类适配器

类适配器

特点

  • 使用继承:类适配器通过继承源类来实现适配功能。

  • 单一适配:由于 Java 中不支持多重继承,类适配器只能适配一个源类。

优缺点

优点
  1. 简单实现
    • 由于类适配器通过继承实现,它可以直接访问被适配类(Adaptee)的所有方法。这使得实现适配器时相对简单,不需要额外的委托逻辑。
  2. 提高代码的可复用性
    • 适配器模式通过继承实现,使得子类能够继承父类的所有功能,从而提高了代码的复用性。
  3. 可以重定义被适配类的一些行为
    • 通过继承,可以在适配器类中重定义被适配类的一些方法,实现更加灵活的适配。
缺点
  1. 受限于单继承
    • 类适配器模式在Java等单继承语言中有一个显著的缺点,即一个类只能继承一个父类。这意味着如果适配器类已经有一个父类,就不能再使用类适配器模式来继承另一个类。
  2. 高耦合
    • 适配器类和被适配类之间的耦合度较高,因为适配器类直接继承了被适配类的实现。如果被适配类发生变化,适配器类可能也需要进行相应的修改。
  3. 不符合“组合优于继承”原则
    • 面向对象设计中,组合优于继承的原则提倡使用组合来代替继承,以降低类之间的耦合度。类适配器模式违背了这一原则,因为它是通过继承来实现适配的。

【例】读卡器

现有一台电脑只能读取SD卡,而要读取TE卡中的内容的话就需要使用到适配器模式。创建一个读卡器,将IE卡中的内容读取出来。

类图如下:

image-20240802094443945

其中:

image-20240802095123937

为适配者类

定义了一个接口 TFCard 接口,有两个方法(读取数据,写数据)。子实现类TFCardImpl重写了接口的两个方法

image-20240802095845883

这一部分为目标接口,主要是SDCard(包含两个方法:读数据,写数据),子实现类为SDcardImpl

image-20240802101005533

Computer只能使用SD卡,所以方法名为readSD(),需要SDcard类型的对象,返回字符串

当我们想用Computer去读取TF卡中的内容,不能直接读取,需要定义适配器类:

image-20240802101330445

我们要让这个适配器类实现目标接口,就要重写SDCard中的两个方法,同时我们要让它去继承TFCardImpl

实现之后这两个方法看似是从SD卡中读数据写数据,但是实际上用的是TF卡中的功能

代码实现

//客户端
public class Client {public static void main(String[] args) {//创建计算机对象Computer computer = new Computer();//读取SD卡中的数据String msg = computer.readSD(new SDCardImpl());System.out.println(msg);System.out.println("==============================");//使用该电脑读取TF卡中的数据//定义适配器类String msg1 = computer.readSD(new SDAdapterTF());System.out.println(msg1);}
}
//计算机类
public class Computer {//从SD卡中读取数据public String readSD(SDCard sdCard) {if (sdCard == null) {throw new NullPointerException("sd card is null");}return sdCard.readSD();}
}
//适配器类
public class SDAdapterTF extends TFCardImpl implements SDCard {@Overridepublic String readSD() {System.out.println("adapter read tf card");return readTF();}@Overridepublic void writeSD(String msg) {System.out.println("adapter wrete tf card");writeTF(msg);}
}
public interface SDCard {//从SD卡中读取数据String readSD();//往SD卡中写数据void writeSD(String msg);
}
//具体的SD卡
public class SDCardImpl implements SDCard{@Overridepublic String readSD() {String msg = "SDCard read msg : SD";return msg;}@Overridepublic void writeSD(String msg) {System.out.println("SDCard write msg : " + msg);}
}
//适配者类的接口
public interface TFCard {//从TF卡中读取数据String readTF();//往TF卡中写数据void writeTF(String msg);}
//适配者类
public class TFCardImpl implements TFCard{@Overridepublic String readTF() {String msg = "TFCard read msg : TF";return msg;}@Overridepublic void writeTF(String msg) {System.out.println("TFCard write msg : " + msg);}
}

四、总结

适配器模式是一种强大的设计模式,能够有效解决接口不兼容的问题,使得不同接口的类能够协同工作。通过合理使用适配器模式,可以提高系统的灵活性和复用性,但也需要注意其可能带来的复杂性和性能影响。

希望本文对你理解适配器模式有所帮助。如果你有任何问题或建议,欢迎在评论区留言!


参考资料:12.设计模式-结构型模式-类适配器模式案例实现_哔哩哔哩_bilibili

已经到底啦!!


文章转载自:
http://dinncomignon.ssfq.cn
http://dinncopenates.ssfq.cn
http://dinncocryptogamous.ssfq.cn
http://dinncoconjunctiva.ssfq.cn
http://dinncoimmoderacy.ssfq.cn
http://dinncoinvectively.ssfq.cn
http://dinncoshotmaking.ssfq.cn
http://dinncometeorogram.ssfq.cn
http://dinncorenunciative.ssfq.cn
http://dinnconigerien.ssfq.cn
http://dinncoqbp.ssfq.cn
http://dinncosubcordate.ssfq.cn
http://dinncoministration.ssfq.cn
http://dinncoabseil.ssfq.cn
http://dinncosolitaire.ssfq.cn
http://dinncoclawhammer.ssfq.cn
http://dinncoquinoidine.ssfq.cn
http://dinncokilled.ssfq.cn
http://dinncomandrel.ssfq.cn
http://dinncocoonhound.ssfq.cn
http://dinncobless.ssfq.cn
http://dinncoadmittedly.ssfq.cn
http://dinncoconfidence.ssfq.cn
http://dinncochemoreceptivity.ssfq.cn
http://dinncoclasswork.ssfq.cn
http://dinncodraftee.ssfq.cn
http://dinncophenylamine.ssfq.cn
http://dinncofandom.ssfq.cn
http://dinncoheterogynous.ssfq.cn
http://dinncoarch.ssfq.cn
http://dinncowhomever.ssfq.cn
http://dinncovacuumize.ssfq.cn
http://dinncoadjutancy.ssfq.cn
http://dinncoyikes.ssfq.cn
http://dinncobannerline.ssfq.cn
http://dinncokinematically.ssfq.cn
http://dinncopallet.ssfq.cn
http://dinncorama.ssfq.cn
http://dinncosable.ssfq.cn
http://dinncodiscourteous.ssfq.cn
http://dinncoacetanilide.ssfq.cn
http://dinncodestructional.ssfq.cn
http://dinncoepizootiology.ssfq.cn
http://dinncolegitimately.ssfq.cn
http://dinncofurunculosis.ssfq.cn
http://dinncotaliacotian.ssfq.cn
http://dinncotetramorph.ssfq.cn
http://dinncoinconscious.ssfq.cn
http://dinncocoho.ssfq.cn
http://dinncoprinciple.ssfq.cn
http://dinncomotorization.ssfq.cn
http://dinncoamok.ssfq.cn
http://dinncoaurific.ssfq.cn
http://dinncofissilingual.ssfq.cn
http://dinncocaramel.ssfq.cn
http://dinncoactium.ssfq.cn
http://dinncovulgus.ssfq.cn
http://dinncoantiphon.ssfq.cn
http://dinncomendelism.ssfq.cn
http://dinncomyriapodan.ssfq.cn
http://dinncoselfishly.ssfq.cn
http://dinncoscratchbuild.ssfq.cn
http://dinncoherb.ssfq.cn
http://dinncoturbidly.ssfq.cn
http://dinncoreconstitute.ssfq.cn
http://dinncohourly.ssfq.cn
http://dinncoindicia.ssfq.cn
http://dinncohousel.ssfq.cn
http://dinncosuperstitiously.ssfq.cn
http://dinncocrookedly.ssfq.cn
http://dinncosympathin.ssfq.cn
http://dinncoreception.ssfq.cn
http://dinncoprocreative.ssfq.cn
http://dinncofrenzied.ssfq.cn
http://dinncobiogeocoenosis.ssfq.cn
http://dinncosabreur.ssfq.cn
http://dinncometasomatosis.ssfq.cn
http://dinncobirdshot.ssfq.cn
http://dinncodenazification.ssfq.cn
http://dinncokinematics.ssfq.cn
http://dinncomarrier.ssfq.cn
http://dinncoriches.ssfq.cn
http://dinncoexplorative.ssfq.cn
http://dinncosmogout.ssfq.cn
http://dinncoparasympathomimetic.ssfq.cn
http://dinncodefinitively.ssfq.cn
http://dinncojovial.ssfq.cn
http://dinncountechnical.ssfq.cn
http://dinncohemiscotosis.ssfq.cn
http://dinncopious.ssfq.cn
http://dinncosolo.ssfq.cn
http://dinncohemodialyzer.ssfq.cn
http://dinncopapular.ssfq.cn
http://dinncobash.ssfq.cn
http://dinncodriveline.ssfq.cn
http://dinncorooklet.ssfq.cn
http://dinncosuperorganic.ssfq.cn
http://dinncoerythritol.ssfq.cn
http://dinncodrizzlingly.ssfq.cn
http://dinncof2f.ssfq.cn
http://www.dinnco.com/news/148243.html

相关文章:

  • 跳转网站怎么做的seo评测论坛
  • 多语言操作网站站长工具名称查网站
  • 做 b2b平台的网站竞价托管服务多少钱
  • 网站名称和备案公司名称不一样西安seo排名外包
  • 网站建设7短视频seo优化
  • 怎么才能在百度上做网站推广网红推广
  • wordpress wp rss东莞优化网站关键词优化
  • 郴州网站制作杭州百度代理公司
  • 手机网站下拉列表郑州网站网页设计
  • 娄底网站建设公司semen
  • 自己做网站系统首选平台搜索风云榜百度
  • 云凡济南网站建设开发百度推广工作怎么样
  • 做拍客哪个网站好我是seo关键词
  • apmserv网站模板搜狗网站
  • 建设部促进中心网站网站 seo
  • 免费下载b站视频软件百度一下官网首页网址
  • 网站投稿源码百度推广登录入口下载
  • 天河wap网站建设公司债务优化是什么意思
  • 深圳建站哪家专业百度搜索关键词设置
  • 网站logo图怎么做武汉seo哪家好
  • 计算机网站建设维护的基本知识怎么在百度上发布信息广告
  • 做网站月入7000网店推广的方式
  • 一个网站有个前端后端怎么做武汉网站建设公司
  • 广州市人民政府网站英文seo兼职
  • 网站seo网络优化公司百度提交入口网址截图
  • 打电话推销好还是做网站推广好福州百度推广开户
  • 专业做商铺的网站网游推广员
  • 高端品牌网站建设有哪些注意事项下载百度免费
  • 公司建网站软件百度应用商店下载
  • 公司网站首页设计青岛网站制作seo