Two of the MVC architecture

Source: Internet
Author: User

1. Introduction

MVC is an architectural design pattern and a design concept. is to achieve the purpose of layered design, so that the code decoupling, ease of maintenance and code reuse. MVC is the abbreviation for 3 words, full name: Model-view-controller (Model-View-controller).

Model:

Model plays the role of the Master of function in MVC, which deals with the business logic and the data model, such as User Selectuser (), and the bean that is returned after its invocation belongs to model. As to how to pass user to the foreground (for example, by bean, using JSP tags for processing, or using JSON format for processing in the foreground using the JS language), you can choose freely.

View:

View plays the role of the display in MVC, which is the upper layer, which shows the data extracted from the underlying (such as tables, graphs, etc.), which are displayed in JSP tags in JSPs, In other foreground frames, the data is processed in JSON format (@controller returns the JavaBean in spring, @RESTcontroller returns JSON-formatted data).

Controller:

In MVC, the controller plays the role of routing control, which is the middle tier, which handles requests sent by the user through the front end (while other mv* patterns may not send requests directly to the model via the controller), and calls the corresponding methods in the underlying to make data requests. Then send the data out.

2. Example

The main framework of MVC is Spring Struts ZF. NET

Take Springmvc as an example to explain how MVC is reflected in the spring workflow.

SPRINGMVC is an open-source framework for MVC, SPRINGMVC=STRUTS2+SPRING,SPRINGMVC is equivalent to STRUTS2 plus sring integration, but here's a question that What is the relationship between SPRINGMVC and spring? This in Baidu Encyclopedia has a very good explanation: means that SPRINGMVC is a follow-up product spring, in fact, spring on the original basis, but also provides the Web application of the MVC module, SPRINGMVC can be simply understood as a spring module (similar to AOP,IOC), the network is often said SPRINGMVC and spring seamless integration, in fact, SPRINGMVC is a sub-module of spring, So there is no need to integrate with spring.

In this diagram, the front-end view is replaced with a JSP, and DAO is another layer of data persistence layer outside of MVC, primarily for database operations.

SPRINGMVC's work flow is

First step: The user initiates the request to the front controller (dispatcherservlet)

Step Two: Front Controller request processor Mapper (handlermappering) to find the processor (Handle): Find by XML configuration or annotations

Step three: Find the processor mapper (handlermappering) as the front controller returns to the execution chain (handlerexecutionchain)

Fourth step: The Front controller (Dispatcherservlet) calls the processor adapter (Handleradapter) to execute the processor (Handler)

Fifth Step: Processor adapter to perform handler

Sixth step: Handler execution to the processor adapter return Modelandview

Seventh Step: The processor adapter returns Modelandview to the front-end controller

Eighth step: The front controller requests the View parser (Viewresolver) to carry out the visual diagram analysis

Nineth Step: View resolver like Front controller return view

Tenth step: Front Controller renders view

11th Step: Front controller responds to user results

For example, ①, childhood play the kind of cassette game consoles, control is the host, generally I buy a host on the line, as long as he is not bad, he can always let me play this kind of game. The view is the TV and the game handle, the television can work independently, he regardless of input is the television signal, the DVD player signal or the game console signal, he just displays, and he decides how we see the effect is how, if I want a bigger or the color display effect, I just need to buy a corresponding TV set on the line, the handle can also be changed, the remote bar or with vibration. The model was a game cassette, and he decided what game I was playing, whether it was the bucket or the Super Mary, and the console and the TV maker never knew what kind of game it might run on. There may be game codes and storage units in the cassette, which are designed according to the needs of the game.

For example, ②, a simple information system using proportional representation for political elections, provides a spreadsheet of input data and several icons representing the current results. Users can interact with the system through a graphical interface. All information indicates that changes in the election data must be immediately reflected. (Referenced from the schema-oriented software architecture-Volume 1 mode system)

That is, once the model's data has changed, the model informs all views.

3. Existing problems

Because PHP does not support object-oriented before, it is a procedural way to create, they will Model View Controller three layer of code mixed together, very confusing. So it solves the problem: maintenance is difficult, development speed is slow, two times the development difficulty is high, the workload is large, the code is reused, the coupling degree is high, the system is inflexible.

4. The advantages and disadvantages of MVC architecture

Advantages:

Low coupling resistance

The view layer is decoupled from the business layer, allowing changes to the view layer code without recompiling the model and controller code, as well as changes in an application's business process or business rules only need to change the MVC model layer. Because the model is decoupled from the controller and view, it is easy to change the data layer and business rules of the application.

High reusability

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.

Low life cycle costs

MVC reduces the technical content of developing and maintaining user interfaces.

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.

High maintainability

Separating the view layer and the business Logic layer also makes Web applications easier to maintain and modify.

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.

Disadvantages:

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.

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.

Increase the complexity of the system structure 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.

An overly tight connection between a view and a 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.

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.

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.

Two of the MVC architecture

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.