DEV Community

Cover image for Publisher Subscriber Design Pattern with C#

Posted on

Publisher Subscriber Design Pattern with C#


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


Top comments (1)

bazen profile image

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