The Model, View, Controller architecture (MVC) - what, why and how.
You may have heard the "Model, View, Controller" mantra being bandied about, but what does it mean, why are some people so keen on it, and does it work? Where is it used?
What does MVC (Model, View, Controller) really mean?
It means that you divide your program / application into three areas.
The "model" is the section which defines the data that you're holding - it defines the database tables, what the columns in your tables are, what's valid and what's not valid, and how the tables may be linked ("joined") to each other.
The "view" is how the data is presented to the user - what the screens look like and which information goes where. For all but the more simple models there will be multiple views.
The "controller" is the code that sits between the model and the view; methods in the controller collect the data from the model and perform any actions necessary on it to display it to the user. And the take new or amended data entered onto a view (usually via a form), and save it validated into the model.
Why are people keen on MVC?
The detachment of the model from the controller means that the model and the business logic that's closely associated with it can be separately tested and maintained, and can be used by various different controllers. So the same model can be used for a controller that looks after web interaction and data entry, and another controller that look after monthly statistics from the database in a batch mode.
Similarly, the views are logically separated from the controller, allow one team of people to look after the control logic and another team to look after the look and feel of the application - how it prsents to the user. To a very great extent each can be tested independently, and the look and feel (the view) can be changed without effecting the underlying business logic.
By providing this separation of tasks, each section can be provided by experts at that section - good programmers aren't often good graphic artists (and vice versa), and database design is a strange discipline which has its own specialised practitioners.
Maintainance of the system as a whole is made much easier as issues that need work are quickly tied down to one area, and the structure encourages template and code reuse. So - a good and efficient scheme of things.
Does it work?
Yes, it does ... and all but the very small web application. To divide an application that does no more that echo the current date onto a page into three - an empty model, a controller that reads the clock, and a viewer that bursts the hours and minutes through onto a web page would be overkill, but very quickly it become worthwhile.
Our online course booking system was built into an MVC based framework in PHP, based on Rasmus Lerdorf's 4 layer model (in effect M, V, C with the encompassing framework being the fourth layer), and I wrote it in a couple of days - without the framework to start from, it would have been a week's work. And it's easy to maintain too - I added a new course - Ruby on Rails
to my model today, and it went in quickly and easily without any major reworking. The controller picks up the new data from the model, and it's viewed - literally a five minute job and you can book for 10th April ;-)
Where is MVC used?
I've given you the clues that we use it, in PHP, for much of our web site work. We were early practitioners, before even the formallity of MVC design came to the front, so it's a bit fuzzy in places - but it's there. And it's also very much there in templating toolkits like Smarty in PHP, and it's used in iOS - the Apple operating system that's used for iPhone and iPad programming.
In Python, the Django Framework implements the MVC model, and Ruby on Rails is an MVC based framework through and through.
You'll find MVC used as the structure within major open source applications too. The Moveable Type software that we use for the blog, the Simple Machines Forum that's inside our
"Ask the Tutor" forum and many (most?) others use MVC in name, or if not in name, but in deed.
(written 2010-02-01, updated 2012-06-23)
| This is one of a series of summaries taking you from initial installation of Ruby and Rails through to a complete multitable application:|
[link] - Installing Ruby and Rails
[link] - Hello World, Ruby on Rails Style
[link] - What and Why - Model, View, Controller
[link] - Multiple views, same model
[link] - A form to allow data to be added to the model
[link] - Validating data entries for storage in the model
[link] - Cleanly viewing model data
[link] - Complete sample, including a multiple table model
These topics are covered on our Introduction to Rails day - an optional extension of Ruby Programming or Learning to program in Ruby
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articlesQ915 - Object Orientation and General technical topics - Principles of Model - View - Controller 
Real life PHP application using our course training MVC example - (2016-06-05) 
Using an MVC structure - even without a formal framework - (2016-02-07) 
Hello Flask world / Python web micro framework - (2015-10-11) 
Refactoring Perl applications to give them a rosy future - (2015-01-11) 
An example of Model-View-Controller techniques in a Perl / CGI script - (2014-11-20) 
Teaching CodeIgniter - MVC and PHP - (2013-06-12) 
MVC and Frameworks - a lesson from first principles in PHP - (2013-04-19) 
Really Simple Rails - (2013-02-17) 
What is a web framework? - (2012-11-10) 
Django Training Courses - UK - (2012-04-23) 
Why do we need a Model, View, Controller architecture? - (2012-02-25) 
Your PHP website - how to factor and refactor to reduce growing pains - (2011-09-24) 
Using functions to keep look and feel apart from calculations - simple C example - (2011-04-09) 
Improving the structure of your early PHP programs - (2009-05-25) 
Presentation, Business and Persistence layers in Perl and PHP - (2006-04-17)
Some other Articles
Defining a static method - Java, Python and RubyString to number conversion with error trapping in RubyNeatly formatting results into a tableConstants in RubyThe Model, View, Controller architecture (MVC) - what, why and how.Sunday Evening, City of LondonCheat Sheet - what do you need for Ruby on Rails?Scope of variables - important to Ruby on RailsSearch and replace in Ruby - Ruby Regular ExpressionsAnswers on Ruby on Rails