How does the model update the view in MVC pattern? -



How does the model update the view in MVC pattern? -

i have confusion construction of mvc pattern.

in places while searching on google found model updates views subscribed model. how model updates view in mvc pattern?

can give me simple , clear thought how happen giving example?

thanks

mvc comes in variety of flavours. sounds may have been reading supervising controller pattern in view observes on changes model.

i see past questions , answers php. i'm not sure how mutual supervising presenter in php (i've never used it, interested know if others do). mutual in .net applications (eg winforms) model can databound ui control. view notified of changes model subscribing model events.

anyhow, because thought fun seek in php set example:

<?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 model changed event because model state has changed $this->raisemodelchangedevent(); } public function getsquaredval() { homecoming pow($this->val, 2); } private function raisemodelchangedevent() { foreach ($this->modelchangedevent $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 model changed event , phone 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) { homecoming 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 $val) $this->model->setval($val); $this->view1->showview(); $this->view2->showview(); } } ?>

model-view-controller view model observer-pattern

Comments

Popular posts from this blog

web services - java.lang.NoClassDefFoundError: Could not initialize class net.sf.cglib.proxy.Enhancer -

Accessing MATLAB's unicode strings from C -

javascript - mongodb won't find my schema method in nested container -