Brief description  about Online courses   join in Online courses

MVC Architecture (Model 2 Architecture)

Nirupama  Pathak
Nirupama Pathak
Java Developer

Flexibility in large component based systems raise questions on how toorganize a project for easy development and maintenance while protectingyour data and reputation, especially from new developers and unwittingusers. The answer is in using the Model, View, Control (MVC) architecture.An architecture such as MVC is a design pattern that describes a recurringproblem and its solution where the solution is never exactly the same forevery recurrence.

To use the Model-View-ControllerMVC paradigm effectively you must understand the division of labor withinthe MVC triad. You also must understand how the three parts of the triadcommunicate with each other and with other active views and controllers;the sharing of a single mouse, keybord and display screen among severalapplications demands communication and cooperation. To make the best useof the MVC paradigm you need also to learn about the available subclassesof View and Controller which provide ready made starting points for yourapplications.

In the MVC design pattern , application flow is mediated by a central controller.The controller delegates requests to an appropriate handler. The controlleris the means by which the user interacts with the web application. Thecontroller is responsible for the input to the model. A pure GUI controlleraccepts input from the user and instructs the model and viewport to performaction based on that input. If an invalid input is sent to the controllerfrom the view, the model informs the controller to direct the view thaterror occurred and to tell it to try again.

A web application controller can be thought of as specialised view sinceit has a visual aspect. It would be actually be one or more HTML formsin a web application and therefore the model can also dictate what thecontroller should display as input. The controller would produce HTML toallow the user input a query to the web application. The controller wouldadd the necessary parameterisation of the individual form element so thatthe Servlet can observe the input. This is different from a GUI, actuallyback-to-front, where the controller is waiting and acting on event-driveninput from mouse or graphics tablet.

The controller adapts the request to the model. The model represents, orencapsulates, an application's business logic or state. It captures notonly the state of a process or system, but also how the system works. Itnotifies any observer when any of the data has changed. The model wouldexecute the database query for example.

Control is then usually forwarded back through the controller to the appropriateview. The view is responsible for the output of the model. A pure GUI viewattaches to a model and renders its contents to the display surface. Inaddition, when the model changes, the viewport automatically redraws theaffected part of the image to reflect those changes. A web applicationview just transforms the state of the model into readable HTML. The forwardingcan be implemented by a lookup in a mapping in either a database or a file.This provides a loose coupling between the model and the view, which canmake an application much easier to write and maintain.

Advantages of Model 2 Architecture 

Since there is no presentation logic in JSP, there are no scriptlets. This meanslesser nightmares. [Note that although Model 2 is directed towards elimination ofscriptlets, it does not architecturally prevent you from adding scriptlets. This hasled to widespread misuse of Model 2 architecture.] 

With MVC you can have as many controller servlets in your web application. In fact you can have one Controller Servlet per module. However there areseveral advantages of having a single controller servlet for the entire web application. In a typical web application, there are several tasks that you want todo for every incoming request. For instance, you have to check if the user requesting an operation is authorized to do so. You also want to log the user’sentry and exit from the web application for every request. You might like to centralize the logic for dispatching requests to other views. The list goes on. Ifyou have several controller servlets, chances are that you have to duplicate thelogic for all the above tasks in all those places. A single controller servlet for theweb application lets you centralize all the tasks in a single place. Elegant codeand easier to maintain. 

Web applications based on Model 2 architecture are easier to maintain and extend since the views do not refer to each other and there is no presentationlogic in the views. It also allows you to clearly define the roles and responsibilities in large projects thus allowing better coordination among teammembers.

Write your comment now
Reader's comments(1)
1: Quote your sources. You plagiarize too much.
Posted by:Pak Pak - 08 Oct, 2013