1

I am trying to create a game in java using several design patterns / principles. One of them being MVC. The situation is like this: Model: Holds all game logic Controller: Button interaction and list of GameElements (see code) View: All GUI stuff including drawing.

Now, my game objects are all located under the Model, but for my drawing I've tried doing this (inside paintComponent)

ArrayList<GameElement> ge = FieldController.getElements(); // This is located under Controller
for(GameElement ge: GameElements)
{
graphics.setColor(ge.getColor());
graphics.fillRect(ge.x,ge.y,ge.width,ge.height);
}

Which works, but my question is: Where should the ArrayList of GameElements really be kept? Is it okay to hold it in the control? Or should it be kept in the view? I'm quite sure it should not be held in the model because then View&Model would be too tightly coupeled.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
Dylan Meeus
  • 5,696
  • 2
  • 30
  • 49

1 Answers1

4

Your List<GameElement> belongs in the model. Upon notification, a listening view should decide how to render the element. In this context, the notion of loose coupling refers to the notification; the view still has to interpret what it learns from the model. In this simplified example, the model manages an abstract Piece having an attribute for color. The GUI view shown renders this attribute as an Icon having the specified color. In contrast, a text view might render the attribute as a String.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • 1
    As an aside, program to the `List` interface, rather than exposing the choice to use an `ArrayList` as the concrete implementation. – trashgod Nov 16 '13 at 08:10
  • If I want to push MVC to the extreme though, so there is no connection between View and Model, would it be okay if I kept the List in the model, and use the controller to fetch the List from the model and parse it to the view? Or would that just complicate things unnecesary. (I'm going to mark this as answered already because my question is in fact answered, this is just something I wonder about as well) Thanks! – Dylan Meeus Nov 16 '13 at 08:47
  • 1
    I'd say the latter; in the Swing separable model architecture, a view and it's controller(s) can remain tightly coupled to minimize perceived latency; use `SwingWorker` to manage external latency; a related answer is seen [here](http://stackoverflow.com/a/17690433/230513). – trashgod Nov 16 '13 at 16:11