Created
January 5, 2017 07:53
-
-
Save ThorstenHans/c6dd0af24d7f3f5520146de8b7088619 to your computer and use it in GitHub Desktop.
Observer Pattern in TypeScript
This file contains hidden or 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 Subjekt { | |
private _beobachter: Array<IBeobachter> = []; | |
constructor(){ | |
this._beobachter = []; | |
} | |
public register(beobachter: IBeobachter): void { | |
this._beobachter.push(beobachter); | |
} | |
public unregister(beobachter: IBeobachter): void { | |
let index = this._beobachter.indexOf(beobachter); | |
if(index > -1) { | |
this._beobachter.splice(index, 1); | |
} | |
} | |
public notify(nachricht: string){ | |
this._beobachter.forEach((b) => { | |
b.update(nachricht); | |
}); | |
} | |
} | |
interface IBeobachter { | |
update(nachricht: string): void; | |
} | |
class LogBeobachter implements IBeobachter { | |
public update(nachricht: string): void { | |
console.log(`LOG: ${nachricht}`); | |
} | |
} | |
class InfoBeobachter implements IBeobachter { | |
public update(nachricht: string): void { | |
console.info(`INFO: ${nachricht}`); | |
} | |
} | |
let testSubjekt = new Subjekt(); | |
let logBeobachter = new LogBeobachter(); | |
let logBeobachter2 = new LogBeobachter(); | |
let infoBeobachter = new InfoBeobachter(); | |
testSubjekt.register(logBeobachter); | |
testSubjekt.notify('Wichtige Nachricht 1'); | |
// Ausgabe | |
// LOG: Wichtige Nachricht 1 | |
testSubjekt.register(logBeobachter2); | |
testSubjekt.register(infoBeobachter); | |
testSubjekt.notify('Wichtige Nachricht 2'); | |
// Ausgabe | |
// LOG: Wichtige Nachricht 2 | |
// LOG: Wichtige Nachricht 2 | |
// INFO: Wichtige Nachricht 2 | |
testSubjekt.unregister(logBeobachter2); | |
testSubjekt.notify('Wichtige Nachricht 3'); | |
// Ausgabe | |
// LOG: Wichtige Nachricht 3 | |
// INFO: Wichtige Nachricht 3 | |
testSubjekt.unregister(logBeobachter); | |
testSubjekt.unregister(infoBeobachter); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment