Thilan Dissanayaka Software Architecture Apr 26

Observer Pattern explained simply

When one object needs to notify many other objects about changes in its state automatically,
the Observer Pattern steps in.

What is the Observer Pattern?

  • Defines a one-to-many dependency between objects.
  • When the subject changes, all its observers are notified and updated automatically.
  • Helps achieve loose coupling between components.

In short:

One subject, many listeners!

Real-Life Analogy

Think about a YouTube Channel:

  • You subscribe (observer) to a channel (subject).
  • When the channel posts a new video, you get a notification.

Simple and automatic.

Structure

  • Subject (Observable): Holds a list of observers and notifies them of changes.
  • Observers: Register themselves to the subject and react to changes.

Example in Java

Subject Interface

public interface Subject {
    void attach(Observer observer);
    void detach(Observer observer);
    void notifyObservers();
}

Observer Interface

public interface Observer {
    void update(String message);
}

Concrete Subject

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

public class Channel implements Subject {
    private List<Observer> subscribers = new ArrayList<>();
    private String latestVideo;

    @Override
    public void attach(Observer observer) {
        subscribers.add(observer);
    }

    @Override
    public void detach(Observer observer) {
        subscribers.remove(observer);
    }

    @Override
    public void notifyObservers() {
        for (Observer observer : subscribers) {
            observer.update(latestVideo);
        }
    }

    public void uploadNewVideo(String title) {
        this.latestVideo = title;
        System.out.println("New video uploaded: " + title);
        notifyObservers();
    }
}

Concrete Observer

public class Subscriber implements Observer {
    private String name;

    public Subscriber(String name) {
        this.name = name;
    }

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

Using the Observer Pattern

public class MainProgram {
    public static void main(String[] args) {
        Channel techChannel = new Channel();

        Subscriber alice = new Subscriber("Alice");
        Subscriber bob = new Subscriber("Bob");
        Subscriber charlie = new Subscriber("Charlie");

        techChannel.attach(alice);
        techChannel.attach(bob);

        techChannel.uploadNewVideo("Observer Pattern in Java!");

        techChannel.attach(charlie);

        techChannel.uploadNewVideo("Singleton Pattern Explained!");
    }
}

Output:

New video uploaded: Observer Pattern in Java!
Alice received notification: Observer Pattern in Java!
Bob received notification: Observer Pattern in Java!
New video uploaded: Singleton Pattern Explained!
Alice received notification: Singleton Pattern Explained!
Bob received notification: Singleton Pattern Explained!
Charlie received notification: Singleton Pattern Explained!

Why Use Observer Pattern?

  • Loose Coupling: Subjects and observers are independent.
  • Dynamic Relationships: Observers can be added/removed at runtime.
  • Scalability: Suitable when multiple objects depend on a single object.

Real-World Use Cases

  • Event handling systems (GUI frameworks, button click listeners).
  • Messaging systems (notifications, chat applications).
  • Distributed systems (monitoring services).
  • Model-View-Controller (MVC) architecture (model updates views).

Summary

The Observer Pattern lets you build dynamic, decoupled, and event-driven applications.
It’s perfect whenever you need automatic notifications between components without tightly coupling them.

ALSO READ
Debugging Binaries with GDB
Mar 23 Low-level Development

GDB is shipped with the GNU toolset. It is a debugging tool used in Linux environments. The term GDB stands for GNU Debugger. In our previous protostar stack0 walkthrough tutorial, we used GDB....

SQL injection login bypass
Apr 26 Web App Hacking

SQL Injection (SQLi) is one of the oldest and most fundamental web application vulnerabilities. While it’s becoming rarer in modern web apps due to better coding practices and frameworks,....

REST API - Interview preparation guide
May 08 Interview Guides

## What is a REST API? A REST (Representational State Transfer) API is an architectural style for designing networked applications. It uses standard HTTP methods to interact with resources, making....

Ballerina connector for Hubspot Schema API
Mar 23 WSO2

Hi all, It's a new article on something cool. Here we are going to see how we can use the Hubspot schema connector with Ballerina. When it comes to building connectors for seamless integration....

Time based Blind SQL Injection
Apr 26 Web App Hacking

Blind SQL Injection happens when: There is a SQL injection vulnerability, BUT the application does not show any SQL errors or query outputs directly. In this case, an attacker has to ask....

Exploiting a Stack Buffer Overflow on Windows
May 17 Exploit development

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut....