Wednesday, August 22, 2012

MVC (Model View Controller): For my Ninth Grader

While talking to my son, I realized that making him understand MVC is tough if I talk all geeky language. So I decided to explain the whole thing in very simple English and bring to the context which he can correlate.

Every high school has School newsletter which is published on period basis, say monthly. For Homestead High it is called, Mustang Pride. Naturally there will be student journalists and editor who will collect material, compile news items and articles. They will also receive the feedback from Mustang Pride readers which primarily consists of Students, school staff and parents. There are certainly some guidelines and rules which sets allow to disallow publication of articles and news items in Mustang Pride. The interaction of all these players can be depicted in following figure.


So with the start of achedmic year, in Aug first edition for year 2012 Mustang Pride is published. Readers give their comments and cuggestions to Mustang Pride’s journalists and editor. After take comments and suggestions into consideration and depending upon material available in their repository and under the rules and guidelines of the newsletter, Sep edition of Mustang Pride is published.

This interaction keeps on going every month.

This interaction has very interesting aspect. Readers want to read what they like as per their taste and pass that to journalists and editor. But editors cannot publish anything and everything; they are bound by guidelines, rules and material available with them. This check keeps system in balance.

Now translate this whole thing into computer systems.

Model is one who has repository of data and rules.

View is one which is shown to user. It is user facing aspect of model

Controller is one who interacts with user and update Model and influence model about what view should be shown to users.


Instead of paper version of Mustang Pride, lets’ make online version of it. For example purpose, Mustang Pride web site has very simple format.

Page 1: At this page user can select month and year of Mustang Pride. On click of month of a particular year, user lands to front page of that month of Mustang Pride (Page 2).

Page 2: Front page of Mustang Pride of a particular month lists various articles and news items and link to them. On click to that link, user lands to that specific article or news item.


Any other page: These are article/news item specific page. Each page has content related to article/news article, hyper link to next and previous page.


These pages constitute View part of the equation. In human body you can compare View to Skin, which is visible part.

There should be some repository of contents (articles and news items) and rules which affect what to be shown to user. This repository can be simple text files or database which stores content. Rules can be written any programming language – Java, PHP, Pyton, etc. Suppose depending upon users (say student, school staff or parent) you decide to show different material (hence View). These rules will reside in Model. In human body you can compare Model to Brain.

Now we have Model and View. A user sees View and Model is repository. But, how users interact with online version of Mustang Pride. This interaction happens via Controller.

If a user clicks on next on a certain page, what to be shown next? If user click on month for a particular year, which page to be shown to him? Controller carry these questions (may be with partial answers) to Model. Model answers these questions and shows appropriate page (View) to user. Controller is like your nervous system which carries messages from your skin to brain. In response brain updates itself and updates your skin (by blush or any other way).

Why programmers implement use MVC, why not MV (Model View). User sees a page and asks for next page and this logic can be embedded in View itself. Yes, this is doable and smaller computer applications do the same. But think about big applications (where Views reaches into hundreds or more). If any change to be implemented, one need to change multiple places and chances of error increases. MVC keeps the logic centralized, so maintenance is easier. Second aspect is complexity. Think about complex computer applications like bank, insurance, stock trading, retail, etc. These applications are immensely complex due to numerous types of users – various types of customers, various staff members with varying roles and responsibilities, various government officials, etc., complex logic. If all aspects are not arranged nicely then think about mess.

Though I have taken example from web based application but MVC is equally useful for non-web based applications as well.

5 comments:

  1. Thank You, Very Simple and easy explanation.

    ReplyDelete
  2. Good explanation. Thanks!

    ReplyDelete
  3. good .... thanx

    ReplyDelete
  4. Thanks!Good and clear explanation.

    ReplyDelete