Simple Publisher/Subscriber (Observer Pattern) Sample With Delegates/Events in C#

I was having a great conversation with one of the developers where I work last week and he was asking me about delegates and events. I figured I’d create a sample for him – and hey, why not – let’s post it here (easier than emailing the code anyway…)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;

namespace DelegateEventSample
{
    [TestFixture]
    public class TestSample
    {
        [Test]
        public void Main()
        {
            Publisher publisherService = new Publisher();
            ESPNNEws espnNews = new ESPNNEws(publisherService);
            CBSSportsNews cbsNews = new CBSSportsNews(publisherService);

            publisherService.NewsUpdate("Mizzou 14, Kansas 0 at the half...");
        }
    }

    public class Publisher
    {
        public delegate void NewUpdateDelegate(object sender, NewsInfoArgs args);
        public event NewUpdateDelegate NewUpdateEvent;

        public void OnNewUpdateEvent(object sender, NewsInfoArgs args)
        {
            if(NewUpdateEvent != null)
                NewUpdateEvent(sender, args);
        }

        public void NewsUpdate(string news)
        {
            OnNewUpdateEvent(this, new NewsInfoArgs(news));
        }
    }

    public class NewsInfoArgs : EventArgs
    {
        private readonly string thenews;

        public NewsInfoArgs(string thenews)
        {
            this.thenews = thenews;
        }

        public string TheNews
        {
            get{ return thenews;}
        }
    }

    public class ESPNNEws
    {
        private Publisher newPublisher;
        public ESPNNEws(Publisher newPublisher) //inject publisher service
        {
            this.newPublisher = newPublisher;
            newPublisher.NewUpdateEvent += newPublisher_NewUpdateEvent;
        }

        static void newPublisher_NewUpdateEvent(object sender, NewsInfoArgs args)
        {
            Console.WriteLine("ESPN reporting... " + args.TheNews);
        }
    }

    public class CBSSportsNews
    {
        private Publisher newPublisher;
        public CBSSportsNews(Publisher newPublisher) //inject publisher service
        {
            this.newPublisher = newPublisher;
            newPublisher.NewUpdateEvent += newPublisher_NewUpdateEvent;
        }

        static void newPublisher_NewUpdateEvent(object sender, NewsInfoArgs args)
        {
            Console.WriteLine("CBSSportsNews on 11 oclock news... " + args.TheNews);
        }
    }
}

Advertisements

2 thoughts on “Simple Publisher/Subscriber (Observer Pattern) Sample With Delegates/Events in C#

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s