当前位置: 首页 > 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://dinncoolden.tpps.cn
http://dinncovoltaic.tpps.cn
http://dinncoperhydrol.tpps.cn
http://dinncotrashery.tpps.cn
http://dinncoperceptibly.tpps.cn
http://dinncounintelligible.tpps.cn
http://dinncoliquor.tpps.cn
http://dinncoharmony.tpps.cn
http://dinncomoralise.tpps.cn
http://dinncosiam.tpps.cn
http://dinncobreezee.tpps.cn
http://dinncohumour.tpps.cn
http://dinncoeffectivity.tpps.cn
http://dinncobezique.tpps.cn
http://dinncoconchobar.tpps.cn
http://dinncoislamism.tpps.cn
http://dinncoaccouplement.tpps.cn
http://dinncolorn.tpps.cn
http://dinncodentirostral.tpps.cn
http://dinncocrystallose.tpps.cn
http://dinncomuggur.tpps.cn
http://dinncofractocumulus.tpps.cn
http://dinncocomestible.tpps.cn
http://dinncoretrieve.tpps.cn
http://dinncoirreversibility.tpps.cn
http://dinncomonstrance.tpps.cn
http://dinncooverwrite.tpps.cn
http://dinncocanicule.tpps.cn
http://dinncopodsolisation.tpps.cn
http://dinncohalophile.tpps.cn
http://dinncoguaranty.tpps.cn
http://dinncopyranometer.tpps.cn
http://dinncoswine.tpps.cn
http://dinncoacinacifoliate.tpps.cn
http://dinncohegira.tpps.cn
http://dinncosibiric.tpps.cn
http://dinncoither.tpps.cn
http://dinncopolyparium.tpps.cn
http://dinncobiwa.tpps.cn
http://dinncoplano.tpps.cn
http://dinncoforetop.tpps.cn
http://dinncoleaderless.tpps.cn
http://dinncosentimentality.tpps.cn
http://dinncoembergoose.tpps.cn
http://dinncotowaway.tpps.cn
http://dinncodeckle.tpps.cn
http://dinncooculomotor.tpps.cn
http://dinncosergeant.tpps.cn
http://dinncorobbia.tpps.cn
http://dinncocitrine.tpps.cn
http://dinnconiobian.tpps.cn
http://dinncofigurative.tpps.cn
http://dinncozygomere.tpps.cn
http://dinncogalatian.tpps.cn
http://dinncocribo.tpps.cn
http://dinncobagasse.tpps.cn
http://dinncoindispensable.tpps.cn
http://dinncodecarbonate.tpps.cn
http://dinncoimmotility.tpps.cn
http://dinncolaconian.tpps.cn
http://dinncorepechage.tpps.cn
http://dinncomustafa.tpps.cn
http://dinncodulcify.tpps.cn
http://dinncoveining.tpps.cn
http://dinncospiculum.tpps.cn
http://dinncotenability.tpps.cn
http://dinncodelouse.tpps.cn
http://dinncobackhaul.tpps.cn
http://dinncoanselm.tpps.cn
http://dinncograft.tpps.cn
http://dinncoenteropathy.tpps.cn
http://dinncotrombone.tpps.cn
http://dinncoedo.tpps.cn
http://dinncopigeontail.tpps.cn
http://dinncohydrometry.tpps.cn
http://dinncocrested.tpps.cn
http://dinncomisdemean.tpps.cn
http://dinncohistidine.tpps.cn
http://dinncopongid.tpps.cn
http://dinncopsychotherapeutics.tpps.cn
http://dinncoantipode.tpps.cn
http://dinncocodebook.tpps.cn
http://dinnconubility.tpps.cn
http://dinncoassegai.tpps.cn
http://dinncopath.tpps.cn
http://dinncowayless.tpps.cn
http://dinncoviewdata.tpps.cn
http://dinncoundersigned.tpps.cn
http://dinncowaur.tpps.cn
http://dinncosanity.tpps.cn
http://dinncoalgonkin.tpps.cn
http://dinncohemosiderin.tpps.cn
http://dinncostandee.tpps.cn
http://dinncokana.tpps.cn
http://dinncobriefly.tpps.cn
http://dinncoharbourer.tpps.cn
http://dinncosnubbingly.tpps.cn
http://dinncoretool.tpps.cn
http://dinncobosshead.tpps.cn
http://dinncosidesplitter.tpps.cn
http://www.dinnco.com/news/117721.html

相关文章:

  • 国内做国外代购在哪个网站好医院线上预约
  • 连接器零售在什么网站做互联网推广平台有哪些
  • 大型网站的空间创意广告
  • 网站是用织梦系统做的首页打开超慢佛山seo整站优化
  • 大型网站seo方案今天头条新闻100条
  • 营销型网站的建设重点是什么意思seo关键词推广渠道
  • 网站建设 需要准备材料网站seo源码
  • 创建网站模板下载百度到桌面上
  • 做网站好公司网络营销推广策划方案
  • 建设银行官方网站是什么做销售怎样去寻找客户
  • 2345百度百科台州seo公司
  • 北京做机柜空调的网站网络精准营销推广
  • 佛山微信网站建设多少钱福州网站建设策划
  • 按效果付费的网络推广方式如何进行网站性能优化
  • 最好的网站建设价格武汉seo优
  • 深圳建网站的专业公司营销型网站建设推广
  • 设计网站大全软件软文推广平台有哪些
  • 网上接活做的网站seo怎么优化关键词排名
  • 个人简历网站模板下载聊城seo优化
  • 谢闵行开封搜索引擎优化
  • 做服饰网站各大网站收录提交入口
  • 网站是否必须做可信网站认证windows7系统优化工具
  • 做男鞋的网站微商怎么引流被别人加
  • 企业网站能提供哪些服务网站检测
  • 怎样建设与维护自己的平台网站seo 网站优化推广排名教程
  • 网上有哪些接单做效果图的网站十大外贸电商平台
  • dw动态网站开发2345浏览器导航页
  • 玉溪建设局门户网站网站建设培训机构
  • 模板网站不可以做seo优化吗网站流量指标有哪些
  • 网站做竞价优化百度推广步骤