观察者模式
发布&订阅 一对多 举例一: //主题,保存状态,状态变化后触发所有观察者对象 class Subject { constructor ( ) { this . state = 0 ; this . observers = [ ] ; } getState ( ) { return this . state ; } setState ( state ) { this . state = state ; this . notifyAllObservers ( ) ; } notifyAllObservers ( ) { 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 }