Published Date: 16 October, 2021

Overview : Observer Pattern

In observer pattern, you can send the signal to the multiple objects at a specific event. Suppose you have an object whose status can be changed at any point of time and you would like to send the signal when the status of the object got change. So, here status change is an event and you can send the status change signal to the n number of objects at the moment object status will change. Whichever objects want to get this signal, those objects have to register themselves. The object which event is being observed can be called publisher or subject and the objects which want to be notified can be called as subscriber or receiver.

We can relate this the real-world scenario to understand this better. Suppose you are running a video library and you would like to update your customer for your upcoming album or movie. Here you are the publisher and your customer is the subscriber and launching a new video in your library is an event.

In this pattern, we need to consider following things:

  1. Registering of the object for getting update (in the above example, registration of your customer)
  2. Notifying to the customers
  3. Allow to deregister themselves in case they are not interested to get notification

First this first, you need to define the definition of publisher, here about your video library

public interface IVideoLibrary
{
void Register(ICustomer customer);
void DeRegister(ICustomer customer);
void NotifyRelease(string movieName);
}

Now need to define about your subscriber, here your customer is your subscriber

public interface ICustomer
{
string CustomerName { get; set; }
void MovieReleaseUpdate(string movieName);
}

Implementation of publisher

public class VideoLibrary : IVideoLibrary
{

List<ICustomer> Customers = null;
public VideoLibrary()
{

Customers = new List<ICustomer>();

}

public void DeRegister(ICustomer customer)
{

Customers.Remove(customer);
Console.WriteLine("Customer removed successfully" + customer.CustomerName);

}

public void NotifyRelease(string movieName)
{

foreach(ICustomer customer in Customers)
{

customer.MovieReleaseUpdate(movieName);

}

}

public void Register(ICustomer customer)
{

Customers.Add(customer);
Console.WriteLine("Customer registered successfully" + customer.CustomerName);

}

}

Implementation of Subscriber

public class Customer : ICustomer
{

public string CustomerName { get; set; }
public Customer(string customerName)
{

this.CustomerName = customerName;

}

public void MovieReleaseUpdate(string movieName)
{

Console.WriteLine(string.Format("Customer: {0} notified for Movie:{1}", this.CustomerName, movieName));

}

}

Now you are ready to test your application

class Program
{

static void Main(string[] args)
{

IVideoLibrary movie = new VideoLibrary();

ICustomer customer1 = new Customer("Customer1");
movie.Register(customer1);

ICustomer customer2 = new Customer("Customer2");
movie.Register(customer2);

ICustomer customer3 = new Customer("Customer3");
movie.Register(customer3);

movie.NotifyRelease("Movie-1");
movie.NotifyRelease("Movie-2");

movie.DeRegister(customer2);

movie.NotifyRelease("Movie-3");
Console.ReadLine();

}

}

Output:

http://www.tekkishare.com

Most Recent Post

DECORATOR

OBSERVER

Subscription
Tags
C-Sharp Design-Pattern