Observer Pattern in C# – Real-Time Example (Event Notification System)
What is Observer Pattern?
The Observer Pattern is a behavioral design pattern where one object (Subject) notifies multiple dependent objects (Observers) automatically when its state changes.
It follows a:
One → Many
relationship.
Why Use Observer Pattern?
- Enable event-driven communication
- Reduce tight coupling
- Automatically notify subscribers
- Improve scalability and flexibility
Real-Time Scenario
Order Placed:
- Send Email
- Send SMS
- Push Notification
- Update Dashboard
👉 One event triggers multiple actions.
Real-Time Example – Order Notification System
Step 1: Observer Interface
public interface IObserver
{
void Update(string message);
}
Step 2: Concrete Observers
Email Service
public class EmailService : IObserver
{
public void Update(string message)
{
Console.WriteLine($"Email: {message}");
}
}
SMS Service
public class SmsService : IObserver
{
public void Update(string message)
{
Console.WriteLine($"SMS: {message}");
}
}
Push Notification Service
public class PushNotificationService : IObserver
{
public void Update(string message)
{
Console.WriteLine($"Push: {message}");
}
}
Step 3: Subject (Publisher)
public class Order
{
private readonly List<IObserver> _observers = new();
public void Subscribe(IObserver observer)
{
_observers.Add(observer);
}
public void Unsubscribe(IObserver observer)
{
_observers.Remove(observer);
}
public void Notify(string message)
{
foreach (var observer in _observers)
{
observer.Update(message);
}
}
}
Usage Example
var order = new Order();
order.Subscribe(new EmailService());
order.Subscribe(new SmsService());
order.Subscribe(new PushNotificationService());
order.Notify("Order Placed Successfully");
Output
Email: Order Placed Successfully
SMS: Order Placed Successfully
Push: Order Placed Successfully
Key Concept
Instead of:
email.Send();
sms.Send();
push.Send();
We do:
order.Notify();
👉 Subject automatically notifies all observers.
Diagram Understanding
Order (Subject)
↓
-------------------------
| | |
Email SMS PushNotification
👉 One event triggers many listeners.
Advantages
- ✔ Loose coupling
- ✔ Easy to add/remove subscribers
- ✔ Event-driven architecture support
- ✔ Flexible notification system
- ✔ Scalable communication model
Disadvantages
- ✖ Harder debugging
- ✖ Too many notifications possible
- ✖ Performance issues with many observers
- ✖ Event chains can become complex
When to Use
Use Observer Pattern when:
- Multiple objects depend on one object
- Event-driven systems are needed
- Notifications are required
- Loose coupling is important
Real Project Mapping (.NET + Angular)
| Feature | Usage |
|---|---|
| Order notifications | Observer |
| SignalR updates | Observer |
| Event bus systems | Observer |
| Email/SMS triggers | Observer |
| Real-time dashboards | Observer |
ASP.NET Core Real Example
Event Handlers
OrderPlacedEvent
├── EmailHandler
├── SmsHandler
└── InventoryHandler
👉 Event published → multiple handlers triggered.
Advanced Example – Microservices
Order Service
↓ Event
Kafka / RabbitMQ
↓
-------------------------
Email Service
Payment Service
Inventory Service
👉 Observer Pattern in distributed systems.
Observer vs Mediator
| Observer | Mediator |
|---|---|
| One-to-many notifications | Centralized communication |
| Event-driven | Controlled interaction |
| Observers don't know each other | Mediator coordinates all |
Pro Tip
Observer Pattern works well with:
- SignalR
- RabbitMQ
- Kafka
- Domain Events
- Event-driven architecture
Summary
Observer Pattern helps you:
- Build event-driven systems
- Notify multiple subscribers automatically
- Reduce direct dependencies
👉 Perfect for:
- Notifications
- Real-time systems
- Event handling
- Messaging systems
Comments
Post a Comment