views:

58

answers:

2

Can anybody point to/give a very very correct example for the Model-View-Controller paradigm? I mean something really simple (like an inches to cm converter) implemented in some easy-to-understand language (VB.NET?) absolutely and exactly following MVC principles.

+1  A: 

This one's pretty good: http://www.c2.com/cgi-bin/wiki?ModelViewController

But really, MVC is very simple:

Model: Your DATA. View: The thing that controls how your data is PRESENTED. Controller: Sits between the data and the view, other data sources, and controls any changing of the data that needs to be done.

This isn't to say that your view won't itself be built out of models, views, and controllers, but if you sort things according to these questions, things will usually stay pretty clean:

  1. Is it something the user interacts with, or that formats things for a user? It goes in the View.
  2. Is it just a representation of data, with little associated "smarts"? It's a model.
  3. Everything else goes in the controller.
Curtis
Oh, and before the flaming starts: None of these are firm rules. Like any "pattern", these are just guidelines that keep things organized. Preserve the paradigm as long as you can, since it keeps things easy to understand. But don't be enslaved by it.
Curtis
Does this make sense? GUI asking for inches and associated event handlers = View. Methods called by view to deal with data from GUI = Controller. Methods called by Controller to convert inches to cm = Model.
Andrew J. Brehm
The biggest thing in my experience is that you want to be able to replace the view without changing what lies below. So, I'd probably put methods to copy data from the GUI to the Model in the View, myself. Then have the view request that the controller do the unit conversion. The controller places the new results in the model, and then the view displays the updated model to the user (that's the observer paradigm cropping up there).
Curtis
+1  A: 

I am created one small login application in java(GWT) which is following MVC pattern.Though it is in java but I used interfaces and classes in it.So hopefully you will understand it.There is one Controller class which is following singleton pattern.You can get all source from here

Rupeshit