当前位置:网站首页>结构型模式之适配器模式
结构型模式之适配器模式
2022-06-22 17:58:00 【OldZhangYH】
源代码
适配器模式
将一个类的接口转换成客户希望的另一个接口,让原本因为接口不兼容不能一起工作的类可以一起工作。比如三叉插头转换成二叉插头。
结构
- 适配器(Adapter):通过继承或者引用适配者的对象来将适配者接口转换为目标接口。
- 适配者(Adaptee):需要被转换的接口
- 目标接口(Target):客户端期待的接口
适配器分为两类:
- 类适配器:耦合度较高,适配器要继承适配者
- 对象适配器:耦合度低,适配器引用适配者
案例
现在有一台电脑只能读SD卡,但是我们现在手上只有TF卡。因此我们要创建一个适配器来让电脑能读TF卡。
类适配器类图
当适配者有接口规范的时候才能用,不然就用不了。因为适配器不能继承两个类。
对象适配器类图
相对于类适配器,对象适配器把适配的的继承改成了引用。降低了耦合度
应用场景
- 老系统中存在满足新系统需求的类,但是接口和新系统的不一样
- 使用第三方的组件,但是组件的接口定义和自己的不一样
JDK中的应用
jdk中的字节字符转换流就用了适配器模式
边栏推荐
- 2年狂赚178亿元,中国游戏正在“收割”老外
- Flutter系列-flutter路由管理
- shell脚本详解(四)——循环语句之while循环和until循环(附加例题及解析)
- 5gc and satellite integrated communication scheme
- 到底使用Thread还是Service?
- TypeScript(7)泛型
- Golang implements reliable delay queue based on redis
- 如何在 FlowUs和Notion 等笔记软件中进行任务管理?
- Niuke.com: consolidation interval
- wpa_ State machine migration of supplicant
猜你喜欢

Activity跳转到Fragment的方法(Intent)

什么?HomeKit、米家、Aqara等生态也能通过智汀与天猫精灵生态联动?

Active Directory用户登录报告

IPLOOK作为O-RAN联盟会员,将共同促进5G产业发展

shell脚本详解(十)——sed编辑器的使用方法

STM32控制矩阵按键,HAL库,cubeMX配置

shell脚本(五)——函数

【建议收藏】消息队列常见的使用场景
DBMS in Oracle_ output. put_ Example of line usage

Implementing Domain Driven Design - using ABP framework - solution overview
随机推荐
UE4_UE5制作3DUI-跟随相机朝向(附工程)
shell脚本详解(二)——条件测试、if语句和case分支语句
2022 operation of simulated examination platform for examination question bank of welder (elementary) special operation certificate
jniLibs. Srcdirs = ['LIBS'] what's the use?
Sre is bound to move towards the era of chaotic engineering -- Huawei cloud chaotic engineering practice
Paopao Mart: empty souls need stories
贪心之分配问题(2)
一些技术想法:
Active directory user logon Report
org. apache. ibatis. binding. BindingException: Invalid bound statement (not found)
维智科技亮相西部数博会,时空AI技术获高度认可
到底使用Thread还是Service?
Detailed explanation of session mechanism and related applications of session
Iplook becomes RedHat (red hat) business partner
Thread pool: reading the source code of threadpoolexcutor
数商云:解析B2B2C多用户商城系统架构设计思路,开启智能商城新时代
Niuke.com: judge whether it is palindrome string
Typescript (7) generic
wpa_ State machine migration of supplicant
Centeros install mangodb