gpt4 book ai didi

model-view-controller - MVC : why do we need "controller", 或者我们什么时候应该使用这个模式?

转载 作者:行者123 更新时间:2023-12-04 08:37:55 26 4
gpt4 key购买 nike

我已经阅读了很多关于 MVC 的出版物,但我仍然无法清楚地理解为什么我们需要“ Controller ”。

我通常在客户端-服务器模型中编写应用程序:

服务器包含所有业务逻辑,它对 gui 一无所知。它完成主要工作,并且尽可能便携。

客户端是一个 GUI,它绑定(bind)到服务器,与用户交互,将命令从用户发送到服务器。

我喜欢这种架构,但我不明白为什么人们真的需要在客户端和服务器之间再增加一种媒介,它似乎是 Controller ?

更新:简单示例:假设我们需要编写一些数据记录器。数据来自 COM 端口,通过某种协议(protocol)进行编码。需要在一个简单的日志窗口中显示收到的消息。

我将如何做到:

服务器包含以下项目:

  • Data_receiver : 实际上是从 COM 端口接收原始数据,但它是接口(interface),所以我们可以创建另一个类来接收来自任何其他来源的数据;
  • Data_decoder :获取原始数据并返回解码后的消息,它也是接口(interface),因此我们可以轻松更改编码协议(protocol);
  • Data_core : 使用 Data_receiver 的实例和 Data_decoder , 向客户端发出信号。

  • 客户端包含以下项目:
  • 应用程序核心:创建 Data_receiver 的实例(连接COM口的那个),Data_decoderData_core (引用 Data_receiverData_decoder 实例),还创建 GUI 简单日志窗口(引用 Data_core );
  • GUI 简单日志窗口:绑定(bind)到 Data_core ,即监听它发出的信号,并显示接收到的数据。

  • 正如我所了解的有关 MVC 的内容,GUI 实际上不应接收来自 Data_core 的消息。 ,因为 Controller 应该这样做,然后将数据传递给 GUI。但是,如果 GUI 直接从模型中获取这些数据,会发生什么坏事呢?

    最佳答案

    过去,我多次问过自己同样的问题,最近我一直在阅读有关 JSP 模型 2 架构的信息,维基百科条目说明了以下内容。

    The literature on Web-tier technology in the J2EE platform frequently uses the terms "Model 1" and "Model 2" without explanation. This terminology stems from early drafts of the JSP specification, which described two basic usage patterns for JSP pages. While the terms have disappeared from the specification document, they remain in common use. Model 1 and Model 2 simply refer to the absence or presence (respectively) of a controller servlet that dispatches requests from the client tier and selects views.



    这基本上意味着 MVC 模式本身存在变化,因此您始终可以根据您的项目应用 MVC 或 MV 模式。然而,一个合适的 MVC 架构确实应该有一个 Controller ,因为模型和 View 不应该直接相互通信。

    关于model-view-controller - MVC : why do we need "controller", 或者我们什么时候应该使用这个模式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13671814/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com