model-view-controller - MVC 系统如何工作?

标签 model-view-controller oop design-patterns

我正在尝试学习MVC模式,但是每个地方都有不同的说法。所以现在我不知道什么是真正的MVC。

所以我猜它是最纯粹的 MVC:

  • 模型只是数据并通知数据更改。
  • View读取Model的消息来更新 View 。
  • Controller View 读取用户输入并据此更改模型。

实现

  • 模特谁都不认识。
  • View 知道模型
  • Controller 知道 View 模型

伪代码:

/* Model */
class Color{ 
  color = blue;
  setColor(color);
  notifyUpdate();
}
/* View */
class ColorPicker(model){
  model.register(update);
  update(){
    this.colorToExhibit = model.color;
  }
}
/* Controller */
class Colorize(view, model){
  view.register(update);
  update(color){
    model.setColor(color);
  }
}

一些问题:

  1. 是吗?
  2. 我不明白为什么 View 不能直接更改模型,而是通过 Controller 。
  3. 假设我要在某个 Action 后执行动画。谁必须处理这个动画:模型、 View 还是 Controller ?另外:动画逻辑是模型、 View 还是 Controller 的一部分?更多:假设一场扑克游戏。用户选择一个 Action (例如,“加注”)后,系统必须播放动画(例如,筹码从玩家位置移动到 table )。如何将这个扑克示例(带动画)视为 MVC?你能解释一下并给出一个伪代码吗?

谢谢。

最佳答案

  • Model is just data and notify data changes.
  • View reads the messages of the Model to update the view.
  • Controller reads the user input from View and changes the Model according.

模型不仅仅是数据。模型也是业务逻辑。它包含系统的所有智能,或者至少是幕后智能的抽象(例如数据库调用或其他服务调用)。考虑一下这句话:“让你的模型重,让 Controller 轻。”

  • Model knows no one.
  • View knows the Model.
  • Controller knows both View and Model.

模型不知道任何人都知道它是正确的。该模型应该可以在应用程序之间移植,并且不应该以任何方式依赖于 UI 问题。 (在本例中, View 和 Controller 是 UI 问题。)

View 知道模型,也是正确的。 View 基本上“绑定(bind)”到模型。它呈现所有 UI 元素,并相应地将模型数据放置在 UI 元素中。

Controller 有点“了解 View ”。它知道应该直接控制哪个 View ,但它不知道有关该 View 的任何信息。它也不知道先前来自哪个控件的哪个 View 。 Controller 响应事件。事件来自 UI,携带某种状态信息(可能是 ViewModel),通过模型(业务逻辑发生的地方)指导逻辑控制,并用模型(或 ViewModel,如果形状特定于特定 View 的数据与模型和 View 不同。

I can't see why the View cannot change the Model directly, but through Controller.

View 可以在用户交互的上下文中操纵模型,但不应期望这些更改以任何方式持续存在。 View 应该被视为“客户端”,并且不知道任何“服务器端”。 (即使您谈论的是 native 应用程序而不是 Web 应用程序。)保留任何更改都被视为 UI“操作”或“事件”,并且将转到 Controller 以使其发生。

Suppose I have animations to be performed after an action. Who must handle this animation: the Model, the View, or the Controller? Also: the animation logic is part of the Model, View, or Controller?

动画听起来像是完全基于 UI 的操作。它将在 View 内。除了 UI 动画之外,还有更多的事情发生吗?动画是否改变后端的任何内容?例如,如果我有一个 Web 应用程序,并且当页面加载时,我想要淡入一些数据(动画)......这完全在 View 中。数据将像任何其他数据一样传递到 View ,并且动画完全发生在 UI( View )内。从模型或 Controller 的角度来看,它不执行任何操作。

Suppose a Poker game. After the user choose an action (say, 'Raise'), the system must play an animation (say, the chips going from player spot to the desk). How can I see this poker example (with animation) as a MVC? Can you explain and give a pseudocode about that?

Action (“Raise”)是一个 Controller 事件。 UI 将联系 Controller 来执行“加注”。所以 Controller 可能有这样的方法:

View Raise(GameState state)
{
    // Interact with the Models to update the known state of the game.
    // The Models would perform the actual Poker game logic.
    // Respond with a View bound to updated Models.
}

一旦 Controller 使用新 View 响应 UI,该 View 将包含要向用户显示的任何动画。 (毕竟,除非操作成功,否则您不想执行动画,对吗?当 Controller 使用指示操作成功的新 View 响应 UI 时,就会播放动画。它可能会响应 UI View 指示错误,在这种情况下, View 会显示其他内容。)

关于model-view-controller - MVC 系统如何工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10774263/

相关文章:

c# - 从代码访问 View 模型所需的错误消息数据注释属性 - MVC、Razor

java - 调用两个不同类的相同方法

c++ - 如何通过引用返回对象

java - 更好的模式观察者?

design-patterns - 闭包设计模式

java - 如何向 null Set<class> 添加元素?

asp.net-mvc - 仅显示模型中的某些值

ios - 如何在 Swift 中实现 MVC?

c# - 在高负载系统 C# 中映射 POCO 的成本

java - Java KeyGenerator 使用单例模式吗?