C# Observer Models in Real Life

Understanding Twitter

First of all, what is the "Observer pattern"? Basically, it is a design pattern that allows subscribers to get notifications from a provider. With at least one "provider", any number of observers can receive notifications if certain conditions are met. A real life application of this design pattern is the popular social media platform, Twitter!

The following code is my take on applying Observer models to real life to better understand them.

using System;
using System.Collections.Generic;

//simple application for basic observer model understanding
namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            //creating new Timeline objects
            Timeline timeline1 = new Timeline();
            Timeline timeline2 = new Timeline();

            //create TwitterAccount objects
            TwitterAccount account1 = new TwitterAccount("CentennialStudent01");
            TwitterAccount account2 = new TwitterAccount("CentennialStudent02");

            //twitter accounts are going to subscribe to the notifications
            timeline1.Follow(account1);
            timeline1.Follow(account2);
            timeline2.Follow(account2);

            //create tweets to be sent out as "notifications"
            timeline1.Tweet = "Today is a nice day.";
            timeline2.Tweet = "Midterms are coming soon."; 
            timeline1.Tweet = "I ate a lot of pasta for lunch.";
            timeline1.Tweet = "This is my third message on this account!";

            Console.ReadLine();

            //"CentennialStudent02" will now be unsubscribed
            timeline1.Unfollow(account2);

            //create new tweets 
            timeline1.Tweet = "How is everyone doing?";
            timeline1.Tweet = "Stay safe. Stay healthy!";
            timeline2.Tweet = "It is really hot! I want ice cream.";

            Console.ReadKey();
        }
    }
}

//generic provider interface
interface IProvider
{
    //call this method for subscribing  
    void Follow(IObserver observer);

    //call this method for unsubscribing
    void Unfollow(IObserver observer);

    //call this method to notify observers of any changes 
    void Notify();
}

//generic observer interface 
interface IObserver
{
    //this method needs to be called when changes are made
    void Update(IProvider provider);
}

//create TwitterAccount class
class TwitterAccount : IObserver
{ 
    //username property for a twitter account
    private string username { get; set; }
    public TwitterAccount(string twitterName)
    {
        username = twitterName;
    }

    //when the twitter account creates a new message ...
    //call this method to update all observers
    public void Update(IProvider provider)
    {
        //create timeline object as provider
        Timeline timeline = provider as Timeline;

        if (timeline != null)
        {
            //output twitter account's tweet message
            Console.WriteLine(String.Format("{0}'s timeline: {1}", username, timeline.Tweet));
        }
    }
}

//Timeline class
class Timeline : IProvider
{
    //create list of observers
    private List<IObserver> _observers;

    //tweet property - the posted message
    private string _tweet;

    //getter for tweets ...
    //since user messages can be private on Twitter (unless you follow them)
    public string Tweet
    {
        get { return _tweet; }
        set
        {
            _tweet = value;
            //when there is a change, it will notify all observers
            Notify();
        }
    }

    //Timeline constructor
    public Timeline()
    {
        _observers = new List<IObserver>();
    }

    //Follow method
    public void Follow(IObserver observer)
    {
        _observers.Add(observer);
    }

    //Unfollow method
    public void Unfollow(IObserver observer)
    {
        //remove the observer from observer list when unsubscribing
        if (_observers.Contains(observer))
            _observers.Remove(observer);
    }

    //Notify method
    public void Notify()
    {
        //send notifications to each observer
        foreach (var o in _observers)
            o.Update(this);
    }
}

My application is an observer model program that imitates the notification system architecture of the Twitter platform. Here, messages are "broadcasted" and only subscribers can receive these messages.

Timelines are the lists of messages for a Twitter account. As such, only those that are subscribed can read them. If you are not following that particular timeline, then you will not be notified of these messages.

Subscribing and Unsubscribing are essentially the Follow and Unfollow buttons on Twitter pages.

image.png

CentennialStudent01 and CentennialStudent02 are subscribed to timeline1, but only CentennialStudent02 is subscribed to timeline2. Only CentennialStudent02 received the "Midterms are coming soon." message.

image.png

The break in the middle is where CentennialStudent02 unsubscribes from timeline1. This means that CentennialStudent02 no longer receives messages from timeline1. CentennialStudent01 is the only one that has received the "How is everyone doing?" and "Stay safe. Stay healthy!" message.