桥接模式:将抽象部分与它的实现部分分离,使它们都可以独立地变化 使用场景:一个类存在两个或多个独立变化的维度,且这两个或多个维度都需要进行扩展
优点: 把抽象与实现隔离开,有助于独立地管理各组成部分
缺点: 每使用一个桥接元素都要增加一次函数调用,这对应用程序的性能会有一些负面影响(提高了系统的复杂程度)
const Aniations = {
bounce: {
show(ele) {
console.log(ele, '弹跳显示')
},
hide(ele) {
console.log(ele, '弹跳隐藏')
},
},
slide: {
show(ele) {
console.log(ele, '滑动显示')
},
hide(ele) {
console.log(ele, '滑动隐藏')
},
},
rotate: {
show(ele) {
console.log(ele, '旋转显示')
},
hide(ele) {
console.log(ele, '旋转隐藏')
},
},
}
function Toast(ele, animation) {
this.ele = ele
this.animation = animation
}
Toast.prototype.show = function () {
this.animation.show(this.ele)
}
Toast.prototype.hide = function () {
this.animation.hide(this.ele)
}
let taost1 = new Toast('div1', Aniations.bounce)
taost1.show()
小于 1 分钟