适配器模式
1. UML 演示
2. 代码演示
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)
3. 场景
3.1 封装旧接口
ajax ( {
url: '/getData' ,
type: 'Post' ,
dataType: 'json' ,
data: {
id: '123'
}
} ) . done ( function ( ) { } )
var $ = {
ajax: function ( oiptions) {
return ajax ( options)
}
}
3.2 vue 中的 computed
< div id = " example" >
< p> Original message: "{{ message }}"</ p>
< p> Computed reversed message: "{{ reversedMessage }}"</ p>
</ div>
var vm = new Vue ( {
el: '#example' ,
data: {
message: 'Hello'
} ,
computed: {
reversedMessage: function ( ) {
return this . message
. split ( '' )
. reverse ( )
. join ( '' )
}
}
} )
4. 设计原则