Hands-On Enterprise Application Development with Python
上QQ阅读APP看书,第一时间看更新

View

The view is responsible for presenting the data to the user, or presenting an interface to the user through which they can manipulate the data stored in the model. The view in MVC is usually dynamic and changes frequently based on the changes that happen in the model. A view can also be considered to contain just the presentation logic for the application, without having consideration for how the data will be stored by the application and how it will be retrieved. Often, a view can be used to cache the presentation state to accelerate the display of the data.

So, in brief, here are the functions performed by the view:

  • Provide the presentation logic for the application to display the data stored in the application
  • Provide the user with an interface to modify the model
  • Optionally provide an ability to cache the view state to accelerate the display of the interfaces

As we can see, in the MVC model, every single component performs its own set of functions and does not duplicate the functionality. This translates into the application, with reduced coupling on a macro scale. Such an implementation makes the task of making the changes in the application less daunting, since a team dealing with the presentation of the data does not need to worry about making changes to the way the application stores and retrieves the data from the underlying storage, and a team dealing with the functionality of how the application deals with the data does not have to worry about how the data will be presented to the user.

The clear separation of interfaces also allows for a way to improve the testing process of the application, where tests can be clearly written to target the individual components.

So, let's take a look at some of the advantages of using the MVC pattern in application development:

  • It provides clear separation of the presentation logic and the business logic
  • It improves code reuse when multiple interfaces need to deal with the same backend model
  • It provides improved ability for unit testing, due to its decoupled user interface and data model
  • It provides improved development workflow, since changes to the view do not affect the model, or the other way around

The MVC pattern finds quite a lot of use in the development of web applications, as well as applications that provide a GUI, where the separation of the UI logic from the data-handling logic can prove to be of great help. Python web frameworks, such as Django, provide a working example of an MVC pattern, where their application structure enforces the use of an MVC pattern by clearly creating boundaries between where the business logic lives and where the presentation logic lives.