js-观察者模式

110 阅读1分钟

class Subject{
    constructor(){
        this.state = 0
        this.observers = []
    }
    getState(){
        return this.state
    }
    setState(state){
        this.state = state
        this.notifyAllObservers()
    }
    notifiyAllObservers(){
        this.observers.forEach(observer =>{
            observer.update()
        })
    }
    attach(observer){
        this.observers.push(observer)
    }
}
class Observer{
    constructor(name,subject){
        this.name = name
        this.subject = subject
        this.subject.attach(this)
    }
    update(){
        console.log(`${this.name} update,state:{$this.subject.getState()}`)
    }
}
//测试
let s = new Subject()
let o1 = new Observer('o1',s)

s.setState(1)

VUE watch方法