Skip to content

Instantly share code, notes, and snippets.

@WesleySmits
Created August 28, 2022 10:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save WesleySmits/6c7b96b635e60e565307c38b25105dc3 to your computer and use it in GitHub Desktop.
Save WesleySmits/6c7b96b635e60e565307c38b25105dc3 to your computer and use it in GitHub Desktop.
Observer pattern: Implementation example
class SubjectExample extends AbstractSubject {
#state = 'some interesting state';
get state(): string {
return this.#state;
}
set state(value: string) {
this.#state = value;
this.notify();
}
}
const subject = new SubjectExample();
class ObserverExample implements Observer {
constructor() {
subject.attach(this);
}
public update(subjectExample: SubjectExample): void {
console.log('Subject notified us of an update', subjectExample.state);
}
}
const observer = new ObserverExample();
subject.state = 'new';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment