Adapter mode
- The old interface format is not compatible with the user
- Add an adaptation conversion interface in the middle
class Adaptee {
specificRequest() {
return '德国标准的插头'
}
}
class Target {
constructor () {
this.adaptee = new Adaptee()
}
request() {
let info = this.adaptee.specificRequest()
return `${
info} -> 转换器 -> 中国标准的插头`
}
}
let target = new Target()
let res = target.request()
console.log(res)
Scenes
- Encapsulate the old interface
vue computed
Encapsulate the old interface
ajax({
url: '/getData',
type: 'Post',
dataType: 'json',
data: {
id: "123"
}
})
.done(funciton(){
})
const $ = {
ajax: funciton (options) {
return ajax(options)
}
}
vue computed
<div id="example">
<p>{
{
message }}</p>
<p>{
{
reverseMessage }}</p>
</div>
var vm = new Vue({
el: '#example',
data: {
message: 'Hello'
},
computed: {
reverseMessage: funciton () {
return this.message.split('').reverse().join('')
}
}
})
Design principle verification
- Separate the old interface from the user
- Comply with the open and closed principle