The Observer pattern is a behavioral design pattern where an object (the subject) maintains a list of its dependents (observers) and notifies them of any state changes, usually by calling one of their methods.
// Subject
class Subject {
constructor() {
this.observers = [];
}
subscribe(observer) {
this.observers.push(observer);
}
unsubscribe(observer) {
this.observers = this.observers.filter((obs) => obs !== observer);
}
notify(data) {
this.observers.forEach((observer) => observer.update(data));
}
}
// Observer
class Observer {
update(data) {
console.log(`Observer received data: ${data}`);
}
}
Classes and Methods
-
Subject Class
- Constructor: Initializes an empty array
observers
to keep track of the observers. subscribe(observer)
: Adds an observer to theobservers
array.unsubscribe(observer)
: Removes an observer from theobservers
array by filtering it out.notify(data)
: Calls theupdate
method on each observer in theobservers
array, passing the data to them.
- Constructor: Initializes an empty array
-
Observer Class
update(data)
: A method that logs the received data to the console. This method is intended to be overridden by concrete observer implementations.
const subject = new Subject();
const observer1 = new Observer();
const observer2 = new Observer();
subject.subscribe(observer1);
subject.subscribe(observer2);
subject.notify("Hello Observers!"); // Both observers will log the data
subject.unsubscribe(observer1);
subject.notify("Hello again!"); // Only observer2 will log the data
- Creating Instances:
- A
Subject
instance is created. - Two
Observer
instances are created.
- A
- Subscribing Observers:
- Both observers are subscribed to the subject.
- Notifying Observers:
- The subject notifies all subscribed observers with the message "Hello Observers!".
- The first observer is unsubscribed.
- The subject notifies the remaining observers with the message "Hello again!".
This code demonstrates the Observer pattern where a Subject
can notify multiple Observer
instances about changes. Observers can subscribe to or unsubscribe from the subject. When the subject's state changes, it notifies all subscribed observers by calling their update
method. This pattern is useful for implementing distributed event handling systems.