Observer methods

S

Steve Green

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.

For example:

Case A)

public interface A
{
event(Event a);
}

Case B)
public interface B
{
event1();
event2();
event3();
}
 
A

Angus Parvis

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.

For example:

Case A)

public interface A
{
event(Event a);
}

Case B)
public interface B
{
event1();
event2();
event3();
}

I'd rather use inheritance to add further Events and use just one method
than add a method for every Event that can happen.
 
C

Chris Uppal

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top