History of MVC

Source: Internet
Author: User
MVC has become our most misuse model,MVC is often misused because different MVC variants are obfuscated.

Classic MVC

Classic MVC in the 1970s S, Xerox PARC Trygve proposed the concept of MVC.

It is also applied in the Smalltalk system. to distinguish it from other types of MVC, classic MVC is used in history.

Classic MVC Mode

Model: encapsulate domain data and logic

View: query domain data and present it to users

Conctroller: intercepts user requests and changes domain data
 

In terms of dependency, the model depends on the view and controller, while the view and controller depend on the model.
 

Classic MVC focuses on two separation:

Detach view from Model

Detach controller from view
 
 

Separating a view from a model is mainly based on the following considerations:

Different concerns: the model focuses on the internal invisible logic, while the view focuses on the external visible logic.

Multiple forms: the same model usually requires multiple view forms, such as text and images.

Improve testability: compared with the model, view is not easy to test.
 

Separating a controller from a view is not that important.
 

In the era of software development, views and controllers often have a one-to-one relationship, so they are often merged into UIS. In fact, most UI frameworks didn't implement controller separation from views. Later, with the rise of web, this separation (template technology) began to become popular.
 

Essentially, the structure of classic MVC is shown in. Essentially, view and controller are related to each other, but this association is totally different from the MVP mentioned later, it is more like a by-product of a framework. To avoid confusion, they are omitted here. For details, refer:How to Use Model-View-controller (MVC)

 

Illustration:

The Controller intercepts a user's request through the mouse or keyboard, and then changes the model status.Observer SynchronizationThe status of the view is changed.

View to query the data displayed by the model.
 

Classic MVC is not perfect and does not apply to complicated logic.

For example:

You can drag the scroll bar to adjust the volume. If the volume is greater than a certain value, the background color turns red to indicate a reminder. When classic MVC is used, how does one deal with the logic of red background color?
 

There are two options:

Model triggers a special event, and the view completes the processing of related logic after receiving the event. As we have said before, from the perspective of dependency, the model should completely ignore the existence of the view.

Determine the threshold value of the volume in the view, and then process the relevant logic. However, as we have mentioned earlier, view is not easy to test, and logical processing should be minimized.

 

Application Model MVC


In the 1980s S, parcplace was divided from Xerox PARC and responsible for smalltalk R & D. To adapt to more complex logic, we developed the simplified version of classic MVC, that is, application model MVC, the application model is introduced based on the original architecture, as shown in:

 

 

Illustration:

Application model plays the role of a relay between model, view, and controller.

Next, let's take a look at the previous example. Since neither model nor view is suitable for the logic of changing the background color to red, we can try to put the relevant logic in the application model for implementation, when the user uses the mouse to adjust the volume for an hour, the model triggers a common event. The application model intercepts the event and determines whether the volume exceeds the threshold. If yes, a special event is triggered, view completes the processing of related logic after receiving the message.

Although application model MVC seems to solve the problem of complicated logic, it still has the following problems:

First of all, with the rise of a graphical operating system based on Microsoft Windows, the operating system itself provides a set of native view interfaces to intercept user requests through the mouse or keyboard.

The result is redundant to the Controller.
 


Secondly, in application model MVC, view rendering can only be implemented by means of events, and application model cannot directly operate the view. In some cases, the business logic cannot be conveniently implemented. Next, we will add a new function to adjust the volume by dragging the scroll bar, instead of giving a text box, the user can enter an Arabic number on the keyboard to indicate the volume size. Once the user inputs illegal content (such as English characters), the background color turns yellow to indicate a warning. The problem is that if the user inputs illegal content, the model state should not be changed, but the model state will not be changed, and the view will not be able to receive rendering events.

MVP

In the 1990s S, IBM's Mike potel proposedMVP. At the same time, the Smalltalk team is developing a new generation of framework. When they see MVP, they find that it is not only very similar to MVC, but also solves complicated logic problems, so they decided to use it, due to the complexity of the relationship, they simplified the MVP and eventually looked more like reversing the original MVC and reversing the VC order:
 
 

Illustration:

View intercepts user requests and delegates them to the presenter. The presenter changes the model status. The model notifies the view that its status has changed through the observer synchronization. View queries the model to display data.

The most important thing is that presenter and view hold each other's references. Although view intercepts a user request, it does not process it, but delegates it to the presenter for processing, ensuring testability. At the same time, because the presenter can directly operate the view, it does not have to be limited by the observer mode.

Next, in the example of adjusting the volume, when you drag the scroll bar to adjust the volume for an hour, the view intercepts the request and delegates the request to the presenter. If the presenter finds that the volume is greater than the threshold value, directly operate the view to implement the logic. When the user inputs the volume by using the keyboard for an hour, the view intercepts the request and delegates the request to the presenter. If the presenter finds that the content is illegal, it directly operates the view to implement the logic.

Martin Fowler analyzed the MVP implementation method, classifiedSupervising ControllerAndPassive View.
 



Illustration:

Two MVP categories: supervising controller and passive view

The difference between the two lies in whether the model and view are related. In the implementation of supervising controller, view can query the model. If the model status changes, the view will be notified. In the implementation of passive view, the view cannot query the model. If the model status changes, the presenter is notified to complete the view rendering by the presenter. In comparison, passive view has better testability, but presenter'sCodeThe amount is larger.
 

We have discussed the evolution history of MVC to MVP. With the rise of web, people began to apply MVC, MVP and other knowledge to the web environment, but the web environment has its own particularity, the most important thing is that HTTP is stateless and each request is independent, so it is impossible to implement the observer mode.
 

Web MVC

Br/> JAVAIs the earliest practitioner of Web MVC.Model 2, Using JavaBean, JSP, and Servlet correspond to the three components in MVC respectively, followedStructsThe emergence of Web MVC began to attract public attention, but what really made web MVC popular isRubyCommunityOfRailsShows the general process:
 



Illustration:

A typical web MVC Process

Controller intercepts user requests

The Controller calls the model to complete read and write operations in the status.

The Controller transmits the data to the view

Render the final result of the view and present it to the user
 

In classic MVC, controler can change the model state, and view can query the model state. Therefore, for model, the status of controller and view is equal, but in Web MVC, the Controller becomes a relay, and the main task is to coordinate model and view. In this case, the Controller in Web MVC is equivalent to the presenter in MVP. Why is it web MVC instead of Web MVP? This is because the request is intercepted by the controller rather than the view.

PythonCommunityDjangoThe framework claims that it is usingMTVThe essence is still web MVC.
 

Web MVP

In the era of desktop, Microsoft achieved MVP through winforms, and brought componentized programming to the extreme, greatly improving development efficiency. With the rise of web, Microsoft hopes to continue this programming model, therefore, webforms is used to implement web MVP and codebehind and viewstate are introduced. The advantages and disadvantages of webforms are very prominent, and the length is limited. For details, refer to the following link:

    1. A few words for webforms and some experience in ASP. NET development (1)
    2. A few words for webforms and some experience in ASP. NET Development (2)
    3. A few words for webforms and some experience in ASP. NET development (3)

Note: Microsoft launchedASP. NET MVCMoving closer to Web MVC seems to require both hands.

Illustration:

Microsoft Web MVP vs web MVC. Check whether the request is intercepted by the Controller or view!

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.