Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC component responsibility, clarification needed

I am working on a card game and among other things have a UIView class responsible for presenting the view of a game board.

As part of the task of presenting the view, class needs to perform a bunch of tasks like

  • Laying out cards from deck
  • Update card layout in response to a user, computer or user's move

But here i wonder, to what degree should my View know about a Deck of Cards? For instance, is it ok that my View maintains a pointer to the Deck it draws?

like image 627
James Raitsev Avatar asked Mar 03 '26 02:03

James Raitsev


2 Answers

For MVC, the View should only know about the information it needs to properly do the layout. The controller should be giving it this data, and also telling the view when it needs to update itself (depends more on the particular MVC framework, but in general, this is true).

So, if your view needs to know about an entire Deck to do the layout, then yes, but it sounds strange to me. I would think that, at most, the view would only need to know the current number of cards in the deck, but I don't know all card games.

For a couple of card games I can think of, a list of the cards that need to go in various places on the board, plus a few counts of cards in other locations is probably all that the view needs to know about. The controller takes care of populating the lists (deciding what cards need to be shown in what places) and the view just represents that.

In general, all a View does is represent the state of the application (in this case, the current board state), and nothing else. It doesn't have any logic that relates to anything more than representing a subset of the full application state. In your case, if the View has any logic relating to more than deciding what pixels to show where, it's doing too much.

like image 157
cdeszaq Avatar answered Mar 06 '26 03:03

cdeszaq


View have no idea what deck of cards is. Model is the brain, it knows everything. View just get DummyCard, which can be simple enum determining color and shape of card. And it knows how to draw it. So, you can tell the View "Draw me an Ace of Spades" and that's it. DummyCard is simplified version of your Card from model. It's without logic. When you want to redraw the game state, you pass the copy (dummy) objects to the View and it draws it.

like image 20
Adam Stelmaszczyk Avatar answered Mar 06 '26 01:03

Adam Stelmaszczyk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!