【问题标题】:How View updates Model?View如何更新Model?
【发布时间】:2020-01-14 15:56:26
【问题描述】:

关注this articleThere are two schools of thought with regard to whether the View interacts directly with the Model or not.

我对 View 不与 Model 交互的情况感兴趣。如果只有 Controller 知道 View 而 View 不知道 Controller,我们可以通过调用 Controller 中的 View 和 Model 方法轻松地使用 Model 数据更新 View(例如写一些文本)。

但是,如果 View 不知道 Controller 或 Model,Controller 和 Model 如何对 View 更改(例如按钮按下)做出反应?

【问题讨论】:

    标签: model-view-controller design-patterns


    【解决方案1】:

    -一个解决方案-

    一种让 2 个对象通过松散耦合进行通信的方法(在您的情况下,视图不知道控制器,反之亦然)它使用 “信使”模式

    “信使”是所有其他人都知道的对象。使用“信使”对象:

    • 您注册对象(您的视图)以发送消息
    • 您注册对象(控制器、模型...)以收听特定消息

    通过这种方式,模型可以对特定视图的事件做出反应,因为它已注册到信使中。

    这里有一个完整的例子(C# 代码): Light messenger pattern

    告诉我这是不是你要找的……

    【讨论】:

      猜你喜欢
      • 2013-11-11
      • 2012-11-01
      • 2012-10-19
      • 2011-12-04
      • 2014-01-10
      • 1970-01-01
      • 2019-05-09
      • 2015-09-23
      • 2012-02-10
      相关资源
      最近更新 更多