What is Model-View-Controller: A Primer

Brief history of Model-View-Controller

 

The original concept of model-view-controller (MVC) first came about in 1978 by Trygve Reenskaug, a Norwegian computer scientist. Although much of its history is shrouded in obscurity, many believe that he collaborated with Xerox Parc to create it while working on Smalltalk.

 

Trygve Reenskaug - MVC
(Source: CW)

Popular use cases

 

Used in object-oriented programming development, it is one of the most widely-used methods to link the user’s interaction with the interface to the underlying data models.

 

MVC can be used for many programming languages and many modern web and GUI frameworks, such as Ruby on Rails (my favorite), Apple’s Cocoa, CakePHP, and Apache Struts, amongst others.

 

The 3 layers of MVC

 

There are 3 basic components to MVC. They are:

 

1. The Model Layer
The underlying data and logic. This object model does not contain any information about the user interface.

 

2. The View Layer
The (visual) representation of its model. Also known as the GUI. A view is usually attached to its model.

 

3. The Controller Layer
A controller is the link between a user and the system. It represents the classes connecting the model and the view, and is used to communicate between classes in the model and view layers.

 

How MVC works

 

To put it simply, the core of MVC lies in the separation of domain objects (what goes behind the scenes) and presentation objects (what we see on the screen), and how they are presented to the real world. Domain objects should function perfectly without presentation objects.

 

The original Smalltalk MVC pattern is essentially a closed loop. The View speaks to the Controller, the Controller speaks to the Model, and the Model speaks to the View.

 

However, many have agreed that a direct communication between the View and Model is not practical for web applications. This is due to the decline in desktop development and rise in web development over time (with its request/response based nature). Instead of a closed loop, think of the MVC model as a horseshoe, with the Controller in the middle. The Controller now “mediates” and bridges the communication between the View and Model.

 

Read a thorough explanation as to why the latter method is preferred. Those guys at Stack Overflow did a pretty great job explaining it.

 

Conclusion

 

After reading countless articles on what model-view-controller is, I learnt that there are many interpretations and use cases for it.

 

From my own understanding, the MVC model is: 1) used in object-oriented programming, 2) promotes the separation of concerns, and 3) is great for medium to large-scale web applications.

 

As for the three layers, let me try describing them as simply as possible. The View is basically the GUI, where a user can interact with it (e.g. clicking theĀ  ‘sign up’ button). The View reports this action to the Controller (e.g. “The user has clicked the ‘sign up’ button”). The Controller receives the message and makes sense of it, eventually manipulating the Model accordingly (e.g. “Load user sign up form”).

 

model-view-controller diagram

 

I hope this helps you derive a clearer understanding of what the Model-View-Controller is.

Leave a Reply

Leave a Reply

Your email address will not be published. Required fields are marked *