Programming Concepts — Observer Pattern
The Observer pattern allows objects to subscribe to changes in another object, enabling reactive programming.
class Subject {
constructor() {
this.observers = [];
}
subscribe(observer) {
this.observers.push(observer);
}
notify(data) {
this.observers.forEach(obs => obs(data));
}
}
const subject = new Subject();
subject.subscribe(data => console.log('Observer 1:', data));
subject.notify('Update received');