Model-View-Controller

MVC(Model-View-Controller)是一种用于计算机用户界面实现的软件架构模式,它将应用程序分为三个相互连接的部分:模型(Model)、视图(View)和控制器(Controller),以实现内部信息表现形式与用户交互方式的分离。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

接着把维基百科上 Model-View-Controller 的介绍抄了一遍。

Model-view-controller(MVC) is a software architectural pattern for implementing user interfaces on computers. It divides a given application into three interconnected parts. This is done to separate internal representations of information from the ways information is presented to, and accepted from, the user. The MVC design pattern decouples these major components allowing for efficient code reuse and parallel development.

Traditionally used for desktop graphical user interfaces (GUIs), this architecture has become popular for designing web applications and even mobile, desktop and other clients. Popular programming languages like Java, C#, Ruby, PHP and others have pupular MVC frameworks that are currently being used in web application development straight out of the box.

这里写图片描述


Description

As with other software architecturs, MVC expresses the “core of the solution” to a problem while allowing it to be adapted for each system. Particular MVC architectures can vary significantly from the traditional description here.

Components

  • The model is the central component of the pattern. It expresses the application’s behavior in terms of the problem domain, independent of the user interface. It directly manages the data, logic and rules of the application.
  • A view can be any output representation of information, such as a chart or a diagram. Multiple views of the same information are possible, such as a bar chart for management and a tabular view for accountants.
  • The third part, the controller, accepts input and converts it to commands for the model or view.

Interactions

In addition to dividing the application into three kinds of components, the model-view-controller design defines the interactions between them.

  • A model stores data that is retrieved according to commands from the controller and displayed in the view.
  • A view generates new output to the user based on changes in the model.
  • A controller can send commands to the model to update the model’s state (e.g., editing a document). It can also send commands to its associated view to change the view’s presentation of the model (e.g., scrolling through a document, movement of document)

注:FlaskApp代码中,Model对应着MySQL, View对应着HTML文件,Controller对应着flaskapp.py文件。


Use in web application

Although originally developed for desktop computing, MVC has been widely adopted as an architecture for the World Wide Web applications in major programming languages. Several web frameworks have been created that enforce the pattern. These software frameworks vary in their interpretations, mainly in the way that the MVC responsibilities are divided between the client and server.

Some web MVC frameworks take a thin client approach that places almost the entire model, view and controller logic on the server. This is reflected in frameworks such as Django, Rails and ASP.NET MVC. In this approach, the client sends either hyperlink requests or form submissions to the controller and then receives a complete and updata web page (or other document) from the view; the model exists entirely on the server. Other frameworks such as AngularJs, EmberJs, JavaScriptMVC and Backbone allow the MVC components to execute partly on the client.


Goals of MVC

Simutaneous development

Because MVC decouples the various components of an application, developers are able to work in parallel on different components without impacting or blocking one another. For example, a team might divide their developers between the front-end and the back-end. The back-end developers can design the structure of the data and how the user interacts with it without requiring the user interface to be completed. Conversely, the front-end developers are able to design and test the layout of the application prior to the data structure being available.

Code reuse

By creating component that are independent of one another, developers are able to reuse components quickly and easily in other applications. The same (or similar) view for one application can be refactored for another application with different data because the view is simply handling how the data is being displayed to the user.


Advantages & disadvantages

Advantages

  • Simultaneous development - Multiple developers can work simultaneously on the model, controller and views.
  • High cohesion - MVC enables logical grouping of related actions on a controller together. The views for a specific model are also grouped together.
  • Low coupling - The very nature of the MVC framework is such that there is low coupling among models, views or controllers.
  • Ease of modification - Because of the separation of responsibilities, future development or modification is easier .
  • Multiple views for a model - Models can have multiple views

Disadvantages

  • Code navigability - The framework navigation can be complex because it introduces new layers of abstraction and requires users to adapt to the decomposition ctiteria of MVC.
  • Multi-artifact consistency - Decomposing a feature into three artifacts causes scattering. Thus, requiring developers to maintain the consistency of multiple representations at once.
  • Pronounced learning curve - Knowledge on multiple technologies becomes the norm. Developers using MVC need to be skilled in multiple technologies.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值