旧接口格式和使用者不兼容,中间加一个适配转换接口。比如生活中去香港或国外,电源接口跟国内不同就得需要一个转换器。
应用:
- 封装旧接口
- Vue computed方法
举例一:
class Adaptee{
constructor(name){
this.name = name;
}
specificRequest(){
return `English:${this.name}`;
}
}
class Target{
constructor(name){
this.adaptee = new Adaptee(name);
}
request(){
let info = this.adaptee.specificRequest();
return `${info} =》 中文:陈开心`;
}
}
let target = new Target('happy chen');
let result = target.request();
console.log(result);
举例二:
ajax({
url:'/getData',
type:'Post',
dataType:'json',
data:{
id:66
}
})
.done(function(){});
//历史原因,代码中全都是:
$.ajax({...})
//做一层适配器
var $ = {
ajax:function(options){
return ajax(options);
}
}
来源:CSDN
作者:HappyChen666
链接:https://blog.csdn.net/u013565133/article/details/103584687