Exforsys

Online Training

Should there be a Notify in the AttachObserver method of an observer?

This is a discussion on Should there be a Notify in the AttachObserver method of an observer? within the Software Patterns forums, part of the Testing category; Hello everybody, a few days ago I read an article which covered an example implementation of the observer pattern. It ...


Go Back   Exforsys > Testing > Software Patterns

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 10-25-2003, 12:26 AM
Malte Persike
Guest
 
Posts: n/a
Should there be a Notify in the AttachObserver method of an observer?

Hello everybody,

a few days ago I read an article which covered an example
implementation of the observer pattern. It alluded to the importance
of calling the Notify() method from within any AttachObserver()
method.
In C++ this would result in something like the following:

void Subject::AttachObserver(Observer* AObs)
{
if (ObserverList.Contains(AObs))
return;

ObserverList.Add(AObs);
Notify();
}


This invocation of Notify() guarantees that a newly attached observer
can immediately adjust to the current state of the observed subject.
Now that seems pretty reasonable to me, which led me to my first of
two questions:

Question 1: Does a reason exist why this is not mentioned in the GoF
book?


The problem I see with notifying all subscribed observers every time a
new observer gets attached is that this could well add up to a
veritable notification surge when a subject already has multiple
observers. If the attachment of several observers to one subject
occured at the same time, a possible solution for avoiding the
reduntant notifications could be a BeginUpdate/EndUpdate pair
enclosing the observer creation.
But I do not really fancy the BeginUpdate/EndUpdate mechanism since
faulty implementations of subject classes as well as imponderabilities
in multithreaded environments might lock the whole updating process.

Question 2: Can the BeginUpdate/EndUpdate mechanism be considered a
good solution to prevent premature notification of observers


Thanks very much for reading and kind regards,
Malte

---

The above e-mail address is not valid. To
contact me, please use my real e-mail address:

malte AT t DASH online DOT de

Just replace the capitalized words with the
corresponding punctuation marks.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 04:01 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.1.0
Copyright 2004 - 2007 Exforsys Inc. All rights reserved.