gatb.core-API-0.0.0
ISubject Class Referenceabstract

Interface of the Subject in the Observer Design Pattern. More...

#include <IObserver.hpp>

Inheritance diagram for ISubject:
Inheritance graph

Public Member Functions

virtual ~ISubject ()
 
virtual InterfaceId getInterface ()=0
 
virtual void addObserver (IObserver *observer)=0
 
virtual void removeObserver (IObserver *observer)=0
 
virtual void notify (EventInfo *event)=0
 

Detailed Description

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:

// We include what we need for the test
#include <iostream>
// We define some notification information class.
class MyEventInfo : public EventInfo
{
public:
MyEventInfo (const std::string& message) : EventInfo(0), _message(message) {}
const std::string& getMessage () { return _message; }
private:
std::string _message;
};
// We define some Observer class.
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; }
}
};
/********************************************************************************/
/* Usage of the Observer/Subject class */
/********************************************************************************/
int main (int argc, char* argv[])
{
// we define a subject instance
ISubject* subject = new Subject ();
// we create a specific observer
IObserver* observer = new MyObserver ();
// we attach the observer to the subject
subject->addObserver (observer);
// the subject sends some notification => should be received by our observer
subject->notify (new MyEventInfo ("Message that should be received"));
// we detach the observer from the subject
subject->removeObserver (observer);
// the subject sends some notification => should not be received by our observer
subject->notify (new MyEventInfo ("Message that should NOT be received"));
}
See also
IObserver

Constructor & Destructor Documentation

virtual ~ISubject ( )
inlinevirtual

Destructor.

Member Function Documentation

virtual void addObserver ( IObserver observer)
pure virtual

Add an observer to the subject.

Parameters
[in]observer: the observer to be added.

Implemented in Subject.

virtual InterfaceId getInterface ( )
pure virtual

Returns the identifier the subject knows about.

Returns
identifier

Implemented in Subject.

virtual void notify ( EventInfo event)
pure virtual

Notify some piece of information to potential observer instances. Actual implementations are likely to loop over all attached observer instances and call the 'update' method on each of them.

Parameters
[in]event: information related to the notification

Implemented in Subject.

virtual void removeObserver ( IObserver observer)
pure virtual

Remove an observer from the subject.

Parameters
[in]observer: the observer to be removed.

Implemented in Subject.


The documentation for this class was generated from the following file: