Model 1 and Model 2 (MVC) Architecture
In this article, I am going to discuss Model 1 and Model 2 (MVC) Architecture. Please read our previous article where we give a brief introduction to Java Struts Framework. At the end of this article, you will understand the following pointers in detail.
- Model 1 Architecture
- Advantage of Model 1 Architecture
- The disadvantage of Model 1 Architecture
- Model 2 (MVC) Architecture
- Advantage of Model 2 (MVC) Architecture
- The disadvantage of Model 2 (MVC) Architecture
- The solution of Model 2 Architecture: Configurable MVC Components
Model 1 Architecture
To build up web applications Servlet and JSP are the two advanced main technologies.
Servlet is considered better than CGI. Servlet technology doesn’t create processes, rather it creates threads to handle requests (or string to deal with solicitation). The advantage of making a thread over process is that it saves memory consumption. Thus, numerous consequent solicitations can be effectively dealt with by servlet.
The problem with Servlet Technology is that the recompilation always takes place whenever any modification occurs in the design of code. It never provides a separation of concern. Presentation and Business logic are blended up.
JSP becomes the solution to all problems of Servlet. It provides a much better partition of concern; it becomes easy to isolate Presentation from Business logic. There is no need for redeployment if JSP is modified. JSP works as a support in developing web applications using JavaBean, custom labels, and JSTL to separate Business logic from our JSP, as a result, the debugging and testing becomes easy to do.
Here, is the brief description of the flow of model 1 Architecture from the above diagram
- The request for the JSP page is sent by Browser.
- JSP accesses Java Bean and invokes business logic
- Database connect by JavaBean and data cannot be lost
- JSP generated response is sent to the browser
Advantages of Model 1 Architecture
The easy and quick development of web applications.
Disadvantages of Model 1 Architecture
- Navigation control is decentralized: only because of each JSP page that is only responsible for managing application state and confirming authentication and security.
- Time-consuming: To create custom labels in JSP much more effort is needed. So we are not able to use scriptlet tags.
- Hard to extend: It is used in smaller, simple task applications.
Model 2 (MVC) Architecture
MVC is the most used software design pattern in the app and web development. The design pattern consists of –
Model: stores & manages data
- It consists of the system’s business logic.
- It contains all the data of the application.
- It represents the state of the application.
- It consists of those classes that have a connection to the database.
- The controller makes a connection with the model, fetches the data, and sends it to the view layer.
- The model connects with the database and stores the data into a database that is connected to it.
View: Graphical User Interface
- It consists of HTML, JSP, etc. into it.
- It represents the UI of the application.
- It is used to show the data that is being fetched from the controller, in turn, it fetches data from model layer classes.
- This view layer shows the data on the UI of the application.
Controller: Brains of the application
- The controller connects the model and view.
- The controller converts inputs from view to fulfill the demand to retrieve/update data in the model.
- The controller receives input from view and uses logic to convert the input to a demand for the model that grabs the data and the controller passes data to the view from the model back for the user for better display.
Advantages of Model 2 (MVC) Architecture
- The development of the application becomes fast.
- Multiple developers’ collaboration is easier.
- Easier to Update the application.
- As multiple levels are written properly in the application, thus debugging is easier.
Disadvantages of Model 2 (MVC) Architecture
- Understanding of the MVC architecture is difficult.
- Must have strict rules on methods.
The solution of Model 2 Architecture: Configurable Components
- The declarative approach is used to define view components, request mapping, etc.
- Configurable MVC support can be easily provided by the Struts Framework.
- All the action classes and view components in Struts 2 are defined in the struts.xml file.
In the next article, I am going to discuss Java Struts 2 Components in detail. Here, in this article, I try to explain Model 1 and Model 2 (MVC) Architecture and I hope you enjoy this Model 1 and Model 2 (MVC) Architecture article.