简体   繁体   中英

Interaction between models MVC Java

I have a question about the MVC pattern. I am creating a maze game in JAVA with swing and I'm trying to use the MVC pattern with it. So far it goes well but I don't get the "rules" of MVC.

Let's say I have two models: one for creating a maze and the other one for the player.

The player get's it's location from the maze created in the maze model. You see, the player determines it's location from that maze and decides if it can move to a new location. This part I understand, But can my player model ask the maze model for the maze? or is it out of the question in MVC used in a java swing application? (interactions between models).

thanks!

öhm, this is one aspect of MVC that many people discuss all the time. For ME it just means that you have the really stupid VIEW-Part which does nothing else than showing Data and taking request. This Requests are forwarded to the Controller, which does some stuff with them and then call the MODEL to do the real work. So from my point of View, you are totally fine when Models talk to each other. But to rreduce dependency and improve encapsulation, i let my models get the other needed Models over the appropriate Controller, so i can use dependency Injection.

I see the COntrollers in my MVC apps as some kind of switchboard, which "regulate the traffic" and provide a place where you can call from inside the building to get a line to some co-worker, and from the outside you have to talk to the secretary.

I know other will say this is WRONG/RIGHT, but for me it works, even in teams.

MVC. Models only model your data and access that data. Views only serve the data to the user. The controller is like a conductor, putting it all together. Whether a model can be made up of another model, which I think you are asking, yes, but it might not be the best in terms of how tightly coupled your objects are. Is a player made of a maze object or just a location? Your question appears to be more about OOP principles than one of MVC. Unless of course I am miss reading your question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM