You are on page 1of 8

MVC Pattern

Table of Contents

MVC (Model View Controller): Stop starting from Scratch

What are Patterns?

What is Pattern Programming?

What is MVC?

Model:

View:

Controller:

How MVC Works in Web Application Development?

Why use MVC framework in web development?

Separation of Concern:

Modularized Development:

Pattern based development:

More Control over URLs:

Maintainability and Code Reuse:

Test Driven Development:

Platform and language independent:

Is MVC Silver Bullet to all web development problems?

Adds additional level of complexity:

More files to manage:

Rigorous separation between the model and view:

Reference
MVC (Model View Controller): Stop
Starting From Scratch
In this article we will explorer the concept of pattern and pattern programming.
Go into the depth of MVC (Model View Controller) pattern and how it has been
implemented in context of web application development.

What are Patterns?


A pattern is a solution to a problem in a given context. As it becomes reliable it
can be followed over and over.

We apply patterns in our day to day behavior; in fact human brain is pattern recognizing
machine. It is so brilliant at a pattern creating and pattern recognizing that we hardly ever
think twice about pattern that we are applying.

Do we ever think about the route that we are going to follow when we move from home to
office? Well that’s because pattern is in working.

So patterns are very normal to human, and why not apply patterns to the programming as
well. With the introduction of high level language and programming become more and
more specific to domains we can see that several patterns are formed and applied to
computer programming as well.

What is Pattern Programming?


Before moving further lets clear this out that, No pattern programming is not a
library or class or even a piece of code.

Pattern programming is set of method to apply a standard solution to a specific type of


problem in your code. They are simply guidelines, not hard and fast rules. Other people
might have run across the same type of problems that all programmers run across, and
they developed some good methods to handle those problems. These are called patterns.

Now once these common sets of solutions to the problems have been set, they can be
shared and talked about in common way by programmers across the world. When
programmers hear a specific patterns being talked about then they instantly have an idea
of how the program is organized and implemented. They know by the pattern how
specific problems in the program are addressed.

It is bit like talking to your doctor. You tell about your headaches, stomachaches and way
of your fever and by diagnosis of pattern doctor predicts about your disease.

As more programmers know about the most common patterns, the code being written will
become more and more readable and more understandable over time. The code becomes
much more maintainable, resulting in the better quality software, webware with the
advantage of reduce cost in development cycle.

Christopher Alexander says each pattern is a three-part rule which expresses a relation
between a certain context, a problem, and a solution.

Pattern in the software development was especially popularized from the publication of
the book named,

“Design Patterns: Elements of Reusable Object-Oriented Software”


by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides or often referred to
as Gang of Four (GoF).

Design patterns represent a solution to problems that arise when developing software
within a particular context. There are different types of patterns and sub-patterns,
generally the patterns can be divided into these categories:

1. Creational Patterns
Creational patterns are standard ways to create objects.
2. Structural Patterns
Structural patterns are standard ways to organize the relationship of objects.
3. Behavioral Pattern
Behavioral patterns are standard ways to have communications between objects, it
also has to do with other relationships between objects and lot more.

What is MVC?
Model View Controller a.k.a MVC was conceived in 1978 by Ward Cunningham and
Kent Beck who were working with SmallTalk and designing GUIs at Xerox PARC. The
original implementation is described in depth in the influential paper,

“Applications Programming in Smalltalk-80: How to use Model-View-Controller”.

Model:
The Model part describes the business logic of the problem. It
encapsulates core data and logic. Model is often related with the
business logic of the application. It knows all the data that needs
to be displayed. It is always isolated from the User Interface (UI)
and the way data needs to be displayed.

View:
The View part describes and present the problem to the user with
the mental model with user can understand and take some action
to it. It normally uses read-only methods of the model and queries data to display them to
the end users. It may be a window GUI or a HTML page. View encapsulates the
presentation of the data; there can be many views of the common data.

Controller:
It acts as a interacting glue between models and views. It accepts input from the user and
makes request from the model for the data to produce a new view.

How MVC Works in Web Application


Development?
Web has evolved from the humble static html pages where the most complex thing was to
master table col span and row span, to today’s dynamic database driven user content
centric application which allows dynamic range of user interactivity.

Traditionally web has been content driven platform, and both server side and client side
scripts have been intermingle with html code to create dynamic web applications.
Integrating so much of scripting languages from humble html code to server side scripts
like php, asp to client side scripts like javascript, vbscript, and integration of technologies
like applets, flash in a single page is like madness. MVC pattern can throw some kind of
method to this madness. Put the complexities under control and let developer keep hair on
their head!

The MVC patterns in web development gained lot of popularity with the introduction of
ruby on rails platform. It made the maintenance, expansion, exchangeability of the web
application very easy. And unit testing in web application was never heard of trend before
this.

MVC divides the code flow in three parts, the model, the view and the controller.

• Model: is the part that interacts with the database, it has functions that wrap the SQL
queries and results.

• View: Are the output of the queries run on a HTML file or a template.

• Controller: is the main glue in the operation, it is the processor of all things. It
contains functions that take data from the view and post it to the models and vice versa.

A figure below portrays how MVC function:

The web application users typically interact in the views via the functions of the controller
that serve up the view. The controller being in the middle of the view and model does
most of the processing. It’s a heated debate on making the controller heavy or the view
heavy but putting the business logic in the controller and only database transactions in the
model makes the controller heavy.

Due to separation of logic various essential factors for a web application and be achieved
with use of MVC. A simple example may be the change in look and feel of the website
also termed theme/skin/template. If the business and presentation logic is coupled together
then changing a theme will be a big headache but with efficient use of MVC it might turn
out to be a piece of cake.

Based on keeping the business logic and presentation logic separate, all the presentation
logic with the XHTML or HTML and CSS lie in the view's part where as all the business
and processing logic rests on the Controller and Model which makes it easier to code any
kind of web application.
Why use MVC framework in web development?
Some of the reasons to use MVC frame work.

Separation of Concern:

MVC has three components whose operations are quite isolated from each other. For
example; people working on view part can concentrate only on the UI and the part visible
to the end users; people working on model part can concentrate on the business logic and
the functional requirements of the system or ‘What’ part of the system and finally people
working on the controller section may have knowledge of both view and model section so
that interaction between other two components could be made easily. There is clear
designation of roles for each stakeholder of a system.

Modularized Development:

Modularization is the process of dividing any complex problem into smaller sub-modules
and we human have been following this approach from the time unknown. In MVC, we
divide our system into three parts in order to reduce complexity.

Pattern based development:

As defined in the earlier section, pattern is the proven solution for any problem in a
particular context. MVC itself is a pattern implemented in the presentation layer where it
handles user’s interaction (controller) with a particular model through view. MVC is a
proven solution for many contexts especially interactive web applications so following it
in order to build a system may be comparatively more effective and trust worthy.

More Control over URLs:

Almost all MVC based frameworks have the feature of URL routing that gives us more
control over the URL we desire. For example; http://www.foo.com/content_id/edit where
‘content_id’ may be unique ID and ‘edit’ may be controller action. This kind of url control
is specially good for Search Engine Friendly website, where search engines can easily
crawl your website and your location is not lost due to change in code directory location.

Maintainability and Code Reuse:

The Modular design of MVC supports the design goal of reusable software. As MVC
requires a definite rule and style for coding, the result can be much more maintainable and
reusable software.

Test Driven Development:

By following MVC, one can easily tests each and every part of the system. Moreover,
most of the MVC frameworks do have one or more built-in testing frameworks.

Platform and language independent:

MVC is simply a pattern which can be implemented in any language or any platforms.
Most of the popular languages and platforms like .NET, PHP, Ruby, Python have one or
more MVC based frameworks. So once you know the MVC funda, you can implement in
the platform of your choice.

Is MVC Silver Bullet to all web


development problems?
Well to your disappoint it probably is not the silver bullet to all the demons of web
development. It does seem to share its fair share of problems and limitation.

Adds additional level of complexity:

MVC can increase the level of complexity of a system since MVC requires in depth
learning curve and each framework tends to have variation in MVC rule implementation.
More files to manage:

This may be context dependent. Some people might feel odd when dealing with more
system and helper files. A MVC based system has comparatively more number of files
than a non-MVC based system.

Rigorous separation between the model and view:

There are certain web applications where this separation between model and view are not
always possible or desire.

Reference
Following this tried and tested MVC pattern there are lots of frameworks developed. For
PHP only Wikipedia here (http://en.wikipedia.org/wiki/Model-view-controller) lists more
than 20 frameworks

PHP Frame Works:

• CodeIgniter
• Cake PHP
• Symphony
• Zend Framework

Ruby Frame Works:

• Ruby on Rails
• Nitro
• Merb
• Camping

Python Frame Works:

• Django
• Pylons
• TurboGears for Python

• web2py Web Framework

You might also like