Observer design pattern in C++ with real life example

As we know, there are three types of design patterns such as 1. Abstract design pattern 2. Structural design pattern 3.Behavioral design pattern  but it is important to know that the Observer design pattern is a behavioral design pattern

Intent

There will be one too many dependencies among different objects  if the subject state changes its state then all its dependents  will be updated and notified

Rendered by QuickLaTeX.com

Suppose that we have the bus depo station in a city and we consider it a subject  which has three terminals  to display time info of the bus leaving from station to passenger  

The terminals which display the bus leaving timeto passengers we consider it  as an observer

Now, The problem with the bus depo station is that it constantly changes its internal info states from  1 to some state  

Why?

Because buses will come and go to their destination and its time of leaving buses changes constantly then how do we update all the terminals accurate information of buses?

The answer is by using an observer design pattern through which we can attach and detach bus info on the terminal display and can notify

#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <algorithm>
using namespace std;
class Iterminal
{
public:
    virtual void newUpdateShow(float Time) = 0;
};

class Busdepo :public Iterminal
{
    string name;
    float Time;
public:
    Busdepo(string name)
    {
        this->name = name;
    }
    void newUpdateShow(float Time)
    {
        this->Time = Time;
        cout << "Bus at "<< name << " Will leave at "<< Time << "\n";
    }
};
class BusdepoOperationSubject
{
    vector<Busdepo*> list;
    vector<Busdepo*>::iterator itr;
public:
      void AttachInfo(Busdepo *Busdepo)
      {
          list.push_back(Busdepo);
      }
      void DetachInfo(Busdepo *Busdepo)
      {
         list.erase(std::remove(list.begin(), list.end(),Busdepo ), list.end());
      }
      void notifyInfo(float Time)
      {
          for(vector<Busdepo*>::const_iterator iter = list.begin(); iter != list.end(); ++iter)
          {
              if(*iter != 0)
              {
                  (*iter)->newUpdateShow(Time);
              }
          }
      }
};
class UpdateTimeInfo : public BusdepoOperationSubject
{
public:
     void Changetime(float Time)
     {
         notifyInfo(Time);
     }
};
int main()
{

    UpdateTimeInfo Display;

    Busdepo Terminal1 ("Terminal 1");
    Busdepo Terminal2 ("Terminal 2");
     Busdepo Terminal3 ("Terminal 3");
    Display.AttachInfo(&Terminal1 );
    Display.AttachInfo(&Terminal2 );
     Display.AttachInfo(&Terminal3 );
    Display.Changetime(10.35);
    Display.DetachInfo(&Terminal2 );
    Display.Changetime(2.45);
    return 0;
}

Output

Bus at Terminal 1 will leave at 10.35
Bus at Terminal 2 will leave at 10.35
Bus at Terminal 3 will leave at 10.35
Bus at Terminal 1 will leave at 2.35
Bus at Terminal 3 will leave at 2.35

Mohammed Anees

Hey there, welcome to aneescraftsmanship I am Mohammed Anees an independent developer/blogger. I like to share and discuss the craft with others plus the things which I have learned because I believe that through discussion and sharing a new world opens up

Leave a Reply

Your email address will not be published.