[Tutor] MVC/MVP examples of how to implement it

Alan Gauld alan.gauld at btinternet.com
Tue Dec 19 01:33:10 CET 2006


"Basil Shubin" <bashu at yandex.ru> wrote

> I have read articles about MVC/MVP, but still can't get a clue to 
> how
> implement it in really working application

Look at TurboGears. It uses a model/view/controller setup.

In TG the views are implemented as kid templates
(a mixture of HTML and embedded python-like markup)

The models are Python classes linked to SQLObject
database tables

The controllers are the functions called prior to loading
a web page, they get their input from the previous pages
POST or GET messages.

Thats a web version but it seems to work.

For a more traditional example I don;t know of any
Python GUI toolkits that explicitly support MVC,
although a model/view paradigm is easily implemented.
Controllers tend to be more tricky and require rigorous
adherence to a style convention for handling events.

Dolphin Smalltalk implements an MVC style and has
a fairly readable tutorial here, provided youi can grok
the Smalltalk syntax:

http://www.object-arts.com/docs/index.html?modelviewpresenter.htm

But since you mention MVP you have maybe already been there!

The other good explanaytion that I know is the one by Apple
for their Cocoa framework on MacOS X - also programmable in Python!
Described here:

http://developer.apple.com/documentation/Cocoa/Conceptual/CocoaFundamentals/CocoaDesignPatterns/chapter_5_section_4.html

And finally, a python example:

http://www.bigbold.com/snippets/posts/show/1050

But I actually don't like this one since it puts too mucxh code in the
controller. Controllers should be lightweight objects with most of the
real processing happening in the models. The controllers should just
collect data changes from the views and send them to the models
or vice-versa. Bad MVC turns the controller into a kind of huge 
monolithioc
procedural program that occasionally fetches data from models and
displays on the views.

Thats the best I can do. Maybe others can find better examples.

BTW there is an OO school of thought that says controllers
in MVC are basically a bad idea and a Model/View paradigm
is better. In this case the View handles the interaction with
the user and sends messages to the models.  This is how
most Windows applications are programmed in VB/Visual C++ etc.
It's certainly easier to implement, but probably less reusable
in the long run.

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 




More information about the Tutor mailing list