Python design pattern: Using Pattern to change software design (1)

Source: Internet
Author: User

In the field of software design, each design pattern is systematically named, interpreted, and evaluated as an important and reusable design in an object-oriented system. In this way, as long as we understand these design patterns, we can fully or to a large extent absorb the valuable experience in the patterns, so as to have a more comprehensive understanding of the software architecture.

BKJIA: Concepts, forms, and applications of Python closures

More importantly, these models can be directly used to guide the important object modeling problems in the design of object-oriented systems. In actual work, once the background is met, you only need to simply apply these models, thus saving a lot of exploration work.

Classic Design Pattern MVC

During the long-term software practice, some practical design patterns were gradually summarized and applied to specific software systems, which solved many design problems. The Model-View-Controller, MVC Model, which is widely used in Java and originated from Smalltalk, is a very classic design pattern, you can better understand the concept of "mode.

The MVC mode is usually used to develop human-computer interaction software. The biggest feature of this type of software is that the user interface is easy to change. For example, when you want to extend the functions of an application, you need to modify the menu to reflect this change. If the user interface and core functions are closely intertwined, it is usually very difficult to establish such a flexible system, because it is easy to produce errors. To better develop such a software system, system designers must consider the following two factors:

◆ The user interface should be easy to change and may be changed even during running;

◆ Modifying or porting the user interface does not affect the core functional code of the software.

To solve this problem, we can use the idea of separating Model), View) and Controller. In this design mode, the model is used to encapsulate core data and functions. It is independent of specific output representations and input behaviors and is the code for executing certain tasks, it is not a model concern that these tasks are displayed to users. The model only has pure functional interfaces, that is, a series of public methods. Some of these methods are value methods, so that other parts of the system can obtain the internal state of the model, while others are value-setting methods, allows other parts of the system to modify the internal status of the model.

A view is used to display information to users. It obtains data from the model and determines the mode in which the model is presented to users. The same model can correspond to multiple views. In this way, the model is reusable code. In general, the model must retain information about all corresponding views so that all views can be updated when the model status changes.

The controller is used together with a view. It captures mouse movement, mouse clicks, keyboard input, and other events, converts them into service requests, and then transmits them to the model or view. The user of the entire software interacts with the system through the Controller. He uses the Controller to manipulate the model, transmit data to the model, change the model status, and finally update the view.

The MVC design pattern separates the model, view, and controller from each other, which can change a sub-system of the software without having an important impact on other subsystems. For example, when you change a non-graphical user interface software to a graphical user interface software, you do not need to modify the model, but add a new input device support, this usually does not affect the view. The basic implementation process of a software system that uses the MVC design pattern is:

1. The controller creates a model;

2. The controller creates one or more views and associates them with the model;

3. The Controller is responsible for changing the model status;

4. When the model status changes, the model will notify the view associated with the change.

If you use UML to represent the MVC design pattern, 1 is shown:


Related Article

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.