JavaScript之装饰者模式

装饰者模式

装饰者模式用于为对象动态增加职责。

和继承的比较

相较于装饰者模式而言,继承导致超类和子类之间的强耦合,超类修改,子类也会随之改变,同时在功能需求增加时,可能会创建出大量的子类。装饰者模式则更加灵活,即用即增。

example

对于需求的增加,我们经常做的处理是直接修改某个对象或某个函数,这很不好,违背了开放-封闭原则。以飞机大战中的飞机对象为例,不同级别的飞机发送不同的子弹,普通飞机发送普通的子弹,二级飞机发送导弹,三级飞机发送原子弹,使用装饰者模式处理:

面向对象方案

const Plane = function () {}
Plane.prototype.fire = function () {
    console.log('发送普通子弹')
}
// 二级飞机
const MissileDecorator = function (plane) {
    this.plane = plane
}
MissileDecorator.prototype.fire = function () {
    this.plane.fire()
    console.log('发送导弹')
}
// 三级飞机
const AtomDecorator = function (plane) {
    this.plane = plane
}
AtomDecorator.prototype.fire = function () {
    this.plane.fire()
    console.log('发送原子弹')
}
let plane = new Plane()
plane = new MissileDecorator( plane )
plane = new AtomDecorator( plane )
plane.fire() // 依次发送三种类型的子弹

装饰者模式的做法是保持原函数的引用,执行原函数,再拓展一部分功能。

JavaScript中的装饰模式

直接对函数作处理:

const fire = function () {
    console.log('发送普通子弹')
}
const _fire = fire
const missileFire = function () {
    _fire()
    console.log('发送导弹')
}
const _missileFire = missileFire
const atomFire = function () {
    _missileFire()
    console.log('发送原子弹')
}
atomFire() //

这样处理,难以管理很多中间变量,且this指向有时候会丢失。

AOP装饰函数

Function.prototype.before = function (beforeFn) {
    const _self = this
    return function () {
        beforeFn.apply( this, arguments )
        return _self.apply( this, arguments )
    }
}

Function.prototype.after = function (afterFn) {
    const _self = this
    return function () {
        const ret = _self.apply( this, arguments )
        afterFn.apply( this, arguments )
        return ret
    }
}

const Plane = function () {}
function fire() {
    console.log('发送普通子弹')
}
function missileFire() {
    console.log('发送导弹')
}
function atomFire() {
    console.log('发送原子弹')
}
const decoratorFire = fire.after(
    missileFire
).after(
    atomFire
)

decoratorFire() // 也会依次打印三个字符串

使用before,after函数可以保持this的传递,也避免了临时变量的创建。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值