tags:

views:

158

answers:

3

I'm creating a pacman game using the MVC pattern. According to this diagram on wikipedia the model and the view interact.

"Note: the solid lines indicate a direct association, and the dashed lines indicate an indirect association"

Now from my understanding, surely everything goes through the controller? Say for example, player moves or user clicks add to cart, does that not then get handled by the controller and the appropriate data in the model manipulated by the controller?

I can't see why the view would interact directly with the model?

Thanks

Adam

+1  A: 

The view gets the data it should display from the model. (for example: the list of items in your shopping cart)

Simon Groenewolt
+1  A: 

It's true that there would be very little reasons to modify your model from your view (it's a no-no), but you could end up accessing (as in reading) the models from your view.

Let's do some pseudo-code.

Say you have a Player and a Game model.

Your controller might do something like:

player1 = new Player();
player1.setLives(3);
player1.setScore(0);
player1.setPosition(0, 0);
game = new Game();
game.addPlayer(player1);
game.launch();

Then, your view might do something like:

foreach (game.getPlayers() as player) {
   gameBoard.draw(pacmanGlyh, player.getPosition());
}

You see that you are using the model from the view.

But keep in mind that philosophy on how to implement MVC differs slightly from one programmer to another. Some might recommend using an observer pattern depending on what your are doing. The end goal of MVC is really to separate your business logic from the user interface, making your life easier when doing maintenance and refactoring as well as keeping your code clean.

lpfavreau
A: 

No, the view reads its data from the model (usually using the observer pattern). PAC (presentation-abstraction-controller) is a good example of an MVC-based pattern that uses the mediator pattern - the controller passes messages between the view and model.

Draemon