当前位置:网站首页>翻译翻译什么是适配器模式?
翻译翻译什么是适配器模式?
2022-04-22 13:32:00 【掘金安东尼】
说到:适配器,大家一定不会陌生:
所有的充电头,就是适配器,用于适配电源插孔和需要充电的设备;
同理,适配器模式(Adapter Pattern)是作为两个不兼容的接口之间的桥梁。这种类型的设计模式属于【结构型模式】,它结合了两个独立接口的功能。
代码示例也非常直观:
class
Adapter {
specificRequest() {
return
'手机充电接口'
}
}
class
Target {
constructor() {
this.
adapter
=
new
Adapter()
}
request() {
let
info
=
`${
this.
adapter.
specificRequest()
}
---通过适配器接入到---充电插头`
return
info
}
}
let
target
=
new
Target()
console.
info(
target.
request())
// 手机充电接口---通过适配器接入到---充电插头
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
当我们有动机地修改一个正常运行的系统的接口,这时应该考虑使用适配器模式。
不过,它也有一个要特别注意的缺点,即:过多地使用适配器,会让系统非常零乱,不易整体进行把握。比如,明明看到调用的是 A 接口,其实内部被适配成了 B 接口的实现,一个系统如果太多出现这种情况,无异于一场灾难。因此如果不是很有必要,可以不使用适配器,而是直接对系统进行重构。
不过,它的好处呢,就是:可以让任何两个没有关联的类一起运行。 2、提高了类的复用。
// 使用ES6改写适配器实现地图SDK统一渲染
class
googleMap {
show() {
console.
log(
'开始渲染谷歌地图');
}
}
class
baiduMap {
display() {
console.
log(
'开始渲染百度地图');
}
}
class
baiduMapAdapter
extends
baiduMap {
constructor() {
super();
}
show() {
this.
display();
}
}
// 外部调用者
function
renderMap(
map) {
map.
show();
// 统一接口调用
}
renderMap(
new
googleMap());
renderMap(
new
baiduMapAdapter());
// 开始渲染谷歌地图
// 开始渲染百度地图
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
OK,以上就是本篇分享,你“学废”了吗?
我是掘金安东尼,输出暴露输入,技术洞见生活。
版权声明
本文为[掘金安东尼]所创,转载请带上原文链接,感谢
https://blog.51cto.com/u_13961087/5242247
边栏推荐
- Walk in the clouds - travel to the edge of the universe
- XML外部实体攻击原理以及实战(XXE)(1)
- 数字孪生:如何撑起一个万亿市场的产业变革?
- 销量大腰斩,岚图无蓝图
- 【Flutter 专题】91 图解 Dart 单线程实现异步处理之 Future (二) #yyds干货盘点#
- Model based RL overview
- PM4PY - BPMN support
- Originally, this is the correct posture for developers to open world book day!
- Digital twin: how to support the industrial transformation of a trillion market?
- 小程序分享给好友列表以及分享到朋友圈
猜你喜欢

Chinese English Dictionary of Accounting & Financial sentiment

Digital twin: how to support the industrial transformation of a trillion market?

SPI协议的通信原理

Advertising offensive and defensive war of Internet giants

Reading target detection (2): fast r-cnn

抖音快手卧榻之侧,黄光裕难以酣睡

小程序分享给好友列表以及分享到朋友圈

Redis6 Detailed installation

Redis如何查看单个key所占用的内存大小

华为云媒体査勇:华为云在视频AI转码领域的技术实践
随机推荐
B站砍向直播,迟早的事
Pm4py - analyze what BPMN can be converted into process tree
ROS Robot Learning -- TF coordinate transformation
Harbor v2.5更新,都增加了哪些功能?
no main manifest attribute / .jar中没有主清单属性
Digital twin: how to support the industrial transformation of a trillion market?
Can ainai get out of the dilemma by 35 billion yuan?
Encapsulate unified response result enumeration class (tool module)
快速串讲校招高频面试题——排序算法和复杂度
ROS Robot Learning -- kinematics solution of mcnamu wheel
托宾Q数据-沪深A股上市公司(含行业名称、代码等指标)2003-2020
General steps for exporting Gerber files from Altium Designer
Redisconfig configuration class
HDU 2680 shortest path Dijkstra + chained forward star + priority queue (template)
Redis如何查看单个key所占用的内存大小
MySQL 使用存储过程添加数据
封装统一响应结果枚举类(工具模块)
Fizz企业级微服务网关-服务编排,祭出终结BFF层的大杀器
中英文会计&金融情绪词典
【黑马早报】知乎今日在港上市;小红书回应裁员20%;王者荣耀被指控抄袭;刘畊宏直播收入10天涨10倍;“知网反垄断第一案”已立案...