top of page
Writer's pictureNimesh Patel

Understanding the Observer Pattern in 3 Minutes

The Observer Pattern is a fundamental design pattern in software development that allows an object (called the "subject") to notify other objects (called "observers") of any changes in its state. This pattern promotes loose coupling, making it easier to maintain and modify your code. In this quick 3-minute read, we'll explore the basics of the Observer Pattern and how to implement it in your software design.



Observer Pattern Example in C#


using System;
using System.Collections.Generic;

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

public interface ISubject {
  void Attach(IObserver observer);
  void Detach(IObserver observer);
  void Notify(string eventDetails);
}

public class Subject : ISubject {
  private List<IObserver> _observers = new List<IObserver>();

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

  public void Detach(IObserver observer) {
    _observers.Remove(observer);
  }

  public void Notify(string eventDetails) {
    foreach (var observer in _observers) {
      observer.Update(eventDetails);
    }
  }
}

public class ConcreteObserver : IObserver {
  public void Update(string eventDetails) {
    Console.WriteLine($"ConcreteObserver received event: {eventDetails}");
  }
}

public class Program {
  public static void Main(string[] args) {
    // Create a subject instance
    Subject subject = new Subject();

    // Create observer instances
    ConcreteObserver observer1 = new ConcreteObserver();
    ConcreteObserver observer2 = new ConcreteObserver();

    // Attach observers to the subject
    subject.Attach(observer1);
    subject.Attach(observer2);

    // Notify observers of an event
    subject.Notify("An event just occurred!");

    // Detach an observer and notify again
    subject.Detach(observer1);
    subject.Notify("Another event occurred!");
  }
}


In this C# example, we've defined the IObserver and ISubject interfaces, and created concrete classes Subject and ConcreteObserver that implement these interfaces. The Subject class maintains a list of observers and provides methods to attach, detach, and notify observers. The ConcreteObserver class defines how to handle the received event.

The Program class demonstrates how to use the Observer Pattern by creating a Subject instance, attaching ConcreteObserver instances, and notifying observers of events.


Using the Observer Pattern

To use the Observer Pattern in your software design, follow these steps:

  • Create a Subject instance that holds the state you want to monitor.

  • Create concrete Observer instances that define how to respond to state changes.

  • Attach the Observer instances to the Subject using the attach method.

  • When the Subject's state changes, call its notify method to update all Observers.


Conclusion:

The Observer Pattern is a powerful design pattern that helps you maintain clean, modular code by decoupling components that need to react to changes in another component's state. By implementing the Observer Pattern, you can easily add, remove, or modify observers without affecting the rest of your code. In just 3 minutes, you've learned the basics of the Observer Pattern and can start applying it in your software projects.

5 views0 comments

Recent Posts

See All

Comments


bottom of page