Observer Design Pattern
Subscriber and Publisher Mechanism
Observer Design Pattern
Subscriber and Publisher Mechanism
Software systems often need to react to changes in data. When one object changes its state, many other objects may need to update themselves automatically. Handling this efficiently without tight coupling , is where the Observer Design Pattern shines.
The Observer Pattern helps ensure that all subscribers are kept up to date without creating tight coupling. It defines a one-to-many dependency so that when one object changes state, all its observers are notified automatically.

Problem Statement
We are building a Weather Monitoring System.
- A *‘*WeatherStation’ **collects temperature data
- Multiple displays want to show the temperature:
- Mobile App
- Web Dashboard
- LED Screen
Requirement
Whenever the temperature changes, all displays must update automatically.
Problematic Code (Without Observer Pattern)
class WeatherStation {
private int temperature;
public void setTemperature(int temp) {
this.temperature = temp;
updateMobileDisplay();
updateWebDisplay();
updateLedDisplay();
}
private void updateMobileDisplay() {
System.out.println("Mobile Display: " + temperature);
}
private void updateWebDisplay() {
System.out.println("Web Display: " + temperature);
}
private void updateLedDisplay() {
System.out.println("LED Display: " + temperature);
}
}
Problems
- WeatherStation must be modified every time a new display is added
- Strong dependency on display implementations
- Not scalable
How to Implement Observer Design Pattern?
- Create an Observer interface
- Create a Subject interface
- Implement WeatherStation as ConcreteSubject
- Implement displays as ConcreteObservers
- Notify observers when state changes
Correct Code
// Observer Interface
public interface Observer {
void update(int temperature);
}
// Subject Interface
public interface Subject {
void registerObserver(Observer o);
void removeObserver(Observer o);
void notifyObservers();
}
// WeatherStation class
import java.util.ArrayList;
import java.util.List;
public class WeatherStation implements Subject {
private int temperature;
private List<Observer> observers = new ArrayList<>();
@Override
public void registerObserver(Observer o) {
observers.add(o);
}
@Override
public void removeObserver(Observer o) {
observers.remove(o);
}
public void setTemperature(int temp) {
this.temperature = temp;
notifyObservers();
}
@Override
public void notifyObservers() {
for (Observer o : observers) {
o.update(temperature);
}
}
}
// Displays - Mobile Display
public class MobileDisplay implements Observer {
@Override
public void update(int temperature) {
System.out.println("Mobile Display: " + temperature);
}
}
// Displays - Web Display
public class WebDisplay implements Observer {
@Override
public void update(int temperature) {
System.out.println("Web Display: " + temperature);
}
}
// Displays - LED Display
public class LedDisplay implements Observer {
@Override
public void update(int temperature) {
System.out.println("LED Display: " + temperature);
}
}
// Main class
public class Main {
public static void main(String[] args) {
WeatherStation station = new WeatherStation();
Observer mobile = new MobileDisplay();
Observer web = new WebDisplay();
Observer led = new LedDisplay();
station.registerObserver(mobile);
station.registerObserver(web);
station.registerObserver(led);
station.setTemperature(25);
station.setTemperature(30);
}
}
Output:
Mobile Display: 25
Web Display: 25
LED Display: 25
Mobile Display: 30
Web Display: 30
LED Display: 30

Observer Design Pattern
The Observer Design Pattern is ideal for event-driven systems where multiple components depend on a single data source. By decoupling the subject from its observers, the system becomes flexible, scalable, and easy to maintain. It is a powerful way to build flexible, maintainable, and scalable systems. By decoupling the subject from its observers, your code becomes easier to extend and reason about an essential skill for any software engineer.
메타데이터
- post_id
- bb774ef7de83
- slug
- observer-design-pattern-bb774ef7de83
- url
- https://medium.com/data-structures-and-algorithms-series/observer-design-pattern-bb774ef7de83
- canonical_url
- https://medium.com/data-structures-and-algorithms-series/observer-design-pattern-bb774ef7de83
- author_url
- https://medium.com/@stackvalley
- status
- ok
- fetched_at
- 2026-08-06 10:49:58