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 ...
|
|||||||
| Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
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. |
![]() |
| Thread Tools | |
|
|