MVC First Attempt - Advice
I'm trying to use MVC for the first time, and I'm trying to follow the example given in
http://java.sun.com/developer/onlineTraining/GUI/Swing2/shortcourse.html
The main part of my model (the equivalent of IntVectorModel) is capable of creating message generators. It keeps a list of which message generators it has created.Each message generator is an instance of a separate class, which runs in its own thread and generates messages at a frequency defined for that type of message. Each message generator keeps its own count of how many messages it has sent.
I want my view to show the list of active message generators, and for each one, the current message count.
My adaptor (the equivalent of IntVectorToListViewAdaptor) listens for changes to the list of message generators and updates the view accordingly. Easy. But I also want the view to be updated whenever any of the message counts changes.
I'd like advice on the best way to do this. Should I have my message generators fire change events after sending messages? And if so, should I have the same adaptor listen for those events also? I presume that in order to achieve this, whenever I instantiate a new generator, I'd need to pass it the list of change listeners.
Or should I hold the counts in the "main" part of the model and have the message generators update the counts there (don't know how I'd do that though) and have the main part generate the change event.
I guess the question boils down to: when a view needs data from different parts of the model, or indeed from more than one model, should there be one adaptor that listens for changes to all the different parts, or should there be an adaptor for each part that can change? The latter seems rather messy.
Or have I got it completely wrong?
Thanks for any advice.
[1858 byte] By [
abthma] at [2007-10-2 21:47:08]

> I'm trying to use MVC for the first time, and I'm
> trying to follow the example given in
>
> http://java.sun.com/developer/onlineTraining/GUI/Swing
> 2/shortcourse.html
>
> The main part of my model (the equivalent of
> IntVectorModel) is capable of creating message
> generators. It keeps a list of which message
> generators it has created.Each message generator
> is an instance of a separate class, which runs in its
> own thread and generates messages at a frequency
> defined for that type of message. Each message
> generator keeps its own count of how many messages it
> has sent.
>
> I want my view to show the list of active message
> generators, and for each one, the current message
> count.
>
> My adaptor (the equivalent of
> IntVectorToListViewAdaptor) listens for changes to
> the list of message generators and updates the view
> accordingly. Easy. But I also want the view to be
> updated whenever any of the message counts changes.
>
> I'd like advice on the best way to do this. Should I
> have my message generators fire change events after
> sending messages? And if so, should I have the same
> adaptor listen for those events also? I presume that
> in order to achieve this, whenever I instantiate a
> new generator, I'd need to pass it the list of change
> listeners.
>
You seem to like the word adaptor. :^)
I think the observer-observable pattern is just what you are looking for.
> Or should I hold the counts in the "main" part of the
> model and have the message generators update the
> counts there (don't know how I'd do that though) and
> have the main part generate the change event.
>
It seems to me that for better encapsulation, each generator should keep track of its own counts.
> I guess the question boils down to: when a view needs
> data from different parts of the model, or indeed
> from more than one model, should there be one adaptor
> that listens for changes to all the different parts,
> or should there be an adaptor for each part that can
> change? The latter seems rather messy.
>
This is the controller's responsibility. Think of the 'generic' MVC controller in the Spring Framework:
ModelAndView process(final Request request, final Response response) {}
interface ModelAndView {
public Map<Object, Object> getModelData();
public View getView();
}
The above is not exact, but close enough. It clearly illustrates the roles of the controller. If you need multiple sources of model data, then the controller should fetch them and dispatch them to the view.
> Or have I got it completely wrong?
>
> Thanks for any advice.
- Saish