Observer Pattern in Java: Explanation and Example
Observer Pattern in Java is a design pattern where an object, called the subject, maintains a list of dependents called observers and notifies them automatically of any state changes. It helps create a one-to-many relationship so that when the subject changes, all observers get updated without tight coupling.How It Works
Imagine you have a newspaper subscription. The newspaper company is the subject, and the subscribers are the observers. When a new edition is published, the company sends the newspaper to all subscribers automatically. This way, subscribers get updates without asking every time.
In Java, the subject keeps a list of observers and notifies them when its state changes. Observers register themselves to the subject to receive updates. This pattern helps keep parts of a program loosely connected, making it easier to manage changes and updates.
Example
This example shows a simple weather station (subject) that notifies display devices (observers) when the temperature changes.
import java.util.ArrayList; import java.util.List; // Observer interface interface Observer { void update(float temperature); } // Subject class class WeatherStation { private List<Observer> observers = new ArrayList<>(); private float temperature; public void addObserver(Observer observer) { observers.add(observer); } public void removeObserver(Observer observer) { observers.remove(observer); } public void setTemperature(float temperature) { this.temperature = temperature; notifyObservers(); } private void notifyObservers() { for (Observer observer : observers) { observer.update(temperature); } } } // Concrete observer class TemperatureDisplay implements Observer { private String name; public TemperatureDisplay(String name) { this.name = name; } @Override public void update(float temperature) { System.out.println(name + " display: Temperature updated to " + temperature + "°C"); } } public class ObserverPatternDemo { public static void main(String[] args) { WeatherStation station = new WeatherStation(); TemperatureDisplay display1 = new TemperatureDisplay("Living Room"); TemperatureDisplay display2 = new TemperatureDisplay("Bedroom"); station.addObserver(display1); station.addObserver(display2); station.setTemperature(25.0f); station.setTemperature(30.5f); } }
When to Use
Use the Observer Pattern when you want to create a system where multiple parts need to stay updated about changes in one part without tightly linking them. It is useful in event handling, user interface updates, and real-time data feeds.
For example, in a chat app, when one user sends a message, all other users (observers) should get notified. Or in a stock market app, when stock prices change, all interested displays update automatically.
Key Points
- The subject maintains a list of observers and notifies them of changes.
- Observers register and unregister themselves to receive updates.
- This pattern promotes loose coupling between objects.
- It is widely used in event-driven programming and UI frameworks.