设计模式(1)-发布-订阅者模式

// 模拟三个订阅者模式
var sub1 = {
    update: function() {
        console.log(1)
    }
}
var sub2 = {
    update: function() {
        console.log(2)
    }
}
var sub3 = {
    update: function() {
        console.log(3)
    }
}
// 发布者
function Dep() {
    this.subs = [sub1, sub2, sub3]
}
Dep.prototype.notify = function() {
    this.subs.forEach(function(sub) {
        sub.update()
    })
}
var dep = new Dep()
dep.notify()
复制代码

转载于:https://juejin.im/post/5d0b2e26e51d45106343181f

猜你喜欢

转载自blog.csdn.net/weixin_33835690/article/details/93163892