MVC Architecture
MVC is a design pattern used in most of theenterprise applications. The MVC (Model-View-Controller) architecture dividesthe web-based application into threeparts: the model, the view and the controller.
Thereare two types architecture used by web-based applications
Model1- This is page centric design pattern. Here JSP page alone responsible forprocessing the incoming request and replying back to the user. This architectureis good for simple applications not for complex applications. Here large amountof Java code required for data processing embedded inside the JSP page. Thisresults in a messy flow of control and design ugliness for the application.Maintenance will be nightmare for the application developed using Model 1architecture.
The following diagram represents the Model-1architecture
Model2 (MVC) –
Model View Controller or model 2 architecture is to separate the business logic and application data from the presentation data to the user.
Following diagram illustrate MVC Pattern in details:
Model
A model represents an application’s data and contains the logic for accessing and manipulating that data. Any data that is part of the persistent state of the application should reside in the model objects. The services that a model exposes must be generic enough to support a variety of clients. By glancing at the model's public method list, it should be easy to understand how to control the model's behavior. A model groups related data and operations for providing a specific service;
these group of operations wrap and abstract the functionality of the business process being modeled. A model’s interface exposes methods for accessing and updating the state of the model and for executing complex processes encapsulated inside the model. Model services are accessed by the controller for either querying or effecting a change in the model state. The model notifies the view when a state change occurs in the model.
View
The view is responsible for rendering the state of the model. The presentation semantics are encapsulated within the view, therefore model data can be adapted for several different kinds of clients. The view modifies itself when a change in the model is communicated to the view. A view forwards user input to the controller.
Controller
The controller is responsible for intercepting and translating user input into actions to be performed by the model. The controller is responsible for selecting the next view based on user input and the outcome of model operations. The controller is often programmed as a Java Servlet.
Advantages of MVC Architecture:
They are reusable : we can reuse Model components. The separation of model and view allows multiple views to use the same enterprise model.
When the problem recurs, there is no need to invent a new solution; we just have to follow the pattern and adapt it as necessary.
We can easily support a new types of clients using MVC architecture. To do this we only need to write a view and some control logic and then wire them to the existing enterprise application.
They are expressive: By using the MVC architecture our application becomes more expressive.
Many technology implement MVC design pattern. such as Struts, Spring.
Example of MVC architecture:
When the user submits Registration form, a request is sent to the controller that is servlet handler.
The servlet handler dispatches the request to the appropriate control class here its is .ResgistrtionAction
The Control performs the action requested by the user on the Model ResgistrionForm model
The Model returns a response; in this example, a successful submission of the registration form.