-
-
Save WesleySmits/8c31122f0eef1826b79f4c96c600c10b to your computer and use it in GitHub Desktop.
Observer Pattern: Abstract subject class
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
export default abstract class AbstractSubject implements Subject { | |
#observers: Observer[] = []; | |
public get observers(): Observer[] { | |
return this.#observers; | |
} | |
public attach(observer: Observer): void { | |
if (this.#observers.includes(observer)) { | |
return; | |
} | |
this.#observers.push(observer); | |
} | |
public detach(observer: Observer): void { | |
this.#observers = this.#observers.filter((o) => o !== observer); | |
} | |
public notify(): void { | |
for (const observer of this.#observers) { | |
observer.update(this); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment