Observer Design Pattern in C#

Observer design pattern comes under behavioral design pattern category of Gang of four (GoF) design patterns. This article explains what observer pattern is and how to implement observer design pattern in C#.

Observer Design Pattern

The observer design pattern is based on Hollywood Principle, which implies – Don’t call us, we will call you. It is also known as publisher-subscriber design pattern.

Observer design pattern includes observers (subscriber) and a Subject (Provider, Observable or Publisher). One or many observers can subscribe to the providers for any specific interest. When some specified condition occurs in the Subject object, it will send notification to all of the subscribed observers.

This pattern is also helpful in any application to send push notifications to the subscribed users.

Gang of Four(GoF) Definition:
Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.

Observer Design Pattern – UML Diagram

Observer Design Pattern in C# - UML Diagram

Participants in the above UML diagram are:

  • ISubject: Interface for Concrete Subject class. Defines Attach (subscribe), Detach (unsubscribe) and notify (publish) methods.
  • ConcreteSubject: Subject will send notification message to the observers.
  • IObserver: Interface for the Observer class.
  • ConcreteObserver: Observer will receive notification from the Subject.

Observer Design Pattern in C# – Example

Problem Statement

Consider a scenario of eCommerce website, where user can subscribe to any product. If there will be any discount on the product then the eCommerce website will send a notification message to all of the subscribed customers.

Class Diagram

Observer Design Pattern in C# - Class Diagram

Participants in the above class diagram are:

  • IProduct: ISubject interface
  • Product: Concrete Subject class
  • ICustomer: IObserver interface
  • Customer: Concrete Observer class

Observer Pattern in C# – Implementation Code

Result

Observer Design Pattern in C# - Result

Hope you get the idea about how to implement observer design pattern in C#. Please post your comments for queries and feedback. Thanks for reading this article.

Share Button
The following two tabs change content below.
Gyanendu Shekhar is a technology enthusiast. He loves to learn new technologies. His area of interest includes Microsoft technologies, Augmented reality, Virtual reality, unity3d and android development.

Gyanendu Shekhar

Gyanendu Shekhar is a technology enthusiast. He loves to learn new technologies. His area of interest includes Microsoft technologies, Augmented reality, Virtual reality, unity3d and android development.

One thought on “Observer Design Pattern in C#

  • April 11, 2018 at 4:09 pm
    Permalink

    Hi,
    Please give an example of Repository Pattern in c#

    Thanks,

    Reply

Leave a Reply

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