Observer Design Pattern
Observer design pattern creates a one-to-many dependency, allowing objects (Observers/subscribers) to automatically receive notifications and updates from another object (the Subject/publisher) when its state changes, without the subject needing to know the specific observers. It promotes loose coupling, enabling flexible event-driven systems for things like GUI updates or data streams, where subjects maintain a list of observers and notify them when events occur, making it ideal for decoupled, modular software.
Key Components
- Subject (Publisher/Observable):
- The object that holds the data/state and notifies its dependents. It manages a list of observers.
- Observer (Subscriber/Listener):
- An object that registers interest in the Subject’s state changes and defines an
update()method to receive notifications.
How it Works
- Registration: Observers subscribe (attach) to a Subject to be notified.
- State Change: The Subject’s state changes (e.g., a data value, an event triggers).
- Notification: The Subject iterates through its registered observers and calls their
update()method, often passing the new state. - Update: Observers receive the notification and react accordingly, updating their own displays or logic.
Get Kaizen Chandra’s stories in your inbox
Join Medium for free to get updates from this writer.
When to Use It
- When a change in one object requires changes in multiple others.
- When you need to decouple the object that changes state (Subject) from the objects that react to it (Observers).
- Common in Model-View-Controller (MVC) architectures and GUI event handling.
Example
- A weather station (Subject) tracks temperature; multiple displays (Observers) subscribe to get real-time readings without the station knowing about each specific display.
// Observer Interface
public interface Observer {
void update(String videoTitle);
}
public interface Subject {
void subscribe(Observer observer);
void unsubscribe(Observer observer);
void notifyObservers();
}import java.util.ArrayList;
import java.util.List;
// Concrete Subject
public class YouTubeChannel implements Subject {
private List<Observer> subscribers = new ArrayList<>();
private String latestVideo;
@Override
public void subscribe(Observer observer) {
subscribers.add(observer);
}
@Override
public void unsubscribe(Observer observer) {
subscribers.remove(observer);
}
@Override
public void notifyObservers() {
for (Observer subscriber : subscribers) {
subscriber.update(latestVideo);
}
}
public void uploadVideo(String videoTitle) {
this.latestVideo = videoTitle;
System.out.println("New Video Uploaded: " + videoTitle);
notifyObservers();
}
}// Concrete Observer
public class Subscriber implements Observer {
private String name;
public Subscriber(String name) {
this.name = name;
}
@Override
public void update(String videoTitle) {
System.out.println(name + " received notification: New video - " + videoTitle);
}
}static void main(String[] args) {
YouTubeChannel channel = new YouTubeChannel();
Observer user1 = new Subscriber("Alice");
Observer user2 = new Subscriber("Bob");
channel.subscribe(user1);
channel.subscribe(user2);
channel.uploadVideo("Observer Pattern Explained!");
}









