observer.pattern

Home > SSE 658 > Project 1 > Examples: Observer pattern

Observer Pattern Example
This is a simple observer pattern implementation. In this implementation, observer(s) can subscribe to multiple subjects.

code format="C++" // Module:       observer.h // Programmer:    Dmitriy Slipak // Date:       January 31, 2010 // Purpose: //               Header file, //               contains observer pattern classes declaration //
 * Module:** observer.h
 * 1) ifndef _OBSERVER_H_
 * 2) define _OBSERVER_H_


 * 1) include

class Subject;

class Observer { public: virtual void update(Subject* s) = 0; };

class Subject { private: std::list observers; std::string message;

public: void set_message(std::string m); std::string get_message;

virtual void subscribe(Observer* o); virtual void unsubscribe(Observer* o); virtual void notify; };

code
 * 1) endif

code format="C++" // Module:       transport.h // Programmer:    Dmitriy Slipak // Date:       January 31, 2010 // Purpose: //               Header file, //               contains transportation related classes declaration // //
 * Module:** transport.h
 * 1) ifndef _TRANSPORT_H_
 * 2) define _TRANSPORT_H_


 * 1) include
 * 2) include "observer.h"

class Vehicle : public Observer { protected: std::list subjects;

public: void subscribe(Subject* s); void update(Subject* s); virtual void confirm(Subject* s) = 0; };

class Airplane : public Vehicle { public: void confirm(Subject* s); };

class Boat : public Vehicle { public: void confirm(Subject* s); };

class Car : public Vehicle { public: void confirm(Subject* s); };

class Dispatcher : public Subject { public: void inform; };

class Manager : public Subject { public: void inform; };

code
 * 1) endif

code format="C++" // Module:       observer.cpp // Programmer:   Dmitriy Slipak // Date:       January 31, 2010 // Purpose: //               contains observer classes implementation //
 * Module:** observer.cpp
 * 1) include "observer.h"

void Subject::set_message(std::string m) { message = m; } std::string Subject::get_message { return message; }

void Subject::subscribe(Observer* o) { observers.push_back(o); } void Subject::unsubscribe(Observer* o) { observers.remove(o); }

void Subject::notify {   std::list::iterator iter;

for(iter=observers.begin; iter != observers.end; ++iter) ((Observer*)*iter)->update(this); } code

code format="C++" // Module:       transport.cpp // Programmer:   Dmitriy Slipak // Date:       January 31, 2010 // Purpose: //               contains transportation hierarchy implementation //
 * Module:** transport.cpp
 * 1) include "transport.h"

void Vehicle::subscribe(Subject* s) { subjects.push_back(s); s->subscribe(this); }

void Vehicle::update(Subject* s) { std::list::iterator iter;

for(iter=subjects.begin; iter != subjects.end; ++iter) { if (s == (Subject*)* iter) confirm(s); } }

void Airplane::confirm(Subject* s) { std::cout << "Pilot: confirmed -> " << s->get_message.c_str << '\n'; }

void Boat::confirm(Subject* s) { std::cout << "Captain: confirmed -> " << s->get_message.c_str << '\n'; }

void Car::confirm(Subject* s) { std::cout << "Driver: confirmed -> " << s->get_message.c_str << '\n'; }

void Dispatcher::inform { notify; }

void Manager::inform { notify; } code

code format="C++" // Module:       company_driver.cpp // Programmer:   Dmitriy Slipak // Date:       January 31, 2010 // Purpose: //               program entry point //
 * Module:** company_driver.cpp
 * 1) include "transport.h"

int main {   //    test observer pattern Dispatcher* d = new Dispatcher; Manager* m = new Manager; Airplane* a = new Airplane; Boat* b = new Boat; Car* c = new Car;

a->subscribe(d); a->subscribe(m);

b->subscribe(d); c->subscribe(d);

d->set_message("message from dispatcher"); d->inform;

m->set_message("message from manager"); m->inform;

delete d;   delete m;    delete a;    delete b;    delete c;

std::cin.get; return 0; } code

In this example, Airplane(observer) will subscribe to both Dispatcher and Manager subjects. Here is the result of program run