Lukas Degener wrote:
MVC sure is a great thing, but I would like to see a concrete
toolkit or a hint list which helps me in making perfect MVC
code immediately. Is it even possible to write a MVC toolkit?
(...)
What would be in such a toolkit?
Depends on what the toolkit is ment to do. The most consequent use of
mvc patterns that i saw so far is found within the JFC/Swing classes.
(Although there "View" and "Controller" are often(or always?) the
same
object iirc. )
It's been a long time since i last did something in java, so i can't
come up with concrete class names, but i think that for instance there
was this Table/TableModel etc. Actualy almost any gui component within
swing is designed as a specific "view" on a certain data "model",
even
for simple things: You could consider the JButton as "View" on a JAction
"model".
So if you want concrete examples of this and other design patterns,
this might be interesting to have a look on. Its nicely API-documented
at
java.sun.com and i think there was also a very good tutorial on Swing
in particular.
Many of these patterns could easily be ported to c++, others might be a
bit more tricky to port, since they depend on garbage collection, which
is a luxury c++ can not effort/offer.
Hope this is usefull to you.
Lukas
Hi,
There is a good technical article explaining its architecture at:
http://java.sun.com/products/jfc/tsc/articles/architecture/index.html
Swing is based completely on the JavaBean event model:
http://java.sun.com/products/javabeans/
Basically, a JavaBean event is made of 3 classes:
- an event class, which holds the state variables of the event
- a listener interface (an abstract class if we speak C++) which
defines methods that will be called upon notification of the event
- a source object, which registers and unregisters listeners and fires
calls to the listener's method when the event is trigged
The translation of MVC in term of JavaBeans is that a Model is an event
source and the View and/or the Controler are listeners (more precisely,
they use 'adapters' to translate the calls to the listener's method into
calls of their own methods).
For instance, if you want to program in MVC a slider that follows an int
value, you will need 3 classes:
- an event class:
class IntEvent {
...
}
- a listener interface:
interface IntListener {
void valueChanged( IntEvent ev);
}
- a source class (which is in fact in Swing an interface):
interface IntModel {
void addIntListener( IntListener listener) {...}
void removeIntListener( IntListener listener) {...}
int getValue() {...}
}
and the graphical widget will be:
class Slider {
Slider( IntModel model) {...}
}
And the gain is that your widget knows nothing about the way you store
your data, if the value change is trigged by a message received other a
socket or a inter-thread fifo etc...
Translating this model to C++ is possible, using abstract classes for
interfaces and multiple inheritance for interface implementation.
However, something I'd be very happy to hear about is a translation in
Python of the MVC architecture.
I think also that one of the computer science bibles can be worth
reading: 'Desing Patterns' by E. Gamma, R. Helm & J. Vlissides.
BTW, not to promote my own kitchen, but jMax code uses a lot MVC :-)
François