【问题标题】:How does the model update the view in MVC pattern?模型如何更新 MVC 模式中的视图?
【发布时间】:2013-01-21 02:57:36
【问题描述】:

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

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

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

谢谢

【问题讨论】:

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


    【解决方案1】:

    MVC 有多种风格。听起来您可能一直在阅读有关 supervising controller pattern 的内容,视图在其中观察模型的更改。

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

    无论如何,因为我认为在 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();
       }
    
    }
    ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-14
      • 1970-01-01
      • 1970-01-01
      • 2019-02-16
      • 2013-08-26
      • 2010-11-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多