[Tutor] Correct use of model-view-controller design pattern

Alan Gauld alan.gauld at yahoo.co.uk
Mon May 30 12:30:07 EDT 2016


On 29/05/16 05:33, boB Stepp wrote:

As promised I'm back so will try to answer these...

> Some other questions:
> 
> 1)  If I were to add code to filter user inputs, which file is the
> logical place to place such code?  My current impression is that it
> should go in the controller, but I could see it being in the view.
> However, if I were to write a Validation class, I could see that as
> having uses beyond checking just user input.

Filtering user input is one type of validation (others include
type and value checking) Usually the view (especially in a GUI)
will control the type checking and in  some cases the value
checking too (think calendar pickers). But the controller may
also do some data adjustment (such as reformatting a date
to suit a particular model). But mostly I expect the view
to collect the correct data in terms of primitive type/value.

An example where more complex validation might be required is
where you want to validate a credit card or an account number.
In that case the controller may call a business service before
passing the validated details onto the appropriate model(s)
to process.

Models should usually expect to be given good data. In some
cases you might want to do a belt n' braces data check in
the model too, but mostly you assume your front end is
catching the bad stuff (see file processing below).

> 2)  Currently I am using a while loop in the controller to keep the
> view 'alive'.  If I were to convert the view to a tkinter GUI, where
> would the root.mainloop() statement go?  In the controller's main()
> method?  What GUI-related stumbling blocks might I easily run into in
> going from CLI to GUI with this existing code?

See previous post. The main loop is almost always in the view.

> 3)  If I were to add data files in place of (or in addition to) user
> input, in which aspect of MVC would file I/O be handled?

This is where things get interesting. When processing files the
view/controller are only likely to be validating the filename and
initiating the process. This means that there needs to be a model object
somewhere that processes the file. But the content of the
file is unsafe  so that model needs to do all the data
validation that would normally be done in the view/controller.

Its not unusual to have a dedicated model for such batch processing
and it will then call the other model methods for each record processed,
effectively becoming a controller of sorts. There may
be opportunities for code reuse between the UI controller and
the batch controller.

> BTW, I did not attempt to write any classes as things seemed simple
> enough that functions seemed more appropriate and straightforward to
> me.  If that is a bad choice, please let me know!

At this level of simplicity its hard to see the advantages of the MVC
paradigm. (A bit like OOP itself!) Its only when things start to get
complicated that MVC starts to simplify things rather than add noise.


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list