0

I have almost completely understood the MVC pattern, but there is one thing that still confuses me:

Let's say - for the purpose of this example - I have got a controller, a model and 5 views. Whenever the model changes, I would like to (optimally) change a (proper) subset of these 5 views as a result of changes in the model.

What would be the right way to do it ?

My understanding of MVC as of now is that the model should not know anything about either controller or view. Does that apply to the view as well ? I.e. does the view also not know about controller AND model ?

Based on the assumption above, the proposal for my MVC program with the given requirements would structurally look something like this:

Pseudocode:

OPTION 1

MODEL

//Random data, no references to either controller or view

notifyObserver(){

    observer.modelChanged();    

}

CONTROLLER

reference MODEL
reference VIEW

var model;

var view1;
var view2;
var view3;
var view4;
var view5;


createController(m,v1,...,v5){
    model = m;
    view1 = v1;
    .
    .
    .
    view5 = v5;

}

modelChanged(){
    view1.updateView(model.someVariable);
    view4.updateView(model.someVariable);
} 

VIEW

//no references to either model or controller

//some visual elements

updateView(var someVariable){

    myVisualElement.change(someVariable);

}

TESTCLASS

main(){

   create view1;
   create view2;
   .
   . 
   . 
   create view5;

   create model;
   create controller(model,view1,...view5);
   model add observer(controller);

}

So what I am basically doing with this, is letting the controller know about everything, while both view and model act completely independently. In this case, the CONTROLLER is the only one who gets notified when something changes in the model. It will then go ahead and change its views accordingly.

OPTION 2

So the VIEW would basically have a property/variable called model, that I will pass in with the constructor like this:

TESTCLASS

 main(){

   create model;

   create view1(model);
   create view2(model);
   .
   . 
   . 
   create view5(model);

   create controller(model);
   model add observer(view1);
   . 
   . 
   .
   model add observer(view5);

}

This way, all the views have references to the model and are notified by the model as soon as something changes. I am confused because I am not sure which is the right approach... I hope you can help me understand this once and for all.

the_critic
  • 12,720
  • 19
  • 67
  • 115
  • possible duplicate of [What are MVP and MVC and what is the difference?](http://stackoverflow.com/questions/2056/what-are-mvp-and-mvc-and-what-is-the-difference) – Agat Nov 09 '13 at 20:22

1 Answers1

0

That's hard to catch what's the best solution for you and what you are actually asking for.

Which technology do you use, as dependently on a platform, there might be some specific nuances on MVC? MVC basically don't have anything like observer.modelChanged and if you do use it, that adds certain limitations/possiblities on the inplementation details.

For instance, if you use mobile applications, you would rather have that MVC transformed to MvvM (Model-View-ViewModel) (where ViewModel would act rather as a Controller) and in that case the "Controller" would know only about Model, and View would know only about Controller (representing its Model partially).

If you use web application ((ASP.NET) MVC), you would have a Controller, which passes Models to Views (without knowing how actually the View works). Views would also know Model and adapt itself to it, but also getting in touch with Controller. At the same time, Model would not know about anyone of mentioned.

Agat
  • 4,577
  • 2
  • 34
  • 62
  • Well I just want to know if it is generally acceptable and common to obscure data between Model and View and let the Controller be the mediator. In iOS it is quite common to do it that way (at least they claim so in iTunes U iPhone and Ipad Development Course), so that is why I'm asking... – the_critic Nov 03 '13 at 21:05
  • Well, 'controller as a mediator' is a basic idea of MVC concept. I am not really following what makes you uncomfortable in this situation? – Agat Nov 04 '13 at 00:30
  • I feel like the view should not know anything about the Model, therefore the view should not have any reference to the view nor would I like it to be an observer thereof. Is that a wrong feeling that I have ? – the_critic Nov 04 '13 at 00:35
  • I am not sure if you've reviewed this answer: http://stackoverflow.com/a/101561/691660, but it's really pretty expanative what and when any part should do. – Agat Nov 04 '13 at 00:47