Steve said:
This is a general style question. If you implement an Observer as part of
the model in a MVC pattern, do you use a large number of methods or one
simple method that receives various messages.
Depends on how closely related they are. If the chances are reasonably good
that the Observer won't want to take different action according to the
different events, then I'd use one parameterised event method. If the meanings
of the events are different enough that the Observer is likely to have to write
conditional code to decode a single event and dispatch "it" to different
handler routines, then using a single event would be a nuisance for both me and
the Observer.
E.g. I would not use a common event method for both "Window Closed" and "Mouse
Moved". I /would/ use a single event method for "Key Pressed" (rather than
onAPressed(), onBPressed(), onCPressed()....). I /might/ us a common method
for "Left Mouse Button Clicked" and "Right Mouse Button Clicked", or then again
I might not.
One option is to generate the most detailed events that are reasonable, but
also to provide analogues of the AWT/Swing XxxAdaptor classes which implement
the detailed event handlers by invoking less detailed handlers of their own
(which are empty by default). Thus Observers can subclass your XxxxAdaptor and
either override the detailed handlers, or more general handler as they prefer.
E.g. given an XxxxListener interface for Observers:
interface XxxxListener
{
void onAaaa();
void onBbbb();
}
where it isn't clear whether "aaaa" and "bbbb" are really all that different,
or whether they are just variant on "cccc", you could provide:
class XxxxAdaptor
implements XxxxListener
{
void onAaaa() {this.onCccc(); }
void onBbbb() {this.onCccc(); }
void onCccc() {}
}
-- chris