Version 13 of Model / View / Controller

Updated 2008-12-27 11:51:51 by lars_h

Model-View-Controller [L1 ] (often abbreviated "MVC") is a way of working with GUIs that provide access to complex data, and it seems to scale really quite well indeed. You do it by separating the code to implement the Model (your underlying data is, say, an updatable list or a read-only tree) from the View (how your data looks when drawn on the screen) and the Controller (how your model responds to updates to the view) though often the view and controller are joined together.

Some features of Tk operate in this way; notably the -variable option to entry, the -textvariable option to label, button et al, and the -listvariable option to listbox.

It's wise to design the rest of your interface that way as well. The trace command (particularly [trace variable]) is very useful for implementing triggers that update a view when the underlying model changes. The bindtags command is useful for associating a widget with one or more controllers other than the standard ones that Tk provides; occasionally, you also need to resort to overloading a widget command. For a worked example, see the page entitled, "Text variable for text widget". In that example, the widget command is overloaded to notify the model when the user attempts to change the text contents. A trace notifies the view when the model is updated. A bind tag is also used so that everything gets cleaned up correctly when the window is destroyed.


See a tiny example at a little multiplication toy, a slightly bigger one at Revello


MVC is starting to accumulate heterodoxy, including "MVC considered harmful" [L2 ], ...

EKB That's an interesting article. It's about an alternative approach called "context-oriented programming". I made a short example on ContexTcl: Playing with Context-oriented Programming.


EKB One way to use the MVC pattern that I apply frequently is to make a core program with programmatic hooks and no user interface elements at all. After making it, I build a command-line program that sources a script that accesses the model. Basically, this means using it as a mini-language, which I think is a particularly Tclish (and Lispish) thing to do. Often the development stops there, because it's all I need, but it doesn't have to. Because it uses the MVC pattern, it would be easy to make a Tk interface that makes use of the core program. There's an example at Mini-language as Model-View-Controller.

Lars H: Hmm... As used above, "mini-language" seems almost a synonym of "package" (although perhaps not registered via the package mechanism). Is there some other distinction between the two that you feel is noteworthy but perhaps not immediately obvious, or is it just a matter of terminology? One distinction could be that a package is often set up to support multiple clients (if it keeps state, then the client might need to provide a handle to the particular context to use) whereas a mini-language has a global context shared by all (at least in that interpreter), but that could just as well be irrelevant.


Category Concept | Category GUI