-
-
Save WesleySmits/6c7b96b635e60e565307c38b25105dc3 to your computer and use it in GitHub Desktop.
Observer pattern: Implementation example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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