当前位置:网站首页>从居家办公中感悟适配器模式 | 社区征文
从居家办公中感悟适配器模式 | 社区征文
2022-06-29 17:01:00 【InfoQ】
一
var googleMap={
show:function(){
console.log('开始渲染google地图');
}
};
var baiduMap={
show:function(){
console.log('开始渲染百度地图');
}
};
var renderMap=function(map){
if(map.show instanceof Function){
map.show();
}
};
renderMap(googleMap); //输出:开始渲染google地图
renderMap(baiduMap); //输出:开始渲染百度地图
var googleMap={
show:function(){
console.log('开始渲染google地图');
}
};
var baiduMap={
display:function(){
console.log('开始渲染百度地图');
}
};
var baiduMapAdapter={
show:function(){
return baiduMap.display();
}
};
var renderMap=function(map){
if(map.show instanceof Function){
map.show();
}
};
renderMap(googleMap);
renderMap(baiduMapAdapter);
二
var getGuangdongCity=function(){
var guangdongCity=[
{name:'shenzhen',id:11},
{name:'guangzhou',id:12}
];
return guangdongCity;
};
var render=function(fn){
console.log('开始渲染广东省地图');
document.write(JSON.stringify(fn()));
};
render(getGuangdongCity());
var guangdongCity={
shenzhen:11,
guangzhou:12,
zhuhai:13
};
var getGuangdongCity=function(){
var guangdongCity=[
{name:'shenzhen',id:11},
{name:'guangzhou',id:12}
];
return guangdongCity;
};
var render=function(fn){
console.log('开始渲染广东省地图');
document.write(JSON.stringify(fn()));
};
var addressAdapter=function(oldAddressfn){
var address={},
oldAddress=oldAddressfn();
for(var i=0,c;c=oldAddress[i++];){
address[c.name]=c.id; //转换为新的数据格式
}
};
render(addressAdapter(getGuangdongCity));
- 适配器模式主要用来解决两个已有接口之间不匹配的问题,它不考虑这些接口是怎样实现的,也不考虑它们将来可能会如何演化。适配器模式不需要改变已有的接口,就能够使它们协同作用。
- 装饰者模式和代理模式也不会改变原有对象的接口,但装饰者模式的作用是为了给对象增加功能。装饰者模式常常形成一条长的装饰链,而适配器模式通常只包装一次。代理模式是为了控制对对象的访问,通常也只包装一次。
- 外观模式的作用倒是和适配器比较相似,有人把外观模式看成一组对象的适配器,但外观模式最显著的特点是定义了一个新的接口。
边栏推荐
- curl: (56) Recv failure: Connection reset by peer
- XAMPP Apache安装时问题总结
- Shenzhen internal promotion | Shenzhen Institute of computing science recruits assistant machine learning Engineer (school recruitment)
- XAMPP Apache安装时问题总结
- 关于onReachButton 不触发可能原因
- When MySQL RDS is collected using Flink CDC, the datetime type field will be compared with the source table after collection
- A simple but scalable feature normalization method
- 全面剖析Seata 分布式事务 AT 与XA
- 基于opencv进行双目相机的标定
- 0基础自学STM32(野火)——寄存器点亮LED
猜你喜欢

After eight years of testing and opening experience and interview with 28K company, hematemesis sorted out high-frequency interview questions and answers

使用kalibr標定工具進行單目相機和雙目相機的標定

贪婪的苹果计划提高iPhone14的价格,这将为中国手机提供机会

Shenzhen internal promotion | Shenzhen Institute of computing science recruits assistant machine learning Engineer (school recruitment)

SpingMVC请求和响应

0基础自学STM32(野火)——寄存器点亮LED

研究所的这些优势真香!上岸率还极高!

MySQL foundation - multi table query
![[R language data science]: Text Mining (taking Trump's tweet data as an example)](/img/4f/09b9885915bee50fb40976a5002730.png)
[R language data science]: Text Mining (taking Trump's tweet data as an example)

适合中小企业的项目管理系统有哪些?
随机推荐
Shenzhen internal promotion | Shenzhen Institute of computing science recruits assistant machine learning Engineer (school recruitment)
Why does selenium become the first choice for web automated testing? (source code attached)
NAACL 2022 | 机器翻译SOTA模型的蒸馏
又拍云 Redis 的改进之路
Perhaps in two years, ASML will be free to supply EUV lithography machines to China
深圳内推 | 深圳计算科学研究院招聘机器学习助理工程师(校招)
[untitled]
isEmpty 和 isBlank 的用法区别,居然一半的人答不上来?
可转债策略之---(摊饼玩法,溢价玩法,强赎玩法,下修玩法,双低玩法)
Fluent的msh格式网格学习
为防止被00后整顿,一公司招聘要求员工不能起诉公司
PHP delete directory
@Difference between component and @configuration
XAMPP Apache安装时问题总结
Étalonnage de la caméra monoculaire et de la caméra binoculaire à l'aide de l'outil d'étalonnage kalibr
手把手教你在windows上安装mysql8.0最新版本数据库,保姆级教学
curl: (56) Recv failure: Connection reset by peer
535. TinyURL 的加密与解密 / 剑指 Offer II 103. 最少的硬币数目
英联邦国家有哪些
最高81.98%!超百所“双一流”高校本科深造率公布