Observer Pattern in C# – Real-Time Example (Order Notification System)

What is Observer Pattern?

The Observer Pattern is a behavioral design pattern where one object (subject) notifies multiple objects (observers) when a change occurs.

It creates a one-to-many relationship between objects.


Why Use Observer Pattern?

  • Decouple sender from receivers
  • Easily add/remove notification channels
  • Build event-driven systems
  • Avoid tight coupling

Real-Time Scenario

In an Order System:

  • When an order is placed → multiple actions should happen
    • Email notification
    • SMS notification
    • Push notification

Instead of hardcoding all these inside Order logic, we use observers.


Implementation

Step 1: Observer Interface


public interface IObserver
{
    void Update(string message);
}

Step 2: Concrete Observers


public class EmailService : IObserver
{
    public void Update(string message)
    {
        Console.WriteLine($"Email: {message}");
    }
}

public class SmsService : IObserver
{
    public void Update(string message)
    {
        Console.WriteLine($"SMS: {message}");
    }
}

Step 3: Subject (Publisher)


public class Order
{
    private List<IObserver> _observers = new();

    public void Subscribe(IObserver observer)
    {
        _observers.Add(observer);
    }

    public void Notify(string message)
    {
        foreach (var obs in _observers)
            obs.Update(message);
    }

    public void PlaceOrder()
    {
        Console.WriteLine("Order Placed");
        Notify("Your order has been placed successfully");
    }
}

Usage Example


var order = new Order();

order.Subscribe(new EmailService());
order.Subscribe(new SmsService());

order.PlaceOrder();

Output


Order Placed
Email: Your order has been placed successfully
SMS: Your order has been placed successfully

Key Concept

Instead of:


SendEmail();
SendSms();
SendNotification();

We do:


Notify("Order placed");

👉 Observers handle the rest automatically


Advantages

  • Loose coupling between objects
  • Easy to add new observers (e.g., WhatsApp notification)
  • Scalable and flexible
  • Supports event-driven architecture

Disadvantages

  • Can create many small classes
  • Harder to trace flow (debugging)
  • All observers get notified (may not always be needed)

When to Use

  • Multiple actions triggered by single event
  • Event-based systems (notifications, logging)
  • Microservices communication (events)
  • Real-time updates

Real Project Mapping (.NET + Angular)

  • Backend: Order service publishes event
  • Observers: Email, SMS, Push services
  • Angular: User gets real-time updates

Pro Tip (Advanced .NET Usage)

Use built-in event system:


public event Action<string> OnOrderPlaced;

Or integrate with:

  • MediatR (for domain events)
  • Message queues (RabbitMQ, Kafka)

Summary

Observer Pattern helps you:

  • Trigger multiple actions from one event
  • Keep code clean and decoupled
  • Build scalable notification systems

👉 Perfect for Order systems, Notifications, Event-driven apps

Comments