Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There is a lot of different implementations possible (whatever the language), but the abstract idea is :</p> <ol> <li>have an event type identifier to know wich events have been fired by the observed object (it can be whatever works);</li> <li>have a list of observers (or several, one by event type?) registered in the registered objects - that suppose that the observers are functors (function pointer or object that act like a function) or are objects with a known interface (with a virtual method to call ion event fired);</li> <li>when an event is fired (by calling a function on the observed object) we just go through the list of observers registered for the event's type and pass the event information (id plus maybe data ) to the observer to be processed;</li> </ol> <p>Here is a simplist implementation :</p> <pre><code>enum EventType { Event_A, Event_B, }; class Observer // for this example i'll suppose observer inherits this class { virtual void catchEvent( EventType e ) = 0; // for sake of the example I just need the event type, not the data, but it's often required virtual ~Observer(){}; // virtual base classes require this to work fine. }; class Observed { typedef std::vector&lt;Observer*&gt; ObserverList; typedef std::map&lt; EventType, ObserverList &gt; ObserversTable; ObserversTable m_observers; // we have observers for each event type public: void addObserver( Observer&amp; observer, EventType eType ) { m_observers[ eType ].push_back( observer ); } // this is simplist, to say the least... void sendEvent( EventType event ) // here we send the event, it will be catched by observers { // note : often this type of system is "buffered", but here we will process it immediatly as it's a simple example ObserverList&amp; observers = m_observers[ event ]; for ( ObserverList::iterator it = observers.begin(); it != observers.end(); ++it ) { Observer* observer = *it; observer-&gt;catchEvent( event ); // here an observer receive the event } } }; </code></pre> <p>It's really a basic example but that might help you understand the idea.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload