Table of Contents
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.
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.
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:
- Creational Patterns
Creational patterns are standard ways to create objects.
- Structural Patterns
Structural patterns are standard ways to organize the relationship of objects.
- 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.
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”.
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.
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.
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.
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 cakeJ.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
There are certain web applications where this separation between model and view are not always possible or desire.
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:
- Cake PHP
- Zend Framework
Ruby Frame Works:
- Ruby on Rails
Python Frame Works:
- TurboGears for Python
- web2py Web Framework