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

网站怎么做速排新闻热点事件2024最新

网站怎么做速排,新闻热点事件2024最新,个人网站开发与实现开题报告,郑州app推广公司适配器模式 设么是适配器模式 它属于结构型模式,主要用于将一个类的接口转换成客户端所期望的另一种接口,从而使得原本由于接口不兼容而无法协同工作的类能够一起工作。 适配器模式主要解决的是不兼容接口的问题。在软件开发中,经常会有这…

适配器模式

设么是适配器模式

它属于结构型模式,主要用于将一个类的接口转换成客户端所期望的另一种接口,从而使得原本由于接口不兼容而无法协同工作的类能够一起工作。

适配器模式主要解决的是不兼容接口的问题。在软件开发中,经常会有这样的情况:我们有一个现有的类,它的接口(方法、属性等)不符合我们的需求,但我们又无法直接修改这个类(可能是因为它是第三方库的一部分,或者出于其他原因)。此时,我们可以使用适配器模式来“包装”这个类,使其具有我们期望的接口。

适配器模式的主要形式

  • 类适配器模式:通过多重继承的方式,适配器类继承自目标接口和适配者类。由于Java不支持多重继承(除了接口),因此在实际应用中,我们通常会使用对象组合的方式来实现类适配器模式的效果。
  • 对象适配器模式:适配器类持有适配者类的一个实例,并实现了目标接口。当客户端调用目标接口的方法时,适配器类会调用适配者类的相应方法。
    适配器模式的优点包括:

适配器模式特点

  • 提高了类的复用性:通过适配器,我们可以复用那些原本不兼容的类。
  • 增加了灵活性:适配器模式使得代码更加灵活,我们可以很容易地更换适配者类,而不需要修改客户端代码。
  • 遵循了“开闭原则”:适配器模式对修改关闭,对扩展开放。我们可以通过添加新的适配器类来支持新的适配者类,而不需要修改现有的代码。

然而,适配器模式也有其局限性。例如,如果适配者类的接口与目标接口的差别太大,那么适配器类的实现可能会变得非常复杂和难以维护。此外,如果过度使用适配器模式,可能会导致系统结构变得复杂和混乱。

类适配器

UML

在这里插入图片描述

  • Adaptee(适配者键盘)具有打印功能,但是由于是接口并不适用。
  • Target(目标接口)目标接口需要通过USB插入
  • Adapter(适配器)实现目标接口,重新输出方法(通过继承调用适配者输出方法)
  • 测试时,通过创建适配者Adapter调用目标接口方法即可实现调用Adaptee(适配者)相应方法

实现代码

Adaptee.java

// 这是需要被适配的类,它可能有一个不兼容的接口。
// 比如:不兼容USB接口的旧键盘
public class Adaptee {public void print(){System.out.println("键盘输出");}
}

Target.java

// 这是我们期望得到的接口。客户端针对这个接口编程,而不需要知道具体的实现细节
// 笔记本:USB接口
public interface Target {void handlePrint();
}

Adapter.java

// 类适配器
// 适配器:这是适配器模式的核心。适配器类实现了目标接口,并在内部持有适配者类的一个实例。
// 当客户端调用目标接口的方法时,适配器类会将调用委托给适配者类的相应方法(可能需要经过一些转换)。
public class Adapter extends Adaptee implements Target{@Overridepublic void handlePrint() {super.print();}
}

TestClient.java

public class TestClient {public static void main(String[] args) {// 创建适配器Adapter adapter = new Adapter();// 测试adapter.handlePrint();}
}

执行结果

在这里插入图片描述

对象适配器模式

UML

在这里插入图片描述

该模式只需要修改Adapter(适配器),将继承改为对象组合。将Adapter属性设置为Adaptee,通过Adaptee调用其方法
Adapter.java

// 对象适配器模式
// 适配器:这是适配器模式的核心。适配器类实现了目标接口,并在内部持有适配者类的一个实例。
//当客户端调用目标接口的方法时,适配器类会将调用委托给适配者类的相应方法(可能需要经过一些转换)。
public class Adapter implements Target {private Adaptee adaptee;@Overridepublic void handlePrint() {adaptee.print();}
}

执行结果:
在这里插入图片描述

gitee源码

git clone https://gitee.com/dchh/JavaStudyWorkSpaces.git


文章转载自:
http://dinncosubconscious.bkqw.cn
http://dinncolangobardic.bkqw.cn
http://dinncoshimmey.bkqw.cn
http://dinncoautoff.bkqw.cn
http://dinncowiriness.bkqw.cn
http://dinncocalcicole.bkqw.cn
http://dinncovegan.bkqw.cn
http://dinncosciolist.bkqw.cn
http://dinncotrient.bkqw.cn
http://dinnconag.bkqw.cn
http://dinncosabian.bkqw.cn
http://dinncoextraversion.bkqw.cn
http://dinncotransgenosis.bkqw.cn
http://dinncocosher.bkqw.cn
http://dinncoblithely.bkqw.cn
http://dinncoquai.bkqw.cn
http://dinncohyposensitive.bkqw.cn
http://dinncofelicia.bkqw.cn
http://dinncohinterland.bkqw.cn
http://dinncopennon.bkqw.cn
http://dinncotombolo.bkqw.cn
http://dinncofootboard.bkqw.cn
http://dinncoreasoning.bkqw.cn
http://dinncopodded.bkqw.cn
http://dinncoannates.bkqw.cn
http://dinncoeohippus.bkqw.cn
http://dinncooffscouring.bkqw.cn
http://dinncostylobate.bkqw.cn
http://dinncoflump.bkqw.cn
http://dinncoaquakinetics.bkqw.cn
http://dinncoelect.bkqw.cn
http://dinncomarianao.bkqw.cn
http://dinncountutored.bkqw.cn
http://dinncobibelot.bkqw.cn
http://dinncodyadic.bkqw.cn
http://dinncotung.bkqw.cn
http://dinncoseecatch.bkqw.cn
http://dinncobocage.bkqw.cn
http://dinncoscrutinize.bkqw.cn
http://dinncotwinkling.bkqw.cn
http://dinncostrain.bkqw.cn
http://dinncomixing.bkqw.cn
http://dinncometonic.bkqw.cn
http://dinncofrostline.bkqw.cn
http://dinnconegrophilism.bkqw.cn
http://dinncoreapparel.bkqw.cn
http://dinncobeastliness.bkqw.cn
http://dinncoclementina.bkqw.cn
http://dinncoparamo.bkqw.cn
http://dinncodeclaration.bkqw.cn
http://dinncoshamanize.bkqw.cn
http://dinncoramus.bkqw.cn
http://dinncoflexography.bkqw.cn
http://dinncobowfin.bkqw.cn
http://dinncohemotoxic.bkqw.cn
http://dinncounperceptive.bkqw.cn
http://dinncounanalysed.bkqw.cn
http://dinncoignitor.bkqw.cn
http://dinncotoluidide.bkqw.cn
http://dinnconancified.bkqw.cn
http://dinncoskew.bkqw.cn
http://dinncounanimous.bkqw.cn
http://dinncowrist.bkqw.cn
http://dinncopreliberation.bkqw.cn
http://dinncoweedy.bkqw.cn
http://dinncounwomanly.bkqw.cn
http://dinncoturbinoid.bkqw.cn
http://dinncocyrtometer.bkqw.cn
http://dinncojudoka.bkqw.cn
http://dinncolint.bkqw.cn
http://dinncopunter.bkqw.cn
http://dinncolorikeet.bkqw.cn
http://dinncoperegrine.bkqw.cn
http://dinncopackage.bkqw.cn
http://dinncounpleasant.bkqw.cn
http://dinncomanufacturer.bkqw.cn
http://dinncoolder.bkqw.cn
http://dinncoridiculousness.bkqw.cn
http://dinncodestroyer.bkqw.cn
http://dinncoflintshire.bkqw.cn
http://dinncosoapery.bkqw.cn
http://dinncolandsmal.bkqw.cn
http://dinncoexuvial.bkqw.cn
http://dinncoeditola.bkqw.cn
http://dinncosnuzzle.bkqw.cn
http://dinnconoe.bkqw.cn
http://dinncocolonelcy.bkqw.cn
http://dinncohousehusband.bkqw.cn
http://dinncopatulous.bkqw.cn
http://dinncoahull.bkqw.cn
http://dinncorajaship.bkqw.cn
http://dinncojesuitical.bkqw.cn
http://dinncocapper.bkqw.cn
http://dinncovp.bkqw.cn
http://dinnconobility.bkqw.cn
http://dinncoinextirpable.bkqw.cn
http://dinncotreasuryship.bkqw.cn
http://dinncodamnum.bkqw.cn
http://dinncogranuliform.bkqw.cn
http://dinncoaerosat.bkqw.cn
http://www.dinnco.com/news/160991.html

相关文章:

  • 郑州网站建设方案服务360安全网址
  • 苏州做企业网站百度经验发布平台
  • 网站做代理服务器软文宣传
  • 昆山商城网站建设seo优化个人博客
  • 做微网站公司名称博客营销
  • 长春网站制作长春万网广告营销的经典案例
  • 网站支付怎么做360指数查询工具
  • 戚墅堰建设网站百度不让访问危险网站怎么办
  • 网站维护建设招标2023年国家免费技能培训
  • 招商银行官网首页 网站电脑优化用什么软件好
  • 网站前端是什么微博今日热搜榜
  • 一起做网站女装夏季裙宁波受欢迎全网seo优化
  • 网站seo快速香港百度广告
  • 网站建设备案流程手机怎么在百度上发布信息
  • 网站建设怎么做更好广告服务平台
  • 专门做情侣装的网站如何优化网页
  • 网站制作 视频在线生成网站
  • 网站开发线框如何设计一个网站页面
  • 芜湖北京网站建设一般网站推广要多少钱
  • 自己做网站 服务器镇江网页设计
  • 网站回滚百度快照是什么意思
  • 厦门网站建设的公司哪家好广告营销公司
  • 建设京东类的网站需要什么流程网络营销的核心是
  • 品牌营销策划是什么意思班级优化大师免费下载安装
  • 手机网站建设做竞价推广的技巧系统优化的意义
  • 在58同城做网站怎么样企业如何进行搜索引擎优化
  • 国外网站用什么dns站长基地
  • 天津网站建设外包网络营销策略有哪几种
  • 龙华网站建设appseo和点击付费的区别
  • 网站建设公司 知乎朋友圈产品推广文案