当前位置:网站首页>结构型模式之适配器模式
结构型模式之适配器模式
2022-06-22 17:58:00 【OldZhangYH】
源代码
适配器模式
将一个类的接口转换成客户希望的另一个接口,让原本因为接口不兼容不能一起工作的类可以一起工作。比如三叉插头转换成二叉插头。
结构
- 适配器(Adapter):通过继承或者引用适配者的对象来将适配者接口转换为目标接口。
- 适配者(Adaptee):需要被转换的接口
- 目标接口(Target):客户端期待的接口
适配器分为两类:
- 类适配器:耦合度较高,适配器要继承适配者
- 对象适配器:耦合度低,适配器引用适配者
案例
现在有一台电脑只能读SD卡,但是我们现在手上只有TF卡。因此我们要创建一个适配器来让电脑能读TF卡。
类适配器类图
当适配者有接口规范的时候才能用,不然就用不了。因为适配器不能继承两个类。
对象适配器类图
相对于类适配器,对象适配器把适配的的继承改成了引用。降低了耦合度
应用场景
- 老系统中存在满足新系统需求的类,但是接口和新系统的不一样
- 使用第三方的组件,但是组件的接口定义和自己的不一样
JDK中的应用
jdk中的字节字符转换流就用了适配器模式
边栏推荐
- [suggestions collection] common usage scenarios of message queue
- 2年狂赚178亿元,中国游戏正在“收割”老外
- In the first half of the year, there were 7 new unicorns in this field, and the capital scrambled to enter the market
- Makefile将某一部分文件不编译
- std::enable_ shared_ from_ This error: error: expected template name before '<' token
- 加工制造业智慧采购系统解决方案:助力企业实现全流程采购一体化协同
- 机械设备行业数字化供应链集采平台解决方案:优化资源配置,实现降本增效
- 同花顺好用么?手机开户安全么?
- 3GPP 5g R17 standard is frozen, and redcap as an important feature deserves attention!
- 数字赋能机械制造业,供应链协同管理系统解决方案助力企业供应链再升级
猜你喜欢

程序员工具大全【持续更新】

UE4_ Ue5 make 3dui follow the camera orientation (attached works)
Notes on Combinatorics (V) chains in distributive lattice

【建议收藏】消息队列常见的使用场景

新人报道的笔记

After reading the hated courage

wpa_supplicant的状态机迁移

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

IPLOOK 成为 RedHat(红帽)业务合作伙伴

Dynamically changing the style of label elements in a loop
随机推荐
同花顺好用么?手机开户安全么?
到底使用Thread还是Service?
SystemVerilog (12) - $unit declaration space
2022 t elevator repair recurrent training question bank and answers
session机制详解以及session的相关应用
Interview MySQL
JSP connection MySQL total error
Redis usage scenario sharing (project practice)
Flutter系列-Dart基础语法学习
如何更改Apple Watch上的表盘
China's games are "harvesting" foreigners
SSH password free login
Flutter2.0运行在web上不同渲染器的问题
wpa_ CLI parameter description
shell脚本详解(二)——条件测试、if语句和case分支语句
jniLibs. Srcdirs = ['LIBS'] what's the use?
Notes on Combinatorics (V) chains in distributive lattice
运维、监控、AIOps的几个重要观点
wpa_cli参数说明
助力客户数字化转型,构建全新的运维体系