[IGSTK-Developers] Observer/Observable pattern

Ziv Yaniv zivy at isis.imac.georgetown.edu
Fri Feb 23 21:08:35 EST 2007


Luis,
If what you say is correct then the ITK documentation is wrong.
This is from the itkObject.h file:

/** Allow people to add/remove/invoke observers (callbacks) to any ITK
   * object. This is an implementation of the subject/observer design
   * pattern. An observer is added by specifying an event to respond to
   * and an itk::Command to execute. It returns an unsigned long tag
   * which can be used later to remove the event or retrieve the
   * command.  The memory for the Command becomes the responsibility of
   * this object, so don't pass the same instance of a command to two
   * different objects  */

That is where I got my many-to-one understanding.

                  Ziv
---------- Original Message ----------------------------------
From: Luis Ibanez <luis.ibanez at kitware.com>
Date:  Fri, 23 Feb 2007 18:34:56 -0500

>Hi Ziv,
>
>The Observer pattern in ITK was mostly copied from
>the functionality existing in VTK at the time.
>
>The relationship between Observers and Observables
>is a many-to-many relationship. That is, and ITK
>object may have many Observers connected, and a
>given Observer may be observing many different
>objects.
>
>When an event is invoked, the Execute method of
>the observer is called. This method include as
>first argument a pointer to the "caller", which
>is a pointer to an itkObject.
>
>Observable objects *ARE NOT* responsible for the
>observer memory, not the other way around.
>
>What you point out, is rather a weakness in the
>implementation since the observables are assuming
>that the observers are still alive by the time the
>observable invokes an event. This may actually be
>considered a bug...
>
>
>So,.. there are two independent issues:
>
>1) Can Observers observe multiple object ?
>
>    The answer is YES.
>
>
>
>2) Why do observables use a list of raw
>    pointers instead of SmartPointers ?
>
>    The answer is : This can be considered a bug.
>                    (or at least a weakness...)
>
>
>The fact that (2) uses raw pointers shouldn't be
>considered as an indication of (1).
>
>
>
>   Regards,
>
>
>
>      Luis
>
>
>
>===================
>Ziv Yaniv wrote:
>> Hi All,
>> 
>> The following question is with regard to ITK, but it spills over to
>> IGSTK, so I'm sending it to this development list as I'm not on the ITK
>> developers one.
>> 
>> I would like to understand the reasoning behind the design of the
>> ITK observer/observable pattern implementation. According to the
>> documentation of the AddObserver() method in itk::Object I cannot
>> have the same Observer attached to several objects, as observable
>> objects are responsible for the observer's memory. This is
>> reflected in the use of a raw pointer to a itk::Command object in
>> the itk::Object::AddObserver() method.
>> 
>> In the spirit of ITK I expected the AddObserver method to receive
>> a SmartPointer which also allows a single observer
>> attached to multiple objects, as the memory would be released
>> when the last observable deleted its observers.
>> 
>> Am I missing something? Should this go into a bug/feature request on
>> the ITK side?
>> 
>>              regards
>>                  Ziv
>> _______________________________________________
>> IGSTK-Developers mailing list
>> IGSTK-Developers at public.kitware.com
>> http://public.kitware.com/cgi-bin/mailman/listinfo/igstk-developers
>> 
>



More information about the IGSTK-Developers mailing list