Observer Pattern

Intent of Observer Design Pattern is to Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.

Observer pattern comes under Behavioral Pattern.

SAMPLE CODE:

#include <iostream>
#include <vector>
#include <unistd.h>
#include <time.h>

using namespace std;

class Subject;

class Observer {
public:
    string name;
    virtual void update() = 0;
};

class Subject {
private:
        vector <Observer*> observers;

public:
    void attach(Observer* obser)
    {
        observers.push_back(obser);
        cout << obser->name << " Suscribed To Get Notification" << endl << endl;
    }

    void detach(Observer* obser)
    {
        int observerCount = observers.size();
        for (int i = 0; i < observerCount; i++) {
            if (observers[i] == obser) {
                observers.erase(observers.begin() + i);
                cout << obser->name << " Unsuscribed From Receiving Notification" << endl;
                break;
            }
        }
    }

    void notify()
    {
        time_t now = time(NULL);
        struct tm * curtime = localtime(&now);
        cout << asctime(curtime);

        int observerCount = observers.size();
        for (int i = 0; i < observerCount; i++) {
            observers[i]->update();
        }
    }

};

class OfferPage : public Subject {
private:
    string  productName;
    double  actualPrice;
    double  offerPrice;

public:
    void setOffer(string product, int actual, int offer)
    {
        productName = product;
        actualPrice = actual;
        offerPrice = offer;
    }

    string getProduct()
    {
        return productName;
    }

    int getActualPrice()
    {
        return actualPrice;
    }

    int getOfferPrice()
    {
        return offerPrice;
    }
};

class GmailUsers : public Observer {
private:
    OfferPage* offerOfDay;

public:
    GmailUsers(OfferPage* offer)
    {
        name = "Gmailusers";
        offerOfDay = offer;
    }

    void update()
    {
        string product = offerOfDay->getProduct();
        double actPrice = offerOfDay->getActualPrice();
        double offPrice = offerOfDay->getOfferPrice();
        cout << "----> Sent TO Gmail Users" << endl;
        cout << "  Offer Of The Day - " << product << endl;
        cout << "    Actual Price   - " << actPrice << " USD" << endl;
        cout << "    Offer Price    - " << offPrice << " USD" << endl;
        cout << "    Savings        - " << (actPrice - offPrice) << " USD" << endl ;
    }
};

class YahooUsers : public Observer {
private:
    OfferPage* offerOfDay;
    
public:
    YahooUsers(OfferPage* offer)
    {
        name = "YahooUsers";
        offerOfDay = offer;
    }

    void update()
    {
        string product = offerOfDay->getProduct();
        double actPrice = offerOfDay->getActualPrice();
        double offPrice = offerOfDay->getOfferPrice();
        cout << "----> Sent TO Yahoo Users" << endl;
        cout << "  Offer Of The Day - " << product << endl;
        cout << "    Actual Price   - " << actPrice << " USD" << endl;
        cout << "    Offer Price    - " << offPrice << " USD" << endl;
        cout << "    Savings        - " << (actPrice - offPrice) << " USD" << endl << endl;
    }
};

int main()
{
    OfferPage todayOffer;
    todayOffer.setOffer("IPHONE 5S", 740, 725);

    Observer* gmailSuscriber = new GmailUsers(&todayOffer);
    todayOffer.attach(gmailSuscriber);
    Observer* yahooSuscriber = new YahooUsers(&todayOffer);
    todayOffer.attach(yahooSuscriber);

    todayOffer.notify();

    sleep(5);
    todayOffer.setOffer("Dell Laptop", 850, 839);
    todayOffer.notify();

    return 0;
}


OUTPUT:

Gmailusers Suscribed To Get Notification

YahooUsers Suscribed To Get Notification

Sun Oct  4 14:00:13 2015
----> Sent TO Gmail Users
  Offer Of The Day - IPHONE 5S
    Actual Price   - 740 USD
    Offer Price    - 725 USD
    Savings        - 15 USD
----> Sent TO Yahoo Users
  Offer Of The Day - IPHONE 5S
    Actual Price   - 740 USD
    Offer Price    - 725 USD
    Savings        - 15 USD

Sun Oct  4 14:00:18 2015
----> Sent TO Gmail Users
  Offer Of The Day - Dell Laptop
    Actual Price   - 850 USD
    Offer Price    - 839 USD
    Savings        - 11 USD
----> Sent TO Yahoo Users
  Offer Of The Day - Dell Laptop
    Actual Price   - 850 USD
    Offer Price    - 839 USD
    Savings        - 11 USD


                                               INDEX - OTHER DESIGN PATTERNS

No comments:

Post a Comment