Models, Views and Controllers

The MVC design pattern is one of the most widely used design patterns around, but it's talked about so much by experienced developers that sometimes noobs can get mislead as to the meaning and purpose of it. So here's an overview of the design pattern and what it's parts are for.




We'll start with a high-level flow of events:

1.) The user interacts with the user interface, which sends a message to the controller.
2.) The controller receives the message, converts it into a form that the Model can understand and interact with, and then sends it to the Model.
3.) The Model accepts the new information, and makes changes to it's own state reflecting the new information that it's just received.
4.) The view detects the changes in the Model, and updates itself accordingly.
5.) Repeat 1 - 4.

This is very high level and can change a little with every implementation, but the overlying theme and idea is there. Also, the way in which the view detects the changes will vary a lot : it can query the model every so often, there could be an event fired when the Model changes that is detected by the View, or the Model can notify the View.

Model

The Model is the representation of your application's data. Models contain most or all of the business logic of your application.

View

Views represent the user interface of your application. They provide your Controllers with data from the user, and provide the user with data stored in the application - in the Models.

Controller

Controllers connect the Views and Models together. They act as a translator between the two, taking data from the View, translating it into something the Model can understand, and then passing it on to the Model. They can also operate in the reverse direction in many cases.

If you want to use MVC in one of your ASP.NET web apps, checkout ASP.NET/mvc. And Ruby on Rails uses the MVC design pattern too!

Now, if anyone starts talking about how MVC helped them build a solid webpage, you can engage them in conversation. Maybe it's a cute girl who said it? Maybe you can impress her with your Ruby on Rails knowledge by reading the previous post too!

:P

...Love you.

Comments

Popular Posts