Observer Design Pattern

Observer Design Pattern

In today’s episode of Software Design Patterns you will learn everything about the Observer Design Pattern. The Observer Pattern is pretty common and widely used. Compared to the already covered creational design patterns like Singleton and Factory Method the Observer is a behavioral pattern.

All the examples, source code and unit tests are in this GitHub repository.

This Blog post and the code example is strongly inspired by the Microsoft’s definition and implementation.

What’s the purpose of the Observer Design Pattern?

The Gang-of-Four [1] reference book (“Design Patterns. Elements of Reusable Object-Oriented Software“) describes the intent of the Observer Pattern as the following.

Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.

Basically you have an observable object (also often named subject) which holds a list of observers. This observable notifies all observers when a state changes. This is archived by calling one of their methods which is often called Notify() or OnNext(). In my example I have a list of newspaper which are subscribed to a news agency – called news handler. After the news handler puts out new news every subscribed newspaper will get notified. In addition to that every new newspaper will receive all existing news as well. This behavior depends on the use case – it could also be useful that you only want to receive brand-new news and not old ones.

How does the Observer look like?

In the picture below you find the Unified Modeling Language (UML) diagram.

Observer UML
Observer UML

In the linked GitHub repository you will find the News class which represents the data model for news.

The NewsPaper class implements the IObserver interface, which provides all the structure for all observers.

The NewsHandler itself implements the IObservable interface, which holds the design for the observable.

Those interfaces are from directly the System name space in .Net.

In the test project you’ll find test cases for subscribing, notifying and unsubscribing from observers.

To make the tests more readable I am using FluentAsserstions. For generating test content I highly recommend using Bogus.

If you have any questions or comments feel free to leave them below.

That’s it. Happy coding as always :).

References

[1] –Design patterns, software engineering, object-oriented programming

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.