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

设计模式笔记_结构型_适配器模式

1.适配器模式介绍

适配器模式是一种结构型设计模式,它允许不兼容的接口协同工作。适配器模式的核心思想是将一个类的接口转换成客户期望的另一个接口,使得原本由于接口不兼容而不能一起工作的类可以一起工作。

你可以将其想象成一个“转换插头”——假设你有一个中国的电器插头,但你在欧洲旅行,需要将它插入欧洲的插座。适配器就是那个可以让中国插头插入欧洲插座的转换器。

在软件开发中,适配器模式通常用于整合一些旧的代码或第三方库,使它们能够与新的系统一起工作,而不需要修改它们的内部实现。适配器模式通过创建一个“适配器类”,这个类在内部转换接口,使得不兼容的接口可以顺利地互操作。

适配器模式通常包含以下几个角色:

  1. 目标接口(Target):这是客户期望的接口,目标可以是具体的或抽象的类。
  2. 适配者(Adaptee):这是需要适配的类,拥有客户希望使用的功能,但接口不兼容。
  3. 适配器(Adapter):通过在内部包装一个适配者对象,把适配者的接口转换为目标接口。

2. 代码演示

场景说明:我们创建一个媒体播放器MediaPlayer作为目标接口,一个老的媒体播放器OldMediaPlayer(接口及其实现)作为适配者,并且通过MediaAdapter 类实现了适配器,最终实现复用老媒体播放器功能。

目标接口:

public interface MediaPlayer {void play(String audioType, String fileName);
}

适配者:OldMediaPlayer及其实现类

//老媒体播放器,里面有希望使用的功能
public interface OldMediaPlayer {void playVlc(String fileName);void playMp4(String fileName);
}//map4播放器
public class Mp4Player implements OldMediaPlayer {@Overridepublic void playVlc(String fileName) {// Do nothing}@Overridepublic void playMp4(String fileName) {System.out.println("Playing mp4 file. Name: " + fileName);}
}//vlc播放器
public class VlcPlayer implements OldMediaPlayer {@Overridepublic void playVlc(String fileName) {System.out.println("Playing vlc file. Name: " + fileName);}@Overridepublic void playMp4(String fileName) {// Do nothing}
}

适配器:

//适配器:通过在内部包装一个适配者对象,把适配者的接口转换为目标接口
public class MediaAdapter implements MediaPlayer {//实现了 MediaPlayer 接口并使用 OldMediaPlayer 的实例OldMediaPlayer oldMediaPlayer;public MediaAdapter(String audioType){if(audioType.equalsIgnoreCase("vlc") ){oldMediaPlayer = new VlcPlayer();} else if (audioType.equalsIgnoreCase("mp4")){oldMediaPlayer = new Mp4Player();}}@Overridepublic void play(String audioType, String fileName) {if(audioType.equalsIgnoreCase("vlc")){oldMediaPlayer.playVlc(fileName);}else if(audioType.equalsIgnoreCase("mp4")){oldMediaPlayer.playMp4(fileName);}}
}

使用适配器

//使用适配器来播放不同格式的音频文件
public class AudioPlayer implements MediaPlayer {MediaAdapter mediaAdapter;@Overridepublic void play(String audioType, String fileName) {// Inbuilt support for mp3 music filesif(audioType.equalsIgnoreCase("mp3")){System.out.println("Playing mp3 file. Name: "+ fileName);}// MediaAdapter is providing support to play other file formatselse if(audioType.equalsIgnoreCase("vlc") || audioType.equalsIgnoreCase("mp4")){mediaAdapter = new MediaAdapter(audioType);mediaAdapter.play(audioType, fileName);}else{System.out.println("Invalid media. " + audioType + " format not supported");}}
}
public class Demo {public static void main(String[] args) {AudioPlayer audioPlayer = new AudioPlayer();audioPlayer.play("mp3", "beyond the horizon.mp3");audioPlayer.play("mp4", "alone.mp4");audioPlayer.play("vlc", "far far away.vlc");audioPlayer.play("avi", "mind me.avi");}
}

上述例子中,MediaAdapter类充当了适配器的角色,通过实现 MediaPlayer 接口,并在内部调用 OldMediaPlayer 的 playXXX() 方法,实现了接口的兼容。这样,新的音频系统就可以使用旧的播放器来播放音乐,而不需要对旧的播放器进行任何修改。


文章转载自:
http://xyris.sxnf.com.cn
http://obfusticated.sxnf.com.cn
http://whitefly.sxnf.com.cn
http://phlegmon.sxnf.com.cn
http://pict.sxnf.com.cn
http://rhabdomancy.sxnf.com.cn
http://crux.sxnf.com.cn
http://photocatalyst.sxnf.com.cn
http://rafflesia.sxnf.com.cn
http://sale.sxnf.com.cn
http://debag.sxnf.com.cn
http://khan.sxnf.com.cn
http://clouted.sxnf.com.cn
http://reformist.sxnf.com.cn
http://perhydrogenate.sxnf.com.cn
http://attention.sxnf.com.cn
http://esker.sxnf.com.cn
http://columba.sxnf.com.cn
http://daymare.sxnf.com.cn
http://augite.sxnf.com.cn
http://homotransplant.sxnf.com.cn
http://pastime.sxnf.com.cn
http://rhyparographer.sxnf.com.cn
http://yellow.sxnf.com.cn
http://eva.sxnf.com.cn
http://reduplicative.sxnf.com.cn
http://spherically.sxnf.com.cn
http://fourplex.sxnf.com.cn
http://fidge.sxnf.com.cn
http://manstealing.sxnf.com.cn
http://www.dtcms.com/a/280713.html

相关文章:

  • 3.正则化——新闻分类
  • 生产问题排查-数据库连接池耗尽
  • 牛客:HJ23 删除字符串中出现次数最少的字符[华为机考][字符串]
  • Linux 环境下安装 Node.js v16.13.0 完整指南
  • MongoDB 数据库 启用访问控制
  • Volta现代化的 Node.js 版本管理工具
  • CSRF 攻击原理与实验测试(附可运行测试案例)
  • NineData 社区版 V4.3.0 正式发布!新增 5 条迁移对比链路,全面支持 MariaDB、GaussDB 等数据库
  • [附源码+数据库+毕业论文]基于Spring+MyBatis+MySQL+Maven+vue实现的酒店预订管理系统,推荐!
  • Gitlab跑CICD的时候,maven镜像和pom.xml使用的maven版本冲突导致没办法build成功的解决方法
  • [附源码+数据库+毕业论文+开题报告]基于Spring+MyBatis+MySQL+Maven+jsp实现的车辆运输管理系统,推荐!
  • 2025-7-15-C++ 学习 排序(4)
  • langchain教程10:LCEL
  • 【c++】c++11新特性(右值引用和移动语义)
  • PySpark 常用算子详解
  • 【BUG处理】构建APK时遇到错误:‘flutter‘ 命令未被识别。这通常表示您的系统中未安装Flutter SDK或环境变量配置不正确。
  • 牛客:HJ20 密码验证合格程序[华为机考][字符串]
  • 【源力觉醒 创作者计划】文心4.5 vs DeepSeek vs Qwen 3.0:三大能力硬核实测!谁才是王者?
  • 纸板加工制造学习1
  • CF37E Trial for Chief 题解
  • 青年科学基金项目答辩PPT模板 | 杰青优青ppt设计制作美化 | WordinPPT
  • uni-app 学习笔记:Vuex 持久化数据
  • 【C++】神奇的AVL树
  • Java单元测试JUnit
  • 使用 Java 获取 PDF 页面信息(页数、尺寸、旋转角度、方向、标签与边框)
  • 已知均数与标准差,如何生成一组正态分布数据?
  • EPLAN 电气制图(九):直流电源绘制+端子排绘制
  • 线程(二) linux 互斥
  • JVM——有哪些常见的垃圾收集器
  • Props