[
Date Prev][
Date Next][
Thread Prev][
Thread Next][
Date Index][
Thread Index]
[
List Home]
[platform-ui-dev] Design Question About Listeners & Event Dispatching
|
My question is whether it should be expected that the list of listeners can be
modified while executing a listener. Should we always design for this case?
In other words, should it be possible for a listener to add or remove a
listener while handling an event?
There is a lot of existing code of the following form, or similar but using
iterators. In the case of iterators, a ConcurrentModificationException will
be thrown. In the case of indexed access, subtle bugs can exist where
listeners are skipped or handled twice.
if (activityListeners != null) {
for (int i = 0; i < activityListeners.size(); i++) {
((IActivityListener) activityListeners.get(i))
.activityChanged(activityEvent);
}
}
If we write defensively, then we need to do a copy of some sort to avoid
corrupting the collection.
int commandListenersSize = commandListeners.size();
if ((commandListeners != null)
&& (commandListenersSize > 0)) {
final ICommandListener[] listeners = (ICommandListener[])
commandListeners
.toArray(new ICommandListener[commandListenersSize]);
for (int i = 0; i < commandListenersSize; i++) {
final ICommandListener listener = listeners[i];
listener.commandChanged(commandEvent);
}
}
Should we expend the effort to hunt down the places where we don't defend
against this case? Or fix them on a case-by-case basis?
cheers,
d.