Search code examples
architecturedesign-patternsmodel-view-controller

In MVC is the View allowed to see but not talk to the model?


I have been reading up on MVC and trying to follow it to the best of my ability but one thing keeps coming up.

Can the Views see Model Objects?

My current structure for my game is as follows.

Objects(Model) <-> Scene (Controller) <-> Sprites (View)

Currently, I do the following.

The scene creates a MapObject(model). The Scene then creates a MapSpriteNode(view) but initializes it with a MapObject. The MapSpriteNode needs to draw and uses the information to lay out its view and sub-views. Now the MapSpriteNode gets to keep a reference to that MapObject, but is not allowed to change the MapObject in any way. That way when update is called on the MapSpriteNode it can see things like dirty tile indexes on the MapObject and it can update its corresponding TileNodes.

So now this happens

Sprite is touched and informs controller --> Controller reports interaction to Model --> Model updates accordingly and marks dirty indexes

Update loop happens

Scene calls update to -> MapObject (any other updates that need to happen over time) The scene calls update to -> MapSpriteNode (looks to see if there are dirty indexes and updates) Scene calls cleanup dirty to -> MapObject (dirty indexes are removed because rendering is done)

Is this still following MVC?


Solution

  • "Can the Views see Model Objects?"

    Yes, View can see "inside" the Model as long as you don't change anything through the View.

    "Is this still following MVC?"

    Definitely.

    In the MVC architecture, Model should be independent of Controller and View, and in case of the "passive" implementation (your case), View should poll the Model to get data after being notified by the Controller, instead of being notified by the Model to do so (you can assume that in this "passive" case Model does not even know that Controller and View exists).

    View should poll data from Model when being notified by Controller and this happens when Scene calls update to MapSpriteNode, and View also should notify Controller about events like when Sprite informs Controller after being touched.

    As far as Controller goes, it should notify View to update like when Scene calls update to MapSpriteNode, and it should notify Model to update.