model-view-controller - 模型如何更新 MVC 模式中的 View ?

标签 model-view-controller view model observer-pattern

我对 MVC 模式的结构感到困惑。

在谷歌搜索的某些地方,我发现该模型会更新所有订阅该模型的 View 。模型如何更新 MVC 模式中的 View ?

谁能通过举个例子给我一个简单而清晰的想法?

谢谢

最佳答案

MVC 有多种风格。听起来您可能一直在阅读 supervising controller pattern View 在其中观察模型的变化。

我从你过去的问题和答案中看到你喜欢 php。我不确定监督演示者在 php 中的常见程度(我当然从未使用过它,但很想知道其他人是否这样做)。在 .Net 应用程序(例如 winforms)中很常见,其中模型可以数据绑定(bind)到 UI 控件。通过订阅模型事件, View 会收到模型更改的通知。

无论如何,因为我认为在 php 中尝试这会很有趣,所以我整理了一个示例:

<?php

$input = array(2, 3, 4, 5, 6, 7, 8, 9, 10);

$model = new model(1);
$controller = new controller(
              $model, new view($model, 0), new view($model, 2)
              );

$controller->doAction($input);

class model {
  //the model changed event
  public $modelChangedEvent = array();
  private $val;

  public function __construct($val) {
     $this->val = $val;
  }

  public function setVal($val) {
     $this->val = $val;
     //raise the model changed event because the model state has changed
     $this->raiseModelChangedEvent();
  }

  public function getSquaredVal() {
     return pow($this->val, 2);
  }

  private function raiseModelChangedEvent() {
     foreach ($this->modelChangedEvent as $handler)
        call_user_func($handler);
  }

}

class view {

  private $model;
  private $decimalPlaces;
  private $valueHistory = array();

  public function __construct($model, $decimalPlaces) {
    $this->model = $model;
    $this->valueHistory[] = $model->getSquaredVal();
    $this->decimalPlaces = $decimalPlaces;
    //listen to the model changed event and call handler
    $this->model->modelChangedEvent[] = array(
                             $this,
                             'modelChangedEventHandler'
                              );
  }

  public function showView() {
    $formatted = array_map(
                 array($this, 'getFormattedValue'), $this->valueHistory
                 );
    echo implode('<br/>', $formatted), '<br/><br/>';
  }

  public function modelChangedEventHandler() {
     $this->valueHistory[] = $this->model->getSquaredVal();
  }

  private function getFormattedValue($val) {
     return number_format($val, $this->decimalPlaces);
  }

}

class controller {

   private $model;
   private $view1;
   private $view2;

   public function __construct($model, $view1, $view2) {
     $this->model = $model;
     $this->view1 = $view1;
     $this->view2 = $view2;
   }

   public function doAction($input) {
     foreach ($input as $val) $this->model->setVal($val);
     $this->view1->showView();
     $this->view2->showView();
   }

}
?>

关于model-view-controller - 模型如何更新 MVC 模式中的 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14720127/

相关文章:

android - 如何将对象返回到表面 View android

mysql - 嵌套集模型优化

ruby-on-rails - Rails - 在没有 url helper 的情况下在模型的虚拟属性中构建绝对 url

PHP MVC - 模型需要从另一个模型访问数据

objective-c - 与 Objective C 中的 MVC 混淆

c# - ASP.NET MVC 获取具有特定属性的数据库条目列表

backbone.js - Backbone ( Marionette )编辑 View

Java - Swing - MVC/观察者模式 - 如何构造这两个类以遵守 MVC 模式?

c# - ASP.NET MVC - 使用 C# 将引用不同程序集的模型从 Controller 传递到 View

Json 到 Gson - 建模