2

I am building a java desktop application where I am attempting to implement MVC. The GUI interface has multiple views (think pop-up windows) from the main view. Each view has its associated model that it receives updates from.

I am running into scenarios where two views need to access a particular data field from one model. This requires that one model "knows about" the another. What are my options to solve this?

The data field is a table that is populated in one of the views and is then subsequently accessed in the main splash view.

I have tried passing the second model into the first's constructor from the controller, however I feel this is sloppy. Should a model listen to another model (Observer pattern)? Perhaps my question is too subjective....

3
  • you should only need one model. that's one of the main ideas about mvc - a single model can have more than one view.
    – Ray Tayek
    Commented Jan 29, 2013 at 23:03
  • What is wrong with constructing one model using the data from another? Does not sound sloppy to me. As long as the view for the main window is updated with the new data it sounds like you are following the MVC pattern just fine. Commented Jan 30, 2013 at 0:35
  • Having thought about it, what you are concerned about can be fixed by the factory pattern or something similar. Instead of using the constructor of the main model, have a class dedicated to building the main model that can take an instance of the other model as a parameter. That will decouple your models from each other. Commented Jan 30, 2013 at 0:44

1 Answer 1

1

Single model and multiple views - I see no problem in your case. Have you tried to implement observer pattern? If not, try to write your classes according to class structure suggested by observer pattern. I think it will solve problem.

You can visit here to find more information about observer pattern.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.