← Back to list

Observer Design Pattern

Subscriber and Publisher Mechanism

Santhosh Kumar in Stack Valley · 2026-02-07 12:55 · 7 claps · 2.3 min read
#design-patterns #java #observer-design-pattern #software-architecture #data-structures
Open on Medium ↗
Wiki topics: 🏛️ · Architecture

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.

  1. A *‘*WeatherStation’ **collects temperature data
  2. Multiple displays want to show the temperature:
  3. Mobile App
  4. Web Dashboard
  5. 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

  1. WeatherStation must be modified every time a new display is added
  2. Strong dependency on display implementations
  3. Not scalable

How to Implement Observer Design Pattern?

  1. Create an Observer interface
  2. Create a Subject interface
  3. Implement WeatherStation as ConcreteSubject
  4. Implement displays as ConcreteObservers
  5. 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

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