Observer

About

Similar to Mediator, but only ONE Object can be the publisher.

Allow many Objects (Subscribers) to subscribe to events that are broadcasted by an Object (Publisher).

circle-info

An Observer have a relationship of 1many1 \to many, meaning:

  • 1 Object acts as the Publisher. (One or multiple Events)

  • Many Objects can subscribe and react to these Events.

Publisher

The class that will notify the Subscribers.

class Publisher {
    subscribers: Subscriber[];
    
    subscribe(Subscriber subscriber): void {
        this.subcribers.push(subscriber);
    };
    
    unsubscribe(Subscriber subscriber): void {
        this.subscribers.pop(subscriber);
    };
    
    notify(): void {
        for (let subscriber of this.subscribers) doSomething();
    };
}

Subscriber

The interface or class of objects that will get notified.

Last updated