Thursday, June 5, 2008

MVC: A very effective old fighter

Most of us have heard of MVC ie Model View Controller. MVC is a famous design pattern used to separate presentation logic and business logic and this bring in Separation of Concern into picture. All web frameworks like struts, ruby on rails, JSF etc are based on MVC design pattern.

Let me define role of Model, View and Controller in a typical web Application.

View
It is responsible for the presentation logic. It displays information supplied by the controller to it. Controller puts the model data in a place where a View can find it. It is also responsible for collecting user inputs and send it to the controller for further action.

Controller
It decides what to do with the input parameters it receive from the view. It decides which model should process the request depending on the request url. It tells the model to update its state and make new model state available to views.

Model
It holds the actual business logic and state . It has methods for updating and getting state of the object. It can also have the logic to communicate with the database. It is entity responsible for interaction with the database.

MVC is not only separating presentation and business logic but to allow different clients to consume models without rewriting the code. Model classes should be independent of the controller i.e Model classes should be plain old reusable java classes. These classes can be reused and work with any client.

Models should not have any idea about the views. It should not know whether a view exists or not.

MVC architecture of 90% of the web application running on the Internet today.

1 comment:

Nimesh Gupta said...

MVC is really powerful as u specifeid. Good to see that you are writing about tech..keep it up :)..this blog reminded me out college time..

Nimesh