Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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....

share|improve this question
    
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 Jan 29 '13 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. –  mortalapeman Jan 30 '13 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. –  mortalapeman Jan 30 '13 at 0:44

1 Answer 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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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