Design Pattern (1) -- MVC

Source: Internet
Author: User

MVC(Model-View-controller, Model-View-Controller) is a software architecture model in software engineering. It divides the software system into three basic parts: model) view and controller ).

MVC is not a design pattern, but an architectural pattern used to describe the structure of an application and the responsibilities and interactions of each part of the structure. It was first proposed in 1979 for the first time. However, the environment was somewhat different and the concept of network application did not exist at the time.

Tim Berners-Lee planted the seeds of the World Wide Web (WWW) in the early 1990s S and changed the world forever. Currently, the model we use in network development is actually an adapted version of the original model. The crazy popularity of this architecture mode is because two extremely popular development frameworks include this mode: struts and Ruby on Rails. These two development frameworks are deeply imprinted on the hundreds of frameworks that will be born later.

1. Three-tier MVC Architecture

Model(Model): a data model is used to encapsulate data related to the application's business logic and to process the data. The model has the right to directly access data, such as accessing the database. "Model" does not depend on "View" and "controller", that is, the model does not care about how it is displayed or operated. However, data changes in the model are generally published through a refresh mechanism. To implement this mechanism, the views used to monitor this model must be registered on this model in advance, so that the views can understand the changes in the data model. (Comparison: Observer mode in software design patterns)

View(View): The view layer can display data purposefully (theoretically, this is not necessary ). Generally, there is no program logic in the view. To refresh the view, the view needs to access the data model it monitors. Therefore, you should register the data that it monitors in advance.

Controller(Controller): The Controller acts as an organizational unit between different layers and is used to control application processes. It processes events and responds. "Events" include user behaviors and changes in data models.

  • Controller-forwards requests and processes the requests.
  • View-the GUI designer designs the GUI.
  • Model-programmers compile the functions (Implementing algorithms, etc.) of programs and database experts to manage data and design databases (implementing specific functions ).

2. MVC principles

The idea behind the MVC (Model-View-Controller) architecture mode is very simple. Our applications must differentiate the following responsibilities:

Applications are divided into three main parts, each of which is responsible for different tasks.

3. MVC network applications

Controller(Controller)

The Controller is in charge of user requests (when a user clicks an element on the graphic user interface (GUI) to perform operations, the Controller will receive an http get or POST request ). Its main function is to call and coordinate required resources/objects to execute user requests. Generally, the Controller calls an appropriate model for the task and selects an appropriate view.

Model(Model)

A model is a data rule and data content applied to data. It generally corresponds to objects to be managed by applications. In a software system, everything can be abstracted into a data model that can be processed in a certain way. What are the users, information, and books in the application? They are just a bunch of data that must be processed according to the corresponding rules (the date cannot be a future date, the email has a specific format, and the name length cannot exceed many characters ).

The model provides the Controller with a data expression (such as information, books, and album) corresponding to the user request content ). No matter how we present it to users, this data model will not change. This is why we can choose which view to use to display data at will. The model contains the most important components of our application logic, which are applied to the problem we are dealing. The controller contains the internal organizational logic of the application.

View(View)

The view provides different ways to display model data. It may be a data filling template. There can be multiple views, and the Controller determines which view to use. A network application usually consists of many controllers, models, and views. A controller can be considered as a master controller that receives all user requests and then calls a specific controller to handle different situations.

4. Advantages of MVC

One of the most obvious advantages of MVC is that it clearly separates View display from application logic.

Support for different users and different device types has always been a common problem. For example:

The view obtained for requests from desktops and mobile phones should be different, and the model will return identical data, however, the difference is that the controller selects a View File to display data (we can regard it as a different template ).

In addition to separating the view from the business logic, the separation of MVC also reduces the difficulty of large-scale application design, and the code is more structured, so it is easier to maintain, test, and reuse.

5. MVC application scenarios

The disadvantage of the MVC mode is that it is not easy to fully understand the MVC mode because it is not clearly defined. Using the MVC pattern requires careful planning. Because of its complicated internal principles, it takes some time to think about it. Developing an MVC-mode architecture project will have to spend considerable time considering how to apply the MVC-mode to applications. At the same time, the model and view must be strictly separated, this also makes it difficult to debug the application. Each component must be thoroughly tested before use. In addition, because the MVC mode divides an application into three parts, this means that the same project will contain more files than before.
The MVC pattern is not suitable for small or even medium-sized applications, which brings in additional workload and increases application complexity. However, the current multi-data design framework can directly provide the MVC skeleton for the development of small and medium-sized applications. This problem no longer exists. For large-scale applications with a large number of user interfaces and complex logic, MVC will bring a new step to the software in terms of robustness, code reuse, and structure. Although the initial construction of the MVC Framework requires a certain amount of work, it will greatly improve the efficiency of software development in the long run.

6. MVC implementation example

1) MFC

MFC (Microsoft Foundation Classes) Document/view architecture is Microsoft's early implementation of the MVC mode. MFC divides the project into two categories: cview and cdocument. The document corresponds to the Model in MVC, view is equivalent to view + controller in MVC, combined with the cwinapp category. However, MFC is basically a failed MVC pattern.
Because the document/view definitions under MFC are too vague and the controller (messagemap) is not taken out, the controller can be placed in the view or document, But no matter which aspect is placed, will be bound to the view or document, with no flexibility.

2) swing

Swing is a standard MVC structure. Componentui represents view, which is used to draw components. Components, especially the model layer, such as the document of jtextfield, The tablemodel of jtable, And the treemodel of jtree.

Control may not be obvious. We may simply regard its event mechanism as a controller developed by the swing team to developers.

As a Java developer, to understand the structure of MVC, learning swing is indeed a good choice.

3). Net (ASP. NET, VB. NET, C #. net)

ASP. NET is not well defined for views and controllers, while models are left for developers to design.

ASP. net does not require a model strictly. developers can choose to create a model class on their own. However, many people choose to abandon this step and put event processing in the Controller to process any computing and data storage. However, using models to include business logic and data access is achievable.

4) Qt Design

QT is a cross-platform C ++ application development framework. It is widely used to develop GUI programs. In this case, it is also called the component toolbox and can be used to develop non-Gui programs, for example, console tools and servers.

The QT design development interface is similar to ASP. NET. The view and controller are isolated, and the model is designed by the designer.

After years of development, QT not only has a complete C ++ graphics library, in addition, the versions in recent years have gradually integrated databases, OpenGL libraries, multi-media libraries, networks, script libraries, XML libraries, WebKit libraries, and so on, its core library is also added to inter-process communication and multithreading modules, which greatly enriches the ability of QT to develop large-scale and complex cross-platform applications, and truly realizes its R & D purpose "code less; create more; deploy anywhere."

In addition, scripts such as Python, Perl, Ruby, and JavaScript also have their own MVC Architecture implementations. For more information, see MVC.

Reference recommendations:

MVC Architecture

MVC (Wiki)

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.