DEV Community

Cover image for Publisher Subscriber Design Pattern with C#
manoj
manoj

Posted on

Publisher Subscriber Design Pattern with C#

pub-sub

Publisher-Subscriber (pub-sub) is a design pattern that allows loose coupling between the application components.
Also the pattern is an implementation of event-driven architecture. For implementing this pattern we will mainly write two classes Publisher Class and Subscriber Class.

The major difference in Pub-Sub design is the event channel that publisher generates to notify changes to its subscriber without any knowledge of subscriber existence.

Realtime Examples:

  • Publisher class publishes an event ( like Youtube channels video notification ) for its' subscribers using an EventHandler.
  • Subscriber class receives the event ( like Youtube channel subscribers ) and handles it as it's needed.
  • EventHandler involved in this process of Publisher-Subscriber pattern to get notifications from Youtube Channel (Publisher) and send it to Channel subscribers.
  • Publisher class and Subscriber class doesn't have to know each other they both are connected to EventHandler.
  • Publisher will send the NotificationEvent to EventHandler and it will send the NotificationEvent to Subscribers.

Alt Text

Cheers!!

Top comments (1)

Collapse
 
bazen profile image
Bazen

It would be good to have sample code since the title states C#