English 中文(简体)
监查系统如何运作?
原标题:How does an MVC system work?

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

所以我猜它最纯净的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.

<强 > 执行

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

替代代码 :

/* 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. Is that right?
  2. I can t see why the View cannot change the Model directly, but through Controller.
  3. 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? More: 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?

谢谢

最佳回答
  • 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.

模型不仅仅是数据。 模型也是商业逻辑。 它包含系统的所有智能, 或至少抽象的幕后情报( 如数据库电话或其他服务电话 ) 。 请考虑一句 : “ 保持模型的重量和控制器的亮度 ” 。

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

《模式》不知情,正确无误,《模式》应在应用程序之间可移植,不应以任何方式依赖对用户界面的关切。 (《观点》和主计长在本案中是用户界面的关切。 )

视图了解模型, 也正确 。 视图基本上是模型的“ 组合” 。 它展示了所有 UI 元素, 并相应将模型数据放入 UI 元素中 。

主计长 kind of " knows the View." 它知道它应该对哪一种视图进行控制, 但是它不知道该视图的哪个视图。 它也不知道该视图来自哪个视图。 主计长对事件做出回应。 一个事件来自UI, 带有某种状态信息( 查看模式, 也许可以), 通过模型( 业务逻辑发生的地方) 指导逻辑控制, 并以模型( 或查看模式, 如果特定视图中的数据形状不同于模型) 和视图回应 。

我不明白为什么《观察》不能直接改变模型,而是通过主计长改变模型。

视图可以在用户互动的背景下操控模型, 但不应该期望这些变化会以某种方式持续下去。 视图应该被视为“ 客户端 ”, 并不了解任何“ 服务器端 ” 。 ( 即使您在谈论本地应用程序而不是网络应用程序 。 ) 维持任何变化都被视为“ 联合行动” 或“ 事件 ”, 并且会让主计长去完成 。

假设我有一个动作后要播放动画。 谁必须处理这个动画: 模型、 视图或主计长? 还有: 动画逻辑是模型、 视图或主计长的一部分?

动画听起来像一个完全基于 UI 的操作。 它将会在视图中 。 有比 UI 动画更多的发生吗? 动画 change 在后端是否有东西? 例如, 如果我有一个网络应用程序, 当页面加载时, 我想淡入一些完全在视图中的数据( 动画) 。 数据会像任何其他数据一样被传送到视图中, 动画会完全在 UI (View) 内进行 。 从模型或主计长的角度看, 它不会 < em> /em > 任何东西 。

假想一个扑克游戏。 在用户选择一个动作( 例如, Raise ) 后, 系统必须播放动画( 比如, 芯片从玩家点跳到桌面 ) 。 我如何将这个扑克例子( 带有动画) 视为 MVC? 您能否解释并给出一个假代码?

动作 (“ Rise ”) 是一个控制器事件。 UI 会联系控制器进行“ 募捐 ” 。 因此, 控制器可能有这样的方法 :

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.
}

当主计长用新视图对 UI 回复时, 该视图将包含任何要向用户显示的动画 。 ( 毕竟, 除非动作成功, 否则您不想执行动画 。 当主计长用新视图回答 UI 时, 动画会播放显示成功动作的新视图 。 相反, 该视图会用显示错误的视图对 UI 做出回应 。 )

问题回答

我会用简单的银行类比。

  • Tellers are Views.
  • Runners are Controllers.
  • Bankers are Models.

银行家是聪明的, 他们知道所有的商业逻辑, 并做所有复杂的计算。

经营人用来把钱(数据)从银行转移到Tellers。

Teller把钱交给客户

简单说明:

< 强 > 模式

public class BankAccount
{
     public int ID;
     public int Balance;

     public BankAccount(int id)
     {
         ID = id;
         Balance = DetermineAmount();
     }

     public int DetermineAmount()
     {
         // Gather transaction info, debits, credits and return a
         // sum of the amount left in the account depending on the
         // id provided.
     }
}

<强 > 主计长

    public class BankAccountController
    {

         public ViewResult Index(int id)
         {
             BankAccount account = new BankAccount(id);
             return View(account);
         }

    }

<强 > 查看

<ul id="account-info">
   <li>Account ID: `@Model.ID`</li>    
   <li>Balance: `@Model.Balance`</li>
</ul>

如果你对历史 true MVC true MVC > 感兴趣,那么先从>>>Trygve Reenskaug 。他在1970年代后期创建了它(观察?,编目???) 。首先,读到“Models-pvical-currenceers" 。它定义了术语。小心注意它的标题 - < 坚固 > 所有三个角色都被多元化为 。这是大多数人似乎会犯的第一件事。

The best description I ve found of the original use of MVC is actually in a presentation dated 2004 entitled "Inside Smalltalk MVC". I would guess that the canonical papers that describe the Smalltalk 80 final version of MVC are Krasner & Pope s "A Cookbook for Using the Model-View-Controller User Interface Paradigm in the Smalltalk-80" and Steve Burbeck s "Applications Programming in Smalltalk-80: How to use Model-View-Controller (MVC)". Both papers are well worth the read.

如果你有时间去杀人,不介意听罗伯特·马丁的话,他做了一个好节目 的“noreferrer" 关键注点,该注点触及MVC。这是一个小时多一点的时间,但很有趣和启发性。我倾向于遵循他的观点,即大多数执行过程都是错误的 MVC。我花了一点时间,终于找到了一个可以连接到描述MVC的图表。我喜欢的图来自Pope和Krasner

MVC
(source: as3dp.com)

从我的观点来看,以下是关键要点:

  • a model instance is responsible for notifying the interested objects of changes. Note that these can be any object instances. The diagram shows both views and controllers receiving updates here.
  • views are responsible for querying the current state and displaying the results. They usually perform filtering or data transformation as well.
  • controllers are responsible for accepting user input and forwarding view messages along to the view.
    • View messages are a common theme in MVC. It is important that these are independent of the UI world - these are not mouse clicks and what not but a view-specific language of events. This brings us to the next point.
  • The view does not depend on the controller in any way. Controller s are responsible for arranging and creating views and providing the interface between the rest of the world and the view.
  • In a perfect world, the view is responsible for making the model representation visible. This is how it worked when MVC was applied to desktop applications.

现实是, MVC 已经被扭曲了, 为网络世界重写了。 它已经不是真正的 MVC 了, 或者可能是 MVC 被简单重新定义了 。 这就是为什么你看到有这么多 MVC 的不同观点和表达方式。 如果您正在研究写桌面风格应用程序, 那么请看看 Krasner & amp ; Pope 。 如果您正在研究MVC 是如何应用到网络的, 那么我建议 Bob 叔叔的主旨, 选择一个更适合网络应用程序的替代方案 — — 他称之为“ em> Interactor, Intutional, Interactor, Unititutional 。





相关问题
Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

JSON with classes?

Is there a standardized way to store classes in JSON, and then converting them back into classes again from a string? For example, I might have an array of objects of type Questions. I d like to ...

Object-Oriented Perl constructor syntax and named parameters

I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...

Passing another class amongst instances

I was wondering what is the best practice re. passing (another class) amongst two instances of the same class (lets call this Primary ). So, essentially in the constructor for the first, i can ...

Where can I find object-oriented Perl tutorials? [closed]

A Google search yields a number of results - but which ones are the best? The Perl site appears to contain two - perlboot and perltoot. I m reading these now, but what else is out there? Note: I ve ...

热门标签