Publish Subscribe Pattern

How to use publish subscribe pattern to display live date time to multiple subscribers


Category: Design Patterns Tags: C#

Publish Subscribe Pattern Code Files

Introduction

    Suppose there is a server which publishes date and time and there are multiple clients wants to display the time and date in their own format. In this case we can use publish and subscribe pattern. Subscribers will be subscribing to the publisher’s date time event and publisher will publish the live date time data to the subscribers.

Implementation

    Let’s create a delegate first which accepts DateTime as parameter.

using System;

namespace PublishSubscribePattern
{
public delegate void DateTimeDelegate(DateTime dateTime);
}

Now create a publisher interface and class.

namespace PublishSubscribePattern
{
public interface IPublisher
{
void Subscribe(DateTimeDelegate dateTimeHandler);
}
}
using System;
using System.Threading;

namespace PublishSubscribePattern
{
public class Publisher : IPublisher
{
private event DateTimeDelegate _dateTimeEvent;

/// <summary>
/// Subscribe to event
/// </summary>
/// <param name="dateTimeHandler"></param>
public void Subscribe(DateTimeDelegate dateTimeHandler)
{
_dateTimeEvent += dateTimeHandler;
}

/// <summary>
/// Start publishing date time
/// </summary>
public void Start()
{
while (true)
{
if (_dateTimeEvent != null)
_dateTimeEvent(DateTime.Now);

Thread.Sleep(1000);
}
}
}
}

Publisher has a method Start which is used to publish live date time data. Each subscriber has to call Subscribe method and provide its DateTime Handler.

Now create Subscriber Interface which can display DateTime information.

using System;

namespace PublishSubscribePattern
{
public interface ISubscriber
{
void Display(DateTime dateTime);
}
}

Implement two different classes to display Date and Day.

using System;

namespace PublishSubscribePattern
{
class DateTimeSubscriber : ISubscriber
{
public void Display(DateTime dateTime)
{
Console.WriteLine("Date time: " + dateTime);
}
}
}
using System;

namespace PublishSubscribePattern
{
class DaySubscriber : ISubscriber
{
public void Display(DateTime dateTime)
{
Console.WriteLine("Day today: " + dateTime.DayOfWeek);
}
}
}

Now let’s run above code.

namespace PublishSubscribePattern
{
class Program
{
static void Main(string[] args)
{
var publisher = new Publisher();

var dateSub = new DateTimeSubscriber();
var daySub = new DaySubscriber();

publisher.Subscribe(dateSub.Display);
publisher.Subscribe(daySub.Display);

publisher.Start();
}
}
}

Output

Date time: 23/04/2022 12:51:28 pm

Day today: Saturday

Date time: 23/04/2022 12:51:29 pm

Day today: Saturday

Date time: 23/04/2022 12:51:30 pm

Day today: Saturday

Date time: 23/04/2022 12:51:31 pm

Day today: Saturday

 


Like 0 People
Last modified on 23 April 2022
Nikhil Joshi

Nikhil Joshi
Ceo & Founder at Dotnetlovers
Atricles: 164
Questions: 16
Given Best Solutions: 16 *

Comments:

No Comments Yet

You are not loggedin, please login or signup to add comments:

Existing User

Login via:

New User



x