Table of Contents

    Publisher and Subscriber Analogy

    To understand the concept of Subscriber and Publisher with a real-life analogy, think of a magazine subscription service.

    Publisher: The Magazine Company

    The magazine company (Publisher) creates content and publishes a new issue every month. The magazine company doesn't know who exactly is going to read the magazine, but they know that people have subscribed to their magazine.

    Subscriber: The Reader

    The readers (Subscribers) sign up for a subscription to receive the monthly magazine. Whenever a new issue is published, it gets delivered to their address. The readers don't need to know how the magazine is created; they just receive it and read it.


    How This Relates to Events in C#

    1. Publisher (Magazine Company):

      • The magazine company creates and distributes magazines.
      • In C#, this is like an object that defines and raises events.
    2. Subscriber (Reader):

      • The readers subscribe to the magazine and receive new issues.
      • In C#, this is like an object that listens for events and handles them when they are raised.

    Steps in the Analogy

    1. Subscription:

      • A reader subscribes to the magazine.
      • In C#, an event handler is attached to an event.
    2. Publishing:

      • The magazine company publishes a new issue.
      • In C#, the publisher raises the event.
    3. Notification:

      • The subscribed readers receive the new issue of the magazine.
      • In C#, the event handler methods are called.

    Example Breakdown

    Magazine Company (Publisher)

    
    public class MagazineCompany
    {
        // Define a delegate
        public delegate void NewIssueEventHandler(object sender, EventArgs e);
    
        // Define an event based on the delegate
        public event NewIssueEventHandler NewIssue;
    
        // Method to raise the event
        public void PublishNewIssue()
        {
            Console.WriteLine("Magazine Company: Publishing new issue...");
            // Raise the event by calling the OnNewIssue method
            OnNewIssue(EventArgs.Empty); // EventArgs.Empty is used because there is no additional event data
        }
    
        // Protected virtual method to allow derived classes to override the event invocation behavior
        protected virtual void OnNewIssue(EventArgs e)
        {
            // Invoke the event handler, if there are any subscribers
            NewIssue?.Invoke(this, e); // The null-conditional operator (?.) ensures that Invoke is only called if NewIssue is not null
        }
    }
    
    

    Reader (Subscriber)

    
    public class Reader
    {
        // Method that will be called when the event is raised
        public void OnNewIssueReceived(object sender, EventArgs e)
        {
            Console.WriteLine("Reader: Received new issue of the magazine.");
        }
    }
    
    

    Subscription Process

    
    public class Program
    {
        public static void Main(string[] args)
        {
            // Create a magazine company (Publisher)
            MagazineCompany magazineCompany = new MagazineCompany();
            
            // Create a reader (Subscriber)
            Reader reader = new Reader();
            
            // Subscribe the reader to the magazine company's new issue event
            magazineCompany.NewIssue += reader.OnNewIssueReceived;
            
            // Publish a new issue
            magazineCompany.PublishNewIssue();
        }
    }
    
    

     

    Explanation

    • MagazineCompany:

      • Defines a delegate NewIssueEventHandler and an event NewIssue based on the delegate.
      • Method PublishNewIssue raises the event by calling OnNewIssue.
      • Method OnNewIssue checks if there are any subscribers before invoking the event handlers.
    • Reader:

      • Defines a method OnNewIssueReceived that matches the delegate signature. This method is called when the NewIssue event is raised.
    • Program:

      • Creates instances of MagazineCompany and Reader.
      • Subscribes the reader to the NewIssue event of the magazine company.
      • Calls PublishNewIssue to simulate publishing a new magazine issue, which triggers the OnNewIssueReceived method of the subscribed reader.

    This analogy and example illustrate how events in C# allow a publisher to notify subscribers when something of interest occurs, much like a magazine company informs its subscribers about new issues.