java适配器模式

news/2024/10/29 0:21:52/

一、是什么?

定义: 将一个类的接口变成另外一个类所期待的另一个接口, 从而使因接口不匹配而无法一起工作的两个类能够一起工作

举个例子, 苹果手机想用type-c的充电器充电, 但充电接口不吻合, 所以就选哦一个转接头, 使type-c

能给苹果手机充电, 这就是适配器

二、适配器的三种实现

2.1 接口适配器模式

SpringMvc中因为handler有三种实现方式(implements Controller、implements HttpRequestHandler、@RequestMapping),要执行这三种handler的话,需要对应适配器才行。这就是接口适配器模式的实际应用之一。

下面的例子用type-c充任何电器

2.1.1类图

 2.1.2 代码实现

注:support方法判断当前适配器是否适配这个数码电器

// 被适配的数码电器接口
public interface DigitalAppliance {void charge();
}// 耳机
public class Earphone implements DigitalAppliance{@Overridepublic void charge() { System.out.println("earphone charge...");}
}// 手机
public class Phone implements DigitalAppliance{@Overridepublic void charge() { System.out.println("phone charge...");}
}// Mp3
public class Mp3 implements DigitalAppliance{@Overridepublic void charge() { System.out.println("mp3 charge...");}
}// 期望的接口
public interface ChargeAdapter {void charge(DigitalAppliance digitalAppliance);boolean support(DigitalAppliance digitalAppliance);
}// 耳机充电适配器
public class EarphoneChargeAdapter implements ChargeAdapter{@Overridepublic void charge(DigitalAppliance digitalAppliance) {System.out.println("转为Earphone充电...");digitalAppliance.charge();}@Overridepublic boolean support(DigitalAppliance digitalAppliance) {return digitalAppliance instanceof Earphone;}
}// 手机充电适配器
public class PhoneChargeAdapter implements ChargeAdapter{@Overridepublic void charge(DigitalAppliance digitalAppliance) {System.out.println("转为phone充电...");digitalAppliance.charge();}@Overridepublic boolean support(DigitalAppliance digitalAppliance) {return digitalAppliance instanceof Phone;}
}// Mp3充电适配器
public class Mp3ChargeAdapter implements ChargeAdapter{@Overridepublic void charge(DigitalAppliance digitalAppliance) {System.out.println("转为mp3充电...");digitalAppliance.charge();}@Overridepublic boolean support(DigitalAppliance digitalAppliance) {return digitalAppliance instanceof Mp3;}
}public class Typec {private static final List<ChargeAdapter> CHARGE_ADAPTERS = new ArrayList<>();static {// 获取所有的适配器CHARGE_ADAPTERS.add(new PhoneChargeAdapter());CHARGE_ADAPTERS.add(new EarphoneChargeAdapter());CHARGE_ADAPTERS.add(new Mp3ChargeAdapter());}public static void main(String[] args) {DigitalAppliance digitalAppliance = new Phone();// 获取合适的适配器ChargeAdapter chargeAdapter = getChargeAdapter(digitalAppliance);chargeAdapter.charge(digitalAppliance);}private static ChargeAdapter getChargeAdapter(DigitalAppliance digitalAppliance) {for (ChargeAdapter chargeAdapter : CHARGE_ADAPTERS) {// 判断是否适配if (chargeAdapter.support(digitalAppliance)) {return chargeAdapter;}}throw new RuntimeException("没有合适的适配器");}
}

2.2 类适配器模式

Iphone所期待的充电插口可以是接口也可以是抽象类或具体类。被适配类和适配器是父子关系,适配器作为能充苹果的Typec

2.2.1 类图

 2.2.2 代码实现

// 被适配的类
public class TypeC {public void charge() {System.out.println("type-c充电....");}
}// 期望使用的接口
public interface IphoneChargeAdapter {void chargeToIphone();
}// 适配器,继承Typec,把自己当成Typec,但又可以充苹果
public class TypeCToIphoneAdapter extends TypeC implements IphoneChargeAdapter {@Overridepublic void chargeToIphone() {System.out.println("转换成iphone口....");charge();}public static void main(String[] args) {// 使用IphoneChargeAdapter iphoneChargeAdapter = new TypeCToIphoneAdapter();iphoneChargeAdapter.chargeToIphone();}
}

2.3 对象适配器模式

被适配类和适配器是组合关系,作为适配器的属性存在。

2.3.1 类图

在这里插入图片描述

 2.3.2 代码实现

public interface IphoneChargeAdapter {void chargeToIphone();
}public class TypeC {public void charge() {System.out.println("type-c充电....");}
}public class TypeCToIphoneAdapter implements IphoneChargeAdapter {TypeC typeC;public TypeCToIphoneAdapter(TypeC typeC) {this.typeC = typeC;}@Overridepublic void chargeToIphone() {System.out.println("转换成iphone口....");typeC.charge();}public static void main(String[] args) {TypeC typeC = new TypeC();IphoneChargeAdapter adapter = new TypeCToIphoneAdapter(typeC);adapter.chargeToIphone();}}

五、与装饰器模式的区别

装饰器模式的目的是在原有功能的基础之上增强扩展功能。而适配器模式的目的是将一个接口转变成另一个接口,改变原有接口的功能。

六、优点

  • 提高了类的复用,低耦合
  • 灵活性好,不想用适配器,直接删除就好,不会影响
  • 较好的扩展性,符合开闭原则

原文: Java设计模式之适配器模式_吖土豆的博客-CSDN博客


http://www.ppmy.cn/news/661982.html

相关文章

i5 1240p和i7 13700h差距 酷睿ii51240和i713700h参数对比

i5-1240P处理器具有4个性能核心&#xff0c;8个效能核心&#xff0c;总计12核心&#xff0c;16线程。12MB三级缓存&#xff0c;最大频率4.4GHz。锐炬Xe集成显卡有80EU&#xff0c;频率最高1.3GHz。CPU基础功率28W&#xff0c;最大功率64W&#xff0c;多线程性能提升可达70%。选…

i9 12900参数 i912900怎么样

i9-12900 为 16 核 24 线程&#xff0c;8 个大核心 8 个小核心&#xff0c;最高 5.1GHz&#xff0c;30MB 三级缓存&#xff0c;基础功耗 65W&#xff0c;最高功耗 202W。核显为 UHD770&#xff0c;32EU&#xff0c;最高 1.55GHz。 i9 12900组装电脑怎么搭配更合适这些点很重要…

求职贴 | 多源融合定位方向24届硕士秋招求职

求职方向 多源融合定位 / LIO / VIO 基本情况 现就读于华中区某985&#xff0c;研究方向为多源融合定位&#xff0c;主要内容是LiDAR / IMU融合两篇期刊论文在投&#xff0c;论文内容即项目经历&#xff1b;具有丰富的电子设计类竞赛经历自我认为学习能力强&#xff0c;爱动…

SAP-QM质量管理视图字段解析

QM物料授权:如果在物料主记录中输入物料授权组,则系统将检查(在物料基础上)用户是否具有对以上名称对象的授权。如果未输入授权组,则不进行授权检查。要通过授权检查,用户需要 物料授权,其中包含有字段值所表示的相关授权组。 收货处理时间:收货后的加盐时间(天数)如…

给网游写一个挂

给网游写一个挂吧&#xff08;一&#xff09; – 反反外挂驱动的驱动 去年做了一些研究&#xff0c;研究做外挂的一些相关技术&#xff0c;打算放出来跟大家分享&#xff0c;分享一下我们做挂的一些思路&#xff0c;挂的原理&#xff0c;希望抛砖引玉。 外挂说白了就是用程序代…

(转载)应该对什么告警

告警的本质 没有多少系统的告警是设计得当的。良好的告警设计是一项非常困难的工作。如何知道你收到的告警是糟糕的&#xff1f;多少次你收到了告警之后&#xff0c;立即就关掉了的&#xff1f;是不是成天被这些然而并没有什么卵用的东西给淹没&#xff1f;最常见的告警设置&am…

《皇室战争》体验报告:游戏时长被无限拉长

《皇室战争》体验报告 http://www.gameres.com/678457.html by伊达政宗 2016.8.26 ↑ 笔者不久前才开出的传奇卡电磁炮 前言&#xff1a;   今年上半年的早一些时候&#xff0c;笔者听说了国民老公王思聪在某款游戏中获得了全球排名第四的好成绩&#xff0c;后…

应该对什么告警?

告警的本质 没有多少系统的告警是设计得当的。良好的告警设计是一项非常困难的工作。如何知道你收到的告警是糟糕的&#xff1f;多少次你收到了告警之后&#xff0c;立即就关掉了的&#xff1f;是不是成天被这些然而并没有什么卵用的东西给淹没&#xff1f;最常见的告警设置&am…