observer design pattern

Here is the definition of observer design pattern

The observer is know as a behavioral pattern, as it’s used to form relationship between object at run time. Its define a 1-m dependency objects so that when one object changes state, all its dependents are notified and update automatically.

Bounce! Bounce! Bounce!
OK, lets define above definition once more, little easy

In ODP an object called the subject, maintains a list of its dependents, called observers, and notify them automatically of any state change, usually by calling one of their method.

OK! OK! OK! little easy

In ODP, Observer and Subject are two interface, where all classes those implement Subject interface have a list of observer object to notify and classes those implement Observer interface have list of Subject object to be notify.

Ohhh GOD! last attempt.

Observer register with the subject and wait for notification.
Subject notify observer about a change in state. Observer take action.

When ODP come into action?

  • When we want to reduce coupling.
  • If we have an object that need to share it’s state with other, without knowing who those objects are, the observer pattern come into action.

Case Study
There is a terrorist group that the RAW(Research And Analysis Wing) are trying to catch.So we need a tracking system that track terrorist information immediately. We need to create a alert device, if terrorist are near.
There are following step we need to follow,

Step 1: Identify the observer and subject
In this case terrorist will be subject and RAW tracking device will be observer.
So we need to represent information about the terrorist in a few line of code which represent by a class.
/* A single terrorist has a location*/
class Terrorist
{
public string location;
}

The RAW device will be the observers. Which will push alert on critical notification.
/*A device has an alert status(as a Boolean), notification update(as a string), location(as a string), it can push alert & update alert.*/

class Module
{
public string location;
public Boolean alert;
public string notification,
void sendAlert()
{
if(alert)
{
Console.WriteLIne("Alert:"+notification+"Agent in "+this.location);
}
}
}

Step 2: Set up the observer and subject abstractions
The abstractions for observer and subject will be interface.

/*All observer classes implement an observer interface, it can update object data based
on subjects. Observer classes may need a list of subject to access*/
interface IObserver
{
void update();
}

/*All subject classes implement a subject interface, it can notify observer, add/remove
observer in its update list. Subject classes also need a list of observer to update*/
interface ISubject
{
void NotifyObserver();
void AddObject(IObserver obj);
void RemoveObject(IObserver obj);
}

Step 3: Modify our subject class via implement ISubject interface
class Terrorist : ISubject
{
public string location;
private List obs = new List();
public void NotifyObserver()
{
foreach (var o in obs)
{
o.update(this);
}
}

public void AddObject(IObserver obj)
{
obs.Add(obj);
}

public void RemoveObject(IObserver obj)
{
obs.Remove(obj);
}
}

Step 4: Modify our observer class via implement IObserver interface
class Module : IObserver
{
public string notification;
public Boolean alert;
public string location;
public void update(Terrorist t)
{
if (this.location == t.location)
{
this.alert = true;
this.notification = "Terrorist Nearby ";
}
else
{
this.alert = false;
this.notification = "";
}
SendAlert();
}
void SendAlert()
{
if (alert)
{
Console.WriteLine("Alert: " + notification + "Agent in " + this.location);
}
}
}

Step 5: Finally run the code
namespace d2cObserverDesignPatternSample
{
class Program
{
static void Main(string[] args)
{
//create all data objects
Terrorist t = new Terrorist();
Module m1 = new Module();
Module m2 = new Module();
Module m3 = new Module();
//Add observer
t.addObs(m1);
t.addObs(m2);
t.addObs(m3);
//create all location
string[] locs = new string[5];
locs[0] = "New Delhi";
locs[1] = "Kolkata";
locs[2] = "Mumbai";
locs[3] = "Punjab";
locs[4] = "Pune";
// Set observer to their location
m1.location = locs[1];
m2.location = locs[3];
m3.location = locs[4];
//As the terrorit relocates all over India, it will notify all device modules of its location.
//Module will trigger when a terrorist is nearby
for (int i = 0; i < 5; i++)
{
t.location = locs[i];
t.notifyObservers();
}
Console.ReadLine();
}
}
}

Output
Alert: Terrorist Nearby Agent in Kolkata
Alert: Terrorist Nearby Agent in Punjab
Alert: Terrorist Nearby Agent in Pune