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;
namespace Test
{
class Program
{
static void Main(string[] args)
{
Timeline timeline1 = new Timeline();
Timeline timeline2 = new Timeline();
TwitterAccount account1 = new TwitterAccount("CentennialStudent01");
TwitterAccount account2 = new TwitterAccount("CentennialStudent02");
timeline1.Follow(account1);
timeline1.Follow(account2);
timeline2.Follow(account2);
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();
timeline1.Unfollow(account2);
timeline1.Tweet = "How is everyone doing?";
timeline1.Tweet = "Stay safe. Stay healthy!";
timeline2.Tweet = "It is really hot! I want ice cream.";
Console.ReadKey();
}
}
}
interface IProvider
{
void Follow(IObserver observer);
void Unfollow(IObserver observer);
void Notify();
}
interface IObserver
{
void Update(IProvider provider);
}
class TwitterAccount : IObserver
{
private string username { get; set; }
public TwitterAccount(string twitterName)
{
username = twitterName;
}
public void Update(IProvider provider)
{
Timeline timeline = provider as Timeline;
if (timeline != null)
{
Console.WriteLine(String.Format("{0}'s timeline: {1}", username, timeline.Tweet));
}
}
}
class Timeline : IProvider
{
private List<IObserver> _observers;
private string _tweet;
public string Tweet
{
get { return _tweet; }
set
{
_tweet = value;
Notify();
}
}
public Timeline()
{
_observers = new List<IObserver>();
}
public void Follow(IObserver observer)
{
_observers.Add(observer);
}
public void Unfollow(IObserver observer)
{
if (_observers.Contains(observer))
_observers.Remove(observer);
}
public void Notify()
{
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.

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

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.