What is the Observer pattern and when would you use it?

The Observer pattern is a behavioral pattern where a subject maintains a list of observers and automatically notifies them whenever its state changes. It models a one-to-many dependency so that many objects can react to a single source of truth without being tightly coupled to it.

Key Points

Example

A weather station (subject) can notify multiple display units (observers) — a phone app, a website widget, and a digital billboard — whenever the temperature changes, without the weather station knowing anything about how each display renders the data.

import java.util.ArrayList;
import java.util.List;

public class WeatherObserver {

    interface Display { void update(double temperature); }

    static class WeatherStation {
        private final List<Display> displays = new ArrayList<>();
        void subscribe(Display d) { displays.add(d); }
        void unsubscribe(Display d) { displays.remove(d); }

        void setTemperature(double t) {
            System.out.println("Station: temperature is now " + t);
            for (Display d : displays) d.update(t);   // notify everyone
        }
    }

    public static void main(String[] args) {
        WeatherStation station = new WeatherStation();

        Display phone = t -> System.out.println("  Phone app shows " + t + " C");
        Display website = t -> System.out.println("  Website widget shows " + t + " C");
        Display billboard = t -> System.out.println("  Billboard shows " + Math.round(t) + " C");

        station.subscribe(phone);
        station.subscribe(website);
        station.subscribe(billboard);
        station.setTemperature(31.6);

        station.unsubscribe(website);
        station.setTemperature(29.2);
    }
}

Output

Station: temperature is now 31.6

Phone app shows 31.6 C

Website widget shows 31.6 C

Billboard shows 32 C

Station: temperature is now 29.2

Phone app shows 29.2 C

Billboard shows 29 C

Common Mistakes

Follow-up Questions

Interview Tip

A concise interview answer is

Observer models a one-to-many relationship where a subject notifies a list of registered observers whenever its state changes, without knowing what each observer does with that update. I'd reach for it any time multiple parts of a system need to react to changes in another object, like UI updates or event-driven notifications.

Last updated: 11 November 2026

Previous question: What are the drawbacks of using the Chain of Responsibility pattern?

Next question: Can you explain how the Observer pattern works in Java using Observer and Observable?

More Java Design Patterns interview questions

All Java Design Patterns questions