Wednesday Aug 07

Key Concepts

MVC Tutorial

PDFPrintE-mail
Thursday, 14 April 2011 08:29
AddThis Social Bookmark Button

Model-View-Controller Tutorial

Model–view–controller (MVC) is a software architecture concept considered as an architectural pattern in software engineering. The pattern isolated domain logics (considered as application logic for the user) from the user interface (GUI). Decoupling the domain logic from the user interface permit independent developlment , testing and maintenance of each. It is usually called separation of concerns.

Separation of concerns is a concept that, when applied to software development, deals with creating distance between dissimilar aspects of your code.

The original implementation is described in depth in the influential paper "Applications Programming in Smalltalk-80: How to use Model–View–Controller". 

MVC was first described in 1979 by Trygve Reenskau.

Control flow is generally considered as follows:

  1. The user interacts with the graphical user interface in some way (for example, by pressing entering input and validating). 
  2. The controller handles the input event from the user interface, often via a registered handler or callback, and converts the event into an appropriate user action, understandable for the model. 
  3. The controller notifies the model of the user action, possibly resulting in a change in the model's state. (For example, the controller updates the user's shopping cart.)[4] 
  4. A view queries the model in order to generate an appropriate user interface (for example the view lists the shopping cart's contents). The view gets its own data from the model. In some implementations, the controller may issue a general instruction to the view to render itself. In others, the view is automatically notified by the model of changes in state (Observer) that require a screen update. 
  5. The user interface waits for further user interactions, which restarts the control flow cycle.


Pros

Simplified model for multiple related views

MVC allows you to have multiple versions of a page that displayed the same data, but in with a different format or perspective.

Unit testable

One of the biggest challenges with GUI is that testing is difficult. Not only for the actual output not easy to test, but the code-behind would tend to be a place that would contain important code that would never get unit tested. With both MVC and gui frameworks, it’s best to keep your logic away from the page, but it’s not always easy or ideal. MVC makes it simple to unit test the logic that is specific to a page. To do so, you simply test the actions in your controllers, which are regular, easy to test classes.

Easy Maintenance

Sites made with MVC pattern are a lot easier to maintain. After you get used to the package structure used, it's much cleaner than dumping all functions and stuff into a class. The separation of Java code from the templates/Style also makes it easy to change site layout and such without breaking any functionality. Well written MVC applications are also quite modular. You can reuse components from old sites in new ones.

 

Cons

Difficult to convert for existing code

MVC is not only a framework in this case, but a style. It is possible to convert specific pages as needed, but the cost is high. The problem is that MVC is radically different, so the benefit of converting probably isn’t worth it for most of your existing Java code.

Related architecture articles

Tags: architecture, user, interface, easy, view, logic

Add comment


Security code
Refresh

Java Tutorial on Facebook