Skip to content
Veltoryx Technologies
ProLesson 6.2Intermediate7 min readUpdated 31 October 2026

Observer Pattern

Learn how one object can tell many others when it changes, without knowing who they are, using an order status example, and how Spring events and the JDK use the same idea.

When you subscribe to a YouTube channel, you do not visit the channel every hour to check whether there is a new video. You subscribe once, and when the creator uploads something, YouTube tells you. The creator does not know who you are or how many subscribers there are, and does not care. They simply publish, and everyone who subscribed gets the news. If you lose interest, you unsubscribe, and the creator carries on without noticing any change in their work.

The Observer pattern is this "subscribe and be told" idea for objects. One object, the subject, keeps a list of interested objects, the observers (or listeners). When the subject changes, it notifies all of them automatically. The subject does not need to know what the observers do with the news.

01The problem: the object that must call everybody

An online shop has an Order class. When an order changes status, for example from PLACED to SHIPPED, several things must happen: the customer gets an email, the admin dashboard refreshes, the inventory is updated. The straightforward first version looks like this:

Before: Order calls every interested part itself
public class Order {

    private OrderStatus status = OrderStatus.PLACED;

    private final EmailService emailService;
    private final Dashboard dashboard;
    private final InventoryService inventory;

    public Order(EmailService emailService, Dashboard dashboard, InventoryService inventory) {
        this.emailService = emailService;
        this.dashboard = dashboard;
        this.inventory = inventory;
    }

    public void changeStatus(OrderStatus newStatus) {
        this.status = newStatus;
        emailService.sendStatusEmail(this);      // Order knows about the email system
        dashboard.refresh(this);                 // and the dashboard
        inventory.update(this);                  // and the inventory
    }
}

This looks harmless, but Order, a core business class, now knows about every part that reacts to it:

  • Adding a new reaction, such as an SMS or an analytics call, means editing Order again.
  • Order depends on the email system, the dashboard and the inventory. It cannot be created or tested without them.
  • Reactions that only matter sometimes (an email only for SHIPPED) clutter the order's code.
  • The Order class breaks the Single Responsibility Principle: it manages an order and coordinates its side effects.

Loading the rest of the lesson…