Version 14 of Model / View / Controller

Updated 2008-12-27 13:49:53 by EKB

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.

EKB Not in terms of the code, but in terms of presentation and use, yes. On one hand I find that if I think about a package as a mini-language then I implement the package and the application somewhat differently. On the other hand, I find that exposing a package as a mini-language (or "domain language") and then presenting it to users that way, without ever mentioning Tcl, leaves them very impressed. It's still good advertising for Tcl/Tk, because later when they ask about it, I say, "Oh, yes, that's quite straightforward in Tcl." This ties into two things that I see in software development discussions: a) the use of domain languages as a strategy (highly recommended in the popular "Pragmatic Programmers" books); b) Lisp as the natural platform for making mini-languages (although not by the Pragmatic Programmers -- they like Ruby). But Tcl is also a natural platform for it. As I was thinking about the mini-language code I've written, I realized that it might be possible to add a (c) to this list, that a mini-language is a natural way to implement MVC that is (IMHO), more transparent and easier to implement than implementing MVC in, say, Java.

So, the crass view of saying "yeah, it's just a package, but..." is that it's a way to do some Tcl evangelism using some popular concepts: domain language and MVC. Something like, "TCL can implement MVC via a domain language at a lower TCO than alternatives and with lower barriers to entry." That sort of thing. The non-crass view of it is that thinking about an existing strategy in a new way affects my overall development process.


Category Concept | Category GUI