liveBook · Manning

Observer pattern

As explained in Microservices Patterns, Second Edition

A behavioral design pattern in which observers subscribe to notifications published by a subject whose state or activity changes.

The Observer pattern lets a subject notify registered observers when something changes. It provides an in-memory publish/subscribe mechanism for separating secondary reactions from a module’s core responsibilities. section 6.2.4

Use it when:

  • A domain module needs to trigger an optional reaction without taking on that reaction as a core responsibility. section 6.2.4
  • Keep in mind that observers can run within the subject’s transaction. For notification delivery that should not delay order creation, asynchronous messaging provides greater decoupling. section 6.2.4
Figure 6.17 Another simple domain modular collaboration pattern is the Observer pattern. For example, the Order Management domain module can notify Notification Management when it creates an Order.
module collaboration pubsub

Shows Order Management notifying Notification Management when it creates an order. section 6.2.4

How Microservices Patterns, Second Edition covers it

How deep it goes
Defines subjects and observers and describes transaction behavior, but gives no implementation details.
Examples
Uses order creation triggering Notification Management to send a confirmation email.
What you can do after
Apply it to notify observers of domain changes while keeping secondary reactions outside the subject’s core responsibility.
How it connects
Connects to the Open-Closed Principle, transaction-per-operation, and asynchronous messaging.

Also explained in

Compare how 5 books explain observer pattern →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Microservices Patterns, Second Edition. The section links point to the parts of the book it draws on.