The Observer Design Pattern is a behavioral design pattern that defines a one-to-many dependency between objects. When the state of one object (the subject) changes, all its dependents (observers) are notified and updated automatically. This pattern is useful for implementing distributed event handling systems, decoupling the sender and receiver, and allowing multiple observers to monitor a subject’s state.
Here’s a C# example of the Observer Design Pattern with a use case for a news agency notifying subscribers about news updates:
Create the Subject Interface
Define an interface that represents the subject, which is the object that is observed.
public interface INewsSubject
{
void RegisterObserver(INewsObserver observer);
void RemoveObserver(INewsObserver observer);
void NotifyObservers();
}
Create the Concrete Subject
Implement the subject interface with a concrete class. This class will maintain a list of registered observers and notify them when there are updates.
using System;
using System.Collections.Generic;
public class NewsAgency : INewsSubject
{
private List<INewsObserver> observers = new List<INewsObserver>();
private string latestNews;
public void RegisterObserver(INewsObserver observer)
{
observers.Add(observer);
}
public void RemoveObserver(INewsObserver observer)
{
observers.Remove(observer);
}
public void NotifyObservers()
{
foreach (var observer in observers)
{
observer.Update(latestNews);
}
}
public void SetLatestNews(string news)
{
latestNews = news;
Console.WriteLine($"Latest news: {news}");
NotifyObservers();
}
}
Create the Observer Interface
Define an interface that represents the observer, which will receive updates from the subject.
public interface INewsObserver
{
void Update(string news);
}
Create Concrete Observers
Implement the observer interface with concrete observer classes that respond to updates from the subject.
using System;
public class Subscriber : INewsObserver
{
private string name;
public Subscriber(string name)
{
this.name = name;
}
public void Update(string news)
{
Console.WriteLine($"{name} received news: {news}");
}
}
Use Case – News Updates
Use the Observer Design Pattern to notify subscribers about news updates.
class Program
{
static void Main(string[] args)
{
NewsAgency newsAgency = new NewsAgency();
INewsObserver subscriber1 = new Subscriber("Subscriber 1");
INewsObserver subscriber2 = new Subscriber("Subscriber 2");
newsAgency.RegisterObserver(subscriber1);
newsAgency.RegisterObserver(subscriber2);
newsAgency.SetLatestNews("Breaking news: Pandas born at the local zoo!");
newsAgency.RemoveObserver(subscriber1);
newsAgency.SetLatestNews("Weather forecast: Sunny skies expected this weekend.");
}
}
In this example, the Observer Design Pattern allows the NewsAgency (subject) to notify its subscribers (observers) about the latest news updates. When the subject sets the latest news, it notifies all registered observers. Subscribers can receive news updates without needing to know about each other. This pattern decouples the subject and observers, promoting flexibility and reusability in event-driven systems.