Concept:
The full name of MVC is the model View Controller, which is the abbreviation for the models-view-controller, a software design paradigm that organizes the code with a method of business data, logic, and interface display separation. Aggregating business logic into a single component does not require rewriting business logic while improving and personalizing the interface and user interaction. MVC is uniquely developed to map the traditional input, processing, and output functions in a logical graphical user interface structure.
The most typical MVC is the JSP + Servlet + JavaBean pattern.
MVC begins in the desktop program, M refers to the business model, v refers to the user interface, C is the controller, the purpose of using MVC is to separate m and v implementation code, so that the same program can use different representations. C exists to ensure the synchronization of M and V, and once M is changed, V should be updated synchronously. Better adjust the mix of M and V.
MVC is a framework pattern, in the final analysis is a framework, not a design pattern, the framework is often code reuse, and design patterns are design reuse, and the architecture is in between the two, part of the code reuse, part of the design reuse, and sometimes can be reused.
View:
A view is an interface that the user sees and interacts with, and can be interpreted by the browser as all the languages of the user interface, the most common being HTML.
Model:
Of the three parts of MVC, the model has the most processing tasks. For example, to the operation of the database, the data returned by the model is neutral, that is, the model is not related to the format of the data to be displayed, such a model can provide data for multiple views, that is, the data displayed in a variety of display modes, such as CSS can give the same data multiple display mode, Because the code applied to the model can be reused by multiple views only once, it reduces the repetition of the code.
Controller:
The controller accepts the user's input and invokes the model and view to complete the user's needs, so when you click a hyperlink in a Web page and send an HTML form, the controller itself does not export anything and do any processing. It simply receives the request and decides which model component is called to process the request, and then determines which view to use to display the returned data. Better adjust the mix of M and V. What can be done on the C layer can be done in either the M or V layers. Just for a better layering.
--------------------------------------------------------------------------------------------------------------- --------------------------------------------------------------------------------------------------------------- ---------------
In MVC, view can directly access the model, for example, on a JSP page, you can nest Java code so that you can access the model over the controller on the view so that the view contains the model information, Inevitably, there are some business logic to be included. In the MVC model, more attention is paid to the model, while there are several different displays of the model, namely view. So, in the MVC model, models do not depend on view, " because the core of MVC is model" but view is dependent on model. Not only that, because there are some business logic implemented in view, it is difficult to change the view, at least those business logic is not reusable, because this part is not a common model, other view can not use the code inside the view.
In MVC, view reads data from the direct model rather than through the Controller. This is the main difference between MVC and MVP. So the model in MVC is not completely detached from the view. and the control layer and the presentation layer are sometimes too close, leading to no real separation and reuse.
Advantages:
1: Low coupling
The view layer and the business layer are detached so that the view layer code is allowed to change without recompiling the model and controller code, [for example, overwriting Jsp,html,css,javascirpt's code, without restarting the server] again, A change in an application's business process or business rules requires only changing the model layer of MVC, " For example, by changing the table name query, changing the conditions of some queries, or using dynamic SQL or static SQL, just change model." Because the model is decoupled from the controller and view, it is easy to change the data layer and business rules of the application.
2: High reusability
as technology continues to evolve, more and more ways are needed to access applications. The MVC pattern allows you to access the same server-side code using a variety of different styles of view, because multiple views can share a model that includes any web (HTTP) browser or wireless browser (WAP), for example, a user can order a product from a computer or via a mobile phone. Although the order is not the same way, but the way to deal with the order of products is the same. Since the data returned by the model is not formatted, the same artifacts can be used by different interfaces. " For example, the model layer implements the same paging, different view layers can be used in 10,000 different display methods, such as Baidu search the following pagination and Google search the following page"MVC reduces the technical content of developing and maintaining user interfaces.
3: Deploy fast
Using the MVC pattern allows for a considerable reduction in development time, which allows programmers (Java developers) to focus on business logic, and interface programmers (HTML and JSP developers) focus on presentation. " For example, the front end can be divided into jobs, high efficiency, convenient multi-developer division of Labor "
4: High maintainability
Separating the view layer and the business Logic layer also makes Web applications easier to maintain and modify. For example: If you want to change the business logic, only to change the business logic, if you want to change the view, only to change the view, if you want to increase the functionality, only need to increase, the most benefit of layering is easy to lower maintenance costs of late maintenance, and add new features, improve code reuse, and thus improve development efficiency "
5: Advantageous software engineering management
Because different layers have their own roles, each layer of different applications has some of the same characteristics, which facilitates engineering and tool-based management of program code. The controller also provides the benefit of using a controller to join different models and views to complete the user's needs, so that the controller can provide a powerful means of constructing the application. Given some reusable models and views, the controller can select the model to process based on the user's needs, and then select the view to display the processing results to the user. " because the controller focuses on allocation, better integration of views and models "
Disadvantages:
1: No definite definition
It's not easy to fully understand MVC. Using MVC requires careful planning, and because its internal principles are complex, it takes some time to think. At the same time, due to the strict separation of the model and view, this also brings some difficulties to debug the application. Each component needs to be thoroughly tested before it can be used.
2: Not suitable for small, medium-sized applications
It takes a lot of time to apply MVC to applications that are not large in size and often outweigh the costs. " This is the most obvious drawback, for example, we just need to go to the database to check information, if not layered design we can directly from the view layer to the model to access, the efficiency will improve, if the complexity of the code at the expense of a layer, the code is greatly increased, at this time the development efficiency is reduced "
3: Adding complexity to system architecture and implementation
For a simple interface, strict adherence to MVC, which separates the model, view, and controller, increases the complexity of the structure and may result in excessive update operations and reduced operational efficiency.
4: An overly tight connection between the view and the controller
Views and controllers are separated from each other, but are tightly connected parts, the view has no controller, its application is very limited, and vice versa, which hinders their independent reuse. " For example, it is not always possible to access the model directly in a JSP page, usually in the logical control layer for processing, servlet"
5: View inefficient access to model data
Depending on the model operator interface, the view may need to be called multiple times to obtain sufficient display data. Unnecessary frequent access to unchanged data will also compromise operational performance. " For example, I didn't update a subset of the data on the page, but the submission to the model layer would also get back to the display"
6: General Advanced Interface Tool or constructor does not support mode
The cost of transforming these tools to accommodate MVC needs and to build separate parts is high, which can make MVC difficult to use.
Summarize:
Advantages: Layered, clear structure, low coupling, large-scale project code reuse has been greatly improved, the development of a clear division of labor, improve the efficiency of development, maintenance convenience, reduce maintenance costs.
Cons: Simple small projects, the use of MVC design instead will reduce the development efficiency, layer and layer although separate, but the correlation is too strong, not to do independent reuse.
An overview of MVC in Java and its pros and cons