MVC design mode

xiaoxiao2021-03-06  102

MVC design mode

Author: Lin Shanmao (CCIDNet)

Foreword

User interface, especially graphical user interface, assumes a role in displaying a problem model to a user and an operation of operating and I / O interaction with a user. The user wants to keep the interaction interface relative stability, but more want to change and adjust the displayed content and form as needed. For example, it is required to support different interface standards or different display effects, adapt to different operational requirements. This requires the interface structure to support the adjustment of the interface to the interface without changing the functionality and model of the software.

To do this, from the perspective of the interface, it is difficult to: how to make the software calculation model independently of the interface configuration while satisfying the interface requirements. Model - View - Control (MVC: Model-View-Controller) is a structural organization model of such an interactive interface.

2 MVC (Model-View-Control)

The MVC is proposed by Trygve Reenskaug, first applied in a SmallTalk-80 environment, making many interactions and interface systems, and Microsoft's MFC base classes also follow MVC's idea.

For interface design variability, MVC is divided into models, views, and control three components of the interactive system.

The model component is the problem logic handled by the software in an inner abstraction independent of the external display content and form, and the computational relationship of the core data, logic, and functionality of the problem is encapsulated. He is independent of the specific interface expression and I / O operation.

View components show the information and specific forms of model data and logical relationships and status to the user. It obtains display information from the model, with multiple different display forms or views for the same information.

The control component is to process the interaction of the user and the software. The responsibility is to control any changes in the model to ensure that the user interface is related to the corresponding connection between the model; it accepts the user's input, input feedback to the model, and then implement the model The calculation control is a component that coordinates the model and view. Typically a view has a controller.

The model, the view is separated from the controller, so that a model can have multiple display elements. If the user changes the model's data through the controller of a view, all other views depending on these data should be reflected in these changes. Therefore, whenever data changes happen, the controller will notify all the views and cause the displayed updates. This is actually a change in model - communication mechanism.

2.1 Models, views and control classes in MVC

Models, views, and control classes in MVC are shown in Figure 1.

(1) The model contains the core data, logical relationships, and computing functions of the application problem. It encapsulates the required data, providing the operation process of completing the problem processing. The controller calls these operations based on the need for I / O. The model also provides a view to the view to provide an operation of accessing its data.

This change-propagation mechanism reflects the registration relationship between each interdependent component. Model data and state changes will stimulate this change-propagation mechanism, which is a bond that is contacted between models, views, and controllers.

(2) The view transfers the information to the user in the form of display. Different views express the data and status information of the model by different displays. Each view has an update operation that can be changed - the propagation mechanism is activated. When the update is called, the view obtains the data value from the model and uses them to update the display.

At initialization, all views and models are established through the registration relationship with the change-propagation mechanism. A one-to-one relationship is maintained between the view and the controller, and each view creates a corresponding controller. The view is provided to the controller to process the displayed operation. Therefore, the controller can get the ability to actively excit the interface update.

(3) The controller accepts the user's input by time triggering. How does the controller get an event dependent on an interface running platform. The controller processes the input event through the event processing process and provides the corresponding operational service for each input event, transforming the event into an excitation operation of the model or related view. If the behavior of the controller relies on the state of the model, the controller should register in the change-propagation mechanism and provide an update operation. In this way, the behavior of the controller can be changed by a change in the model, such as disabling some operations.

3 MVC implementation

Implementing MVC-based applications need to complete the following work, as shown in Figure 2:

3.1 Analyze the application problem, separate the system

Analyze the application problem, separating the system's kernel function, and the control input of the function, the output behavior of the system is three major parts. The design model component makes it packaged in kernel data and computing functions, providing an operation of accessing display data, providing an operation of controlling internal behavior, and other necessary operational interfaces. The data constitution and calculation relationship of the model class is formed. This part is closely related to the specific application problem.

3.2 Design and implement each view

Design of each view form, it got data from the model, display them on the screen.

3.3 Design and implement each controller

For each view, specify the response time and behavior of the user operation. Under the influence of model status, the controller accepts and interprets these events using specific methods. The initialization of the controller establishes the connection with the model and the view, and start the event processing mechanism. The specific implementation method of the event processing mechanism relies on the interface of the interface.

3.4 Controller with installable and uninstalled

The controller can be installed and uninstable, bringing higher degrees of freedom and helps to form a highly flexibility. The separation of the controller and the view supports the flexibility combining the view and the different controller to achieve a different mode of operation, such as a read-only view to ordinary users, professional users, or not using the controller. This separation also provides a way for integrating new I / O devices in applications.

4 MVC changes

Separate models, views, and controllers, making design and use great flexibility. However, in reality, the functions of the views and controllers are usually closely linked. The input event for controlling the view is usually related to the composition of the view. In a real-world design environment, interface operations and their processing are closely associated with interface forms. In this case, the view and controller are separated, and it is inconvenient to the analysis and design, and the operation is low.

Therefore, the view and controller can be combined to design and implement. In the above implementation, you can generate a new view class as long as the views and controllers are combined. Thus, it is still maintained with the separation of the model, so the same model can still use multiple views. These views themselves have an event handling capacity and can still be controlled by the model.

5 MVC's advantages and deficiency

5.1 Advantages of MVC

The advantages of MVC are in the following aspects:

(1) Multiple views can be established and used for one model at the same time. The change-propagation mechanism ensures that all related views get the model data change, so that all associated views and controllers make behavioral synchronization.

(2) Connectivity of the view and the controller, allows replacement views and controller objects, and can be replaced or changed during operation based on demand dynamics.

(3) Portability of the model. Because the model is independent of the view, you can transplant a model to a new platform work. It is only necessary to make new modifications on the new platform.

(4) Potential frame structure. The application framework can be established based on this model, not just in the design interface.

5.2 MVC deficiency

The shortcomings of MVC are in the following aspects:

(1) Increase the complexity of system structure and implementation. For simple interfaces, strictly follow the MVC, separate the model, view and controller, increase the complexity of the structure, and may generate too much update operation, reduce operational efficiency. (2) The view is too close to the controller. The view and the controller are separated from each other, but it is indeed closely close components, and the view does not have the existence of the controller. Its application is very limited, and vice versa, this hampering their independent reuse.

(3) View the low efficiency accesses of model data. According to the different model operating interface, the view may need to call multiple calls to get enough display data. Unnecessary frequent access to untrurified data will also harm the operating performance.

(4) Currently, generally advanced interface tools or constructors do not support MVC mode. The cost of transforming these tools to accommodate MVC needs and the establishment of the part of the component is high, resulting in difficulties in using MVC.

6 Other similar patterns

Similar structural modes include PAC (Presentation-Abstract-Control), Forward-Receiver, Publisher-Subscriber, various visualized user interface controls.

Among them, "Representation - Abstract - Control" Structure Mode (PAC) is also proposed in processing from the data model and its relationship. Among them, it indicates that the view corresponds to the view, the abstraction, the model corresponds to the control. From logic in nature, there is no difference between the two. However, MVC and PAC still have different places.

(1) The control of the MVC is more focusing on the I / O processing of the user on the view, and the control of the PAC mainly refers to the transfer and coordination of abstraction.

(2) In addition, the PAC divides the system into collaboration but loosely coupled intelligents, and MVC is dedicated to the interactive interface, and the associations between the components are more closely related.

(3) In addition, from the architecture, the PAC is a system level because it solves the problem more inclined to collaboration and association between the system and components.

7 small knot

Compared with the inner model of the problem handled by the software, the user interface is required to change frequently. The MVC design pattern can be used to make the software calculation model independently of the interface. This paper first introduces three components of MVC (model components, view components, and control components), as well as work based on MVC-based applications; then analyzes the advantages and deficiencies of MVC; finally, Several other similar structural modes and compare MVC and PACs.

8-reference book

10,000 completed, Lu Ren, "Software Architecture Principle, Composition and Application", Science Press, 2002

2 Qi Zhichang, Tan Qingping, Ninghong compiled "Software Engineering" Beijing: Higher Education Publishing House 1997

3 (US) Roger S.Pressman, Caucasi, Mei Hong translation "Software Engineering - Practitioner Research Method (Fourth Edition)" Beijing: Machinery Industry Publishing House 1999

转载请注明原文地址:https://www.9cbs.com/read-103320.html

New Post(0)