Monday, March 9, 2026

Observer Design Pattern in Java

The Observer Design Pattern is a Behavioral Design Pattern used to create a one-to-many dependency between objects. When one object (called the Subject) changes its state, all its dependent objects (called Observers) are automatically notified and updated.

This pattern is widely used in event-driven systems, UI frameworks, messaging systems, and real-time applications.




Why Observer Pattern is Needed

In many applications, one object needs to notify multiple objects about changes.

For example:

  • Stock price updates

  • Notification systems

  • Social media feeds

  • Event handling systems

Instead of tightly coupling objects together, the Observer Pattern provides a loose coupling mechanism where observers subscribe to updates.


Components of Observer Pattern

The Observer Pattern consists of the following components:

1. Subject

The object that maintains a list of observers and notifies them when its state changes.

2. Observer

Objects that want to receive updates from the subject.

3. ConcreteSubject

The actual implementation of the subject that manages observers.

4. ConcreteObserver

The implementation of observers that react to updates.


Example of Observer Pattern in Java

Step 1: Observer Interface

interface Observer {
    void update(String message);
}

Step 2: Subject Interface

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

class Subject {

    private List<Observer> observers = new ArrayList<>();

    public void registerObserver(Observer observer){
        observers.add(observer);
    }

    public void removeObserver(Observer observer){
        observers.remove(observer);
    }

    public void notifyObservers(String message){
        for(Observer observer : observers){
            observer.update(message);
        }
    }
}

Step 3: Concrete Observer

class User implements Observer {

    private String name;

    User(String name){
        this.name = name;
    }

    public void update(String message){
        System.out.println(name + " received update: " + message);
    }
}

Step 4: Client Code

public class ObserverDemo {

    public static void main(String[] args) {

        Subject subject = new Subject();

        Observer user1 = new User("Alice");
        Observer user2 = new User("Bob");

        subject.registerObserver(user1);
        subject.registerObserver(user2);

        subject.notifyObservers("New Product Launched!");
    }
}

Output

Alice received update: New Product Launched!
Bob received update: New Product Launched!

Both observers receive the notification when the subject sends an update.


Advantages of Observer Pattern

Loose Coupling

Subject and observers are loosely connected.

Dynamic Subscription

Observers can be added or removed at runtime.

Supports Event-Driven Systems

Ideal for applications where events trigger updates.

Scalable Architecture

Supports multiple observers without modifying the subject.


Real-World Examples

Observer Pattern is commonly used in:

  • Java Event Handling

  • GUI frameworks (Swing / JavaFX)

  • Stock market applications

  • Notification systems

  • Messaging platforms

  • Reactive programming

Java also provides built-in support through:

  • Observer

  • Observable (deprecated but historically used)

Modern frameworks often use event listeners and reactive streams.


Observer Pattern in System Design

In large-scale distributed systems, the Observer Pattern helps implement:

  • Event-driven architecture

  • Real-time notifications

  • Microservice communication

  • Streaming data pipelines

It plays a key role in scalable backend architectures.


🚀 Learn Design Patterns in Real System Architecture

Design patterns like Observer, Builder, Factory, Singleton, and Prototype are essential for designing scalable enterprise applications.

If you want to master these concepts with real-world architecture examples, explore:

👉 No 1 System Design with Java Online Training


No comments:

Post a Comment

To build frictionless production-ready Java applications in 2026, developers must move beyond traditional coding styles and adopt modern pra...