One interview question that I’ve been asked repeatedly is “Describe the model-view-controller (MVC) pattern”. I guess that’s technically a demand, not a question.
Anyway, when you’re building software, if you really wanted to, you could put together in one place all the code that deals with 1) deciding what to show the user (View), 2) figuring out what to do with user input (Controller), and 3) doing what the application decided to do in step 2 and in the process, storing and retrieving necessary data (Model). But unless the software is really, really simple (only a little more than “hello world”-simple), trust me, you do not want to do that.
Why not? Because as the software’s codebase becomes larger and more complex, it is likely that you may want to reuse certain components. Like you may want to retrieve some set of data from a number of different places. The code is much more extendable and maintainable when you use MVC.
Typical flow is:
1) The view show the user something and give him/her a means to input/respond to that something
2) The controller figures out what to do with the user input and asks the model to do what it decided to do
3) The model does what the controller told it to do along with getting and modifying relevant data in the process, then passes resulting data to the controller
4) The controller passes the resulting data to the view
5) The view shows the user the results
Fortunately, in the Java world, there are many free open source frameworks that make writing Model-View-Controller applications pretty easy. At my current company, we use a combination of Struts (Controller layer), Tiles/JSP (View layer), Spring (Model layer – application logic), and Hibernate (Model layer – data abstraction). I’m not sure why we do that. Although we only use Spring Framework for its Inversion of Control container, it supports the whole MVC stack.
Next post, I will talk about Struts.




neanderthal said
I don’t get it. You say..
“But unless the software is really, really simple (only a little more than “hello world”-simple), trust me, you do not want to do that.”
which seems to say you don’t like MVC.
and then you talk about hose useful it is….?
some clarification?
Peter said
Sorry, poor writing organization. I meant that you don’t want to put all your code (UI, DB calls, business logic, etc) in a JSP page unless your application is super, super simple.