7
← Back to Pattern Arsenal
behavioral

Observer

must know

Stage 1: Problem

The Problem

You are building an order management system. When an order is placed, several things must happen:

typescript
1class OrderService {
2 async placeOrder(order: Order): Promise<void> {
3 await this.orderRepo.save(order);
4
5 // Send confirmation email
6 await this.emailService.sendConfirmation(order.customerEmail, order);
7
8 // Update inventory
9 await this.inventoryService.reduceStock(order.items);
10
11 // Notify analytics
12 await this.analyticsService.trackPurchase(order);
13
14 // Send to warehouse
15 await this.warehouseService.queueFulfillment(order);
16 }
17}

This works. Then:

  1. SMS notifications — marketing wants to send an SMS to customers who opted in.
  2. Loyalty points — the rewards team needs to credit points on purchase.
  3. Fraud detection — security team needs to score every order.
  4. Real-time dashboard — ops wants live order counts pushed to a dashboard via WebSocket.
  5. Partner API — a third-party marketplace needs webhook notifications.

Each new requirement means editing placeOrder. The OrderService now imports 8 different services. It's become the God method — the single point of failure for the entire order flow.

How do you decouple order placement from all the things that react to it?


Related Patterns

Confused With a Similar Pattern?

1 / 10 — Problem