MVC Architecture
MVC is popularly called, is a software design pattern for developing web applications. A Model-View-Controller pattern is made up of the following three parts:
- Model - The lowest level of the pattern which is responsible for maintaining data.
- View - This is responsible for displaying all or a portion of the data to the user. The View represents the visualization of the data that model contains.
- Controller - Software Code that controls the interactions between the Model and View. It keeps view and model separate.
Fig. MVC Architecture
The Model-View-Controller (MVC) object-oriented architecture originally came from Smalltalk-80 as a methodology to separate user interface presentation from the underlying data. The purpose of MVC is to decompose the whole system into three sub-systems (modules) that are Model, View, and Controller. It is also called a component-based architectural style.
The MVC is a triad of classes is used to build user interfaces in Smalltalk-80. MVC consists of three kinds of objects.
- The Model is the application object;
- The View is its screen presentation, and
- The Controller defines the way the user interface reacts to user input.
MVC decouples views and models by establishing a subscribe/notify protocol between them. A view must ensure that its appearance reflects the state of the model. Whenever the model's data changes, the model notifies views that depend on it. In response, each view gets an opportunity to update itself. This approach lets you attach multiple views to a model to provide different presentations.
The following diagram shows a model and three views. The model contains some data values, and the views defining a spreadsheet, histogram, and pie chart display these data in various ways. The model communicates with its views when its values change, and the views communicate with the model to access these values.
A Simple MVC Example in Java
The following example illustrates a simple implementation of MVC architecture in Java where there is only one Java class in each of the three modules in the MVC architecture.
- The MyBean JavaBean class plays the role of model.
- MyServlet Servlet class plays the role of controller and
- The fromServlet JSP plays a role of view in the MVC architecture.
Below fig. shows the architecture diagram of this Web application.
A simple example of MVC architecture
This example emphasizes the MVC so we omit the user input interfaces. The myServlet Servlet set a username and stores this name in a JavaBean named myBean, then transfers the control to a JSP page named fromServlet.jsp which retrieves the username from the myBean and displays on a Web page.
0 Comments