D
Daniel Kay
Hello!
I have written two template classes which implement the observerpattern
in C++. I hope I manage to desribe the problem I have.
template<class T> class Observer {
/* ... */
};
template<class T> classSubject {
/* ... */
};
These template classes are being used as base classes for non abstract
Observer and Subject classes. The update() method in the observer class
is pure virtual and needs to be implemented by the class which derives
the Observer template class. This is good, because the user can't forget
to implement it.
In my concept the subject has a list of attached observers, and the
Observer has a list of subject it was attached to. When a observer is
destructed for some reason, the destructor detaches itself from all
Subjects it was attached to.
Now my problem: At that point, when I am in the destructor of the
Observer template, the update method isn't available anymore. (Because
the derived class is already destructed) So in case the Subject now
tries to notify the observer the application crashes. Of course this
only happens with a multithreaded application. In normal case this
condition will happen very very seldom. But it can and will happen some
day. I have found two ways to resolve this issue. I don't like one of them.
1. Make the method update() in the observer base class virtual only
(defining an empty body). The user isn't forced anymore to implement
update in the derived class of the Observer pattern and might forget it.
2. By forcing the user to call some method from the destructor of the
derived class which was defined in the Observer base class which
detaches itself from all Subjects. The user may forget to insert this
call in his destructor and we have the problem again.
Anyone knows a third way to resolve my problem? I hope I managed to
describe my problem... If not, then just ignore it and i am lost... *g*
Have a nice day,
Daniel
I have written two template classes which implement the observerpattern
in C++. I hope I manage to desribe the problem I have.
template<class T> class Observer {
/* ... */
};
template<class T> classSubject {
/* ... */
};
These template classes are being used as base classes for non abstract
Observer and Subject classes. The update() method in the observer class
is pure virtual and needs to be implemented by the class which derives
the Observer template class. This is good, because the user can't forget
to implement it.
In my concept the subject has a list of attached observers, and the
Observer has a list of subject it was attached to. When a observer is
destructed for some reason, the destructor detaches itself from all
Subjects it was attached to.
Now my problem: At that point, when I am in the destructor of the
Observer template, the update method isn't available anymore. (Because
the derived class is already destructed) So in case the Subject now
tries to notify the observer the application crashes. Of course this
only happens with a multithreaded application. In normal case this
condition will happen very very seldom. But it can and will happen some
day. I have found two ways to resolve this issue. I don't like one of them.
1. Make the method update() in the observer base class virtual only
(defining an empty body). The user isn't forced anymore to implement
update in the derived class of the Observer pattern and might forget it.
2. By forcing the user to call some method from the destructor of the
derived class which was defined in the Observer base class which
detaches itself from all Subjects. The user may forget to insert this
call in his destructor and we have the problem again.
Anyone knows a third way to resolve my problem? I hope I managed to
describe my problem... If not, then just ignore it and i am lost... *g*
Have a nice day,
Daniel