Hubert Mayr wrote:
I played with the demo application provided by wxPython
and came across the MVC example. I see the advantage when
having such a simple logic but I'm asking myself how such
a pattern could be adapted to an, let's say, editor application.
I understand that the View part is the frame with its
menu, toolbar and statusbar.
But is a styled text control (stc) the controller in such a scenario?
It depends. If you are allowing the STC to hold the master version of
your document, then it is essentially acting as model, view, AND controller.
The model holds the data. For an editor, the data is the characters in
the document. If you have your own string buffer containing the
document, then that would be the model, but if the STC holds the only
copy, then it is acting as the mode. It's obviously acting as view,
since it is presenting the UI. And because it is updating the UI based
on changes in the document text, it is also acting as a controller.
MVC is a very useful paradigm, but remember that it's only a guideline.
There are many applications where the lines between them are extremely
fuzzy, especially for small applications.
Whenever one cuts text within the stc, the toolbar and menu
needs to reflect this action by enabling some buttons and disabling
others.
In that case, you have a controller, and STC has a controller. STC's
controller is notifying yours so that you may signal changes in your view.
Or is it needed to have another instance between the stc and model
to fulfill the MVC design pattern?
Only if it make encapsulation clearer, or enhances your ability to
change to a different view. If it makes things muddier, then don't
worry about religious purity to the model. It's supposed to make things
easier.
If one decides to replace the control with another one,
you still have to code the communication to the controller, don't you?
So, you could also code the communication to the model, couldn't you?
Sure. Often times, the model doesn't actually have any code at all --
it's just a collection of data structures. The controller interprets
the data structures to tell the view how to update itself.
Are there any good examples available which could be used to study MVC
and other patterns?
There are lots of good examples on the web. Microsoft also espouses an
MVVM pattern (model - view - viewmodel), which is somewhat similar.
Do remember, however, that these patterns are intended only as
guidelines. There is good justification for separating the presentation
of a piece of data from the representation of that piece of data, and
that's primarily what MVC is about. If you're doing a very large
application, adherence to the pattern makes it much easier to implement
remote control and COM control, for example (since that can hook
straight into the controller without going through the view), but in
many small applications, it's just not worth the overhead.
···
--
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.