/** Observer is a behavioral design pattern that defines
 * a subscription mechanism to notify multiple objects about
 * any events that happen to the object they're observing.
 */ interface Publisher { attach ( observer ) : void ; detach ( observer ) : void ; notify ( ) : void ; } export class ConcretePublisher implements Publisher { private observers : Set < Observer > = new Set ( ) ; attach ( observer ) { if ( this . observers . has ( observer ) ) { return ; } this . observers . add ( observer ) ; } detach ( observer ) { if ( this . observers . has ( observer ) ) { this . observers . delete ( observer ) ; } } notify ( ) { this . observers . forEach ( ( observer ) => observer . update ( ) ) ; } } interface Observer { update ( ) : void ; } export class ConcreteObserverA implements Observer { public received : number = 0 ; public update ( ) { this . received += 1 ; } } export class ConcreteObserverB implements Observer { public received : number = 0 ; public update ( ) { this . received += 1 ; } }