Interface of the Subject in the Observer Design Pattern.
This class provides methods for adding/removing observers to the subject.
There is also a 'notify' method that should be called when a notification has to be sent.
Example:
#include <iostream>
class MyEventInfo : public EventInfo
{
public:
MyEventInfo (const std::string& message) : EventInfo(0), _message(message) {}
const std::string& getMessage () { return _message; }
private:
std::string _message;
};
class MyObserver : public IObserver
{
public:
void update (EventInfo* evt, ISubject* subject)
{
MyEventInfo* info = dynamic_cast<MyEventInfo*> (evt);
if (info != 0) { std::cout << "Receiving: " << info->getMessage() << std::endl; }
}
};
int main (int argc, char* argv[])
{
ISubject* subject = new Subject ();
IObserver* observer = new MyObserver ();
subject->addObserver (observer);
subject->notify (new MyEventInfo ("Message that should be received"));
subject->removeObserver (observer);
subject->notify (new MyEventInfo ("Message that should NOT be received"));
}
- See also
- IObserver