In the following functions, it it entirely possible for the IObserver's Process()
function to try to remove itself from the notify list, using the this
pointer's DeleteObserver()
.
This causes hell with the iterators (not surprisingly!), is there a way round this? Or should I be taking a closer look at my design?
void cButtonManager::DeleteObserver(IObserver *observer)
{
list<IObserver*>::iterator iter;
for (iter = m_ObserverList.begin(); iter != m_ObserverList.end(); ++iter)
{
if (*iter == observer)
{
// Found the specified observer in the list, delete it
m_ObserverList.erase(iter);
return;
}
}
}
void cButtonManager::NotifyObservers(void)
{
list<IObserver*>::iterator iter;
for (iter = m_ObserverList.begin(); iter != m_ObserverList.end(); ++iter)
{
(*iter)->Process(this);
}
}
For example, imagine that the list is a collection of people that subscribe to a magazine and the Process()
function is the delivery of a new magazine issue; if the magazines latest issue is awful the subscriber may wish to un-subscribe as a direct result of that issue.