MVC

Explain MVC (Model View controller) software architecture

Model-view-controller (MVC) is a software architectural pattern for implementing user interfaces and business logic. It divides a software into three layers which manages the request coming from users and separates the internal representation of information.


Components:

The components of MVC are given below

  1. Model, captures the behavior of the application in terms of its problem domain, independent of the user interface. The model directly manages the data, logic and rules of the application. It is mainly used for database interaction.
  2. View can be any output representation of information, such as a chart or a diagram. Multiple views of the same information are possible, such as a bar chart for management and a tabular view for accountants. What we see on screen, comes from view. HTML or display is part of view.
  3. Controller, accepts input and converts it to commands for the model or view. When you hit any URL, firstly calls goes to controller. Here we decide, we have to show plain HTML or need to display data from database to HTML. If we have to display from plain HTML, we simply call view from controller otherwise, we calls model for database interaction, fetch relevant data from database and pass it to view.


Interactions:

In addition to dividing the application into three kinds of components, the model-view-controller design defines the interactions between them.

  1. A model stores data that is retrieved according to commands from the controller and displayed in the view.
  2. A view generates new output to the user based on changes in the model.
  3. A controller can send commands to the model to update the model's state (e.g. editing a document). It can also send commands to its associated view to change the view's presentation of the model (e.g. by scrolling through a document).


Model: The model represents data and the rules that govern access to and updates of this data. In enterprise software, a model often serves as a software approximation of a real-world process.

View: The view renders the contents of a model. It specifies exactly how the model data should be presented. If the model data changes, the view must update its presentation as needed. This can be achieved by using a push model, in which the view registers itself with the model for change notifications, or a pull model, in which the view is responsible for calling the model when it needs to retrieve the most current data.

Controller: The controller translates the user's interactions with the view into actions that the model will perform. In a stand-alone GUI client, user interactions could be button clicks or menu selections, whereas in an enterprise web application, they appear as GET and POST HTTP requests. Depending on the context, a controller may also select a new view -- for example, a web page of results -- to present back to the user.


There are two types of Programming models:

  1. Model 1
  2. Model 2(MVC) Architecture

Model 1 Architecture :

Servlet and JSP are the main technology to develop the web application. Servlet was considered as the superior to CGI. But is has some advantages and disadvantages also.

The main advantage is Servlet creates thread instead of creating process to handle the request. The advantage of Creating thread over process is that it doesn't allocate the separate memory. So many subsequent request can be handlled parallely.

The main disadvantage is :

  1. Servlet mixs up the Presentation and Business Logic.
  2. Servlet needs recompilation after a single change in the code.


To overcome the above mentioned problem, JSP can be used. It provides better seperation of Presentation and Business Logic by using the Java Bean, custom tags and JSTL, not required to compile the source file every time after a single change in the source file.

In this diagram, Users are sending the request through web browser for the JSP pages. JSP access Java Bean and invokes the Business Logic. Java Bean connects to the database to operate with the data and it gives the response to the browser which will be shown to the user through the JSP Pages.

Advantage:

  1. Very easy to implement the web application using MVC 1 Architecture.
  2. Clear separation between business logic and presentation logic.
  3. All objects and classes are independent to each other. So change in one class doesn't need to alter in another class.
  4. Reusable : Multiple views using the same model.
  5. Clarity of Design : Easy to maintain the code and future improvements.

Disadvantage :

  1. Time Consuming : It will take more time to develop the custom tags in JSP.
  2. It is better for small application, not for the large application.
  3. Decentralizatio of Navigation Control : Every page has the logic to determine the next page navigation. If the name of the JSP page is changed then it will affect the other pages also.
  4. Complexity : It is increasing the complexity.


Model 2 Architecture :

Model 2 is based on MVC (Model-View-Controller) design pattern.

Model The model represents the state (data) and business logic of the application.

View The view module is responsible to display data i.e. it represents the presentation.

Controller The controller module acts as an interface between view and model. It intercepts all the requests i.e. receives input and commands to Model / View to change accordingly.

Features of MVC 2:

  1. The MVC2 architecture removes the page centric property of MVC1 architecture by separating Presentation, control logic and the application state.
  2. In MVC2 architecture there is only one controller which receives the entire request for the application and is responsible for taking appropriate action in response to each request.

Difference Between MVC 1 and MVC 2:

MVC 1MVC 2
MVC 1 associates the presentation and Business Logic. MVC 2 isolates the presentation logic from Business Logic.
One component is responsible for receiving the request and sending the response. There are separate components for receiving and sending the response i.e. Controller and View.
As Business Logic and Presentation Logic mixes up, so web designer and web developer can't work simultaneously. As both are separated so web designer and developer can work simultaneously.
Doesn't support reusability of components. Support reusability of components.

View : JSP

Controller : Servlet

Model: JavaBeans

View : JSP/HTML

Controller : ActionServlet

Model : ActionForm(Java Class)

Name
Email
Comments
Back
Funding

We need your support to operate it properly. We have lots of ideas but less fund, so help us with your funding.

Thoughts for the day

If A is a success in life, then A equals x plus y plus z. Work is x; y is play; and z is keeping your mouth shut.

Albert Einstein
Polls
Which device you use most for facebook application?
Laptop/PC
25%

 


Android
50%

 


IOS
16%

 


Blackberry
9%

 


Tutorial On Request
Q. Ask us for any tutorial or any thing which helps to build your career better.
Email:
Query: