Observar Design Pattern

This is a behavioural pattern.

The observer pattern is a design pattern that defines a link between objects so that when one object's state changes, all dependent objects are updated automatically.


1. Suppose there are three users Anil,Prashant and Asha. All three wants to purchase same product  book which is not in stock on E-Commerce website.
2. So all our three users used a Notify option given by E-Commerce website. In this case website should notify a users when the product will be available.






Implementation:


There are 2 basic class in observer design pattern
1. Subject  2. Observer


Subject: The main responsiblilty of this class is to maintain the user list and update all subscribed users if any change happen .

    vector<Users*> userlist;
    void Attach(Users *user);
    void Detach(Users *user);
    void Notify(bool isavailable);


Observer: This is the basic class of observer pattern. all user should inherit form this & the subject should contain the userlist of this class.


#include<iostream>
#include <vector>
using namespace std;

class ASubject
{
    vector<Users*> userlist;
public:
    void Attach(Users *user);
    void Detach(Users *user);
    void Notify(bool isavailable);
};

void ASubject::Attach(Users *user)
{
    userlist.push_back(user);
}
void ASubject::Detach(Users *user)
{   
     int count = userlist.size();
     int i;

      for (i = 0; i < count; i++) {
       if(userlist[i] == user)
            break;
}
if(i < count)
  userlist.erase(userlist.begin() + i);
    
}

void ASubject::Notify(bool isavailable)
{
    if(!isavailable)
        return;
     int count = userlist.size();
     int i;

    for (int i = 0; i < count; i++)
       (userlist[i])->Update(True);
   
}

class Book : public ASubject
{
public:
    void Changeavailability(bool isavailable);
};

void Book::Changeavailability(bool isavailable)
{
   if (isavailable)
      Notify(isavailable);
}

#pragma once
class IObserver
{
public:
    virtual void Update(bool isavailable) = 0;
};

class Users : IObserver
{
    //Name of the User
    string name;
    bool status;
public:
    Users(string n);
    void Update(bool isavailable);         
};

Users::Users(std::string name)
{
    this->name = name;
}

void Users::Update(bool isavailable)
{
    this->status = isavailable;

    //Lets print on console just to test the working
//    std::cout <<  << "\n";
}

int main(int argc, char* argv[])
{
    Book kiterunner;
                
    Users  Anil("Anil");
    Users  Prashant("Prashant");

//When users click on Notify option

    kiterunner.Attach(&Anil);
    kiterunner.Attach(&Prashant);

    //This has updated by E-Commerce Website when the book is available

    kiterunner.Changeavailability(True);

    //Now prashant is not interested in this book so he can unsuscribe
    kiterunner.Detach(&Prashant);           

    //Now The product is again out of stock
    kiterunner.Changeavailability(false);

    getchar();
    return 0;
}

Comments

Popular posts from this blog

STL Questions

Producer Consumer problem using mutex

Interview questions