Publisher and Subscriber Pattern in C# with Event and Delegate

Publisher / Subscriber Pattern

This Pattern have 3 components.

  • Publishers
  • Subscribers
  • Events





Publisher : It is an object which publish the event. This class contains the definition of event and delegate.
Subscriber : It is the other class or classes that accept this event, or you can say listen to that event.
Events : These are the notifications raised by the publisher and listened by the subscribers.

Now Let us create this Publisher / Subscriber Pattern with events and delegate

Publisher Class – Publisher
Subscriber Class – Subscriber
Client Class – Program

In the Publisher we are required to create
– An Event
– A Delegate
– A Method – to fire the event

Let us create 1 more class Broadcast that contains the data that the publisher is going to send/broadcast to its subscribers.

Note :
You can also define data in the Publisher class itself but for clean code separation I have created other class for the broadcasting data/message.

class Publisher
{
public string PublisherName = “seekyourcareer.in”;
public delegate void EventHandler(Publisher P, BroadCast e);
public event EventHandler EventTicked;

//create a function to check if event is fired, and handle that event with the help of event handler
public void NewPost()
{
while (true)
{

Thread.Sleep(3000); // fire event after every 3 second
if (EventTicked != null)
{
BroadCast bc = new BroadCast();
bc.BroadCast_Date = DateTime.Now;
bc.BroadCast_Message = “New Article has been Published!!!”;
EventTicked(this, bc);
}
}
}
}

class BroadCast // class containing data which is broadcasted by the publisher
{
private DateTime broadCast_Date;
public DateTime BroadCast_Date
{
get { return broadCast_Date; }
set { broadCast_Date = value; }
}

private string broadCast_Message;
public string BroadCast_Message
{
get { return broadCast_Message; }
set { broadCast_Message = value; }
}
}

Now create the Subscriber Class. A publisher can have 1 or many subscribers. I am creating two subscribers for demo purpose, you can create as per your need.

Subscriber1 Class

class Subscriber1
{
string SubscriberName = “Sudha”;
public void Listener(Publisher P) // This function listen to the event if it is raised by the Publisher
{
P.EventTicked+=P_EventTicked;
}

void P_EventTicked(Publisher P, BroadCast e) // It get executed when the event fired by the Publisher
{
Console.WriteLine(“Hey “ + SubscriberName + “, Message from “ + P.PublisherName + ” at “ + e.BroadCast_Date + ” — “ + e.BroadCast_Message);
}
}

Subscriber2 Class

class Subscriber2
{
string SubscriberName = “Arvind”;
public void Listener(Publisher P)
{
P.EventTicked += P_EventTicked;
}

void P_EventTicked(Publisher P, BroadCast e)
{
Console.WriteLine(“Hey “ + SubscriberName + “, Message from “ + P.PublisherName + ” at “ + e.BroadCast_Date + ” — “ + e.BroadCast_Message);
}
}

Client/ Main / Program Class

class Program
{
static void Main(string[] args)
{
Publisher Pub = new Publisher(); // Creating Instance of Publisher

//Create Instances of Subscribers and pass the obj the publisher class to their Listener function
Subscriber1 Sub1 = new Subscriber1();
Sub1.Listener(Pub);

Subscriber2 Sub2 = new Subscriber2();
Sub2.Listener(Pub);

Pub.NewPost(); // Make the Publisher to fire the event – When working with GUI this event could be raised by a button click.
Console.ReadKey();
}

When you run the application you can see that after every 3 seconds Publisher raise an event which is listened by the Subscribers. And the data Broadcasted by the Publisher can be caught by its subscribers. You can see the output in the below screen

Publisher Subscriber

Hope this article helps you in understanding the concept of Publisher and Subscriber Architecture.