kerel
kerel

Reputation: 25

Basic MVC pattern communication

I just started to study the Model View Controller pattern. I now understand the basic usage of MVC, but when I tried to implement MVC in a simple test, I ran into a problem. Of course I could easily adjust the code so it works, but I wish to learn how to correctly implement the MVC pattern.

The test: I used actionscript 3 to make a simple program. It consist of a turret, and a mouseclick. The turret is in the middle of the screen. When I click anywhere the turret rotates towards the point where I clicked. Both the mouse and the turret have their own model,view and controller. When I click, the MouseModel changes correctly. But for the actual TurretView to respond, the TurretModel must change its rotation variable and send out an event.

The question is who responds to the MouseModel event?

     /------->MouseControl------\
    /                            \
MouseView                ?<---MouseModel

TurretView <------------------TurretModel

             TurretControl

I figured its best not to have MouseModel directly influence TurretModel or TurretControl, because this would require them to be an eventListener. Making TurretView listen to the MouseModel, and then tell TurretControl to adjust TurretModel, after wich TurretView can update through a TurretModel event looks like a lot of extra code for a simple task. Also I'd rather not have MouseControl affect TurretModel, this would break the flexibility of the mouse as input for future classes.

Ow, also in which class do I put the code for the angle calculation?

Thanks in advance

Upvotes: 1

Views: 738

Answers (2)

redhotvengeance
redhotvengeance

Reputation: 27866

Remember that the goal with MVC is primarily the separation of Model and View, and the Controller can serve as the communicator between the two.

Unless you are planning on storing each action that occurs (clicking, rotating, etc.), there is no real need for an action (in this situation) to send data to the Model. Everything you'd like to do should be easily handled with the Controller. So the flow would be:

  • Mouse click
  • Event is fired to trigger a command (in the Controller), passing along the mouse location
  • The command calculates the turret's rotation
  • The command tells the View to rotate the turret

This is, of course, my suggestion based off of your example. In truth, depending on the project, the above flow could easily change (for instance, in this situation it seems good to do rotation calculation in the command, but in other situations that may not make as much sense). Look at MVC as a goal - you're trying to separate these elements as much as possible, but there is no 100% "works-every-time" way to do it.

Robotlegs, a popular MVC framework, has a great diagram on their site showing how they tackled MVC:

http://www.robotlegs.org/diagram/

I'm not advertising that you NEED to use Robotlegs (it's good, but there's plenty of other alternatives), but they definitely made a pretty diagram :)

Upvotes: 3

Amy Blankenship
Amy Blankenship

Reputation: 6961

You should have one model, which then has pieces in it that you are currently calling MouseModel and TurretModel. You can either keep those names and breakdowns, or do something else once you have a better "handle" on what needs to be done.

The View that is tracking the mouse clicks should dispatch an event that your Controller component catches to update the TurretModel (at this point, there's probably no need for a MouseModel). Your TurretView can then update itself based on the TurretModel, or the Controller can update the TurretView based on the new value. This will depend on how you have it wired.

Usin a Framework such as Robotlegs can help you figure out all the ins and outs of this process, and I've heard that this book http://shop.oreilly.com/product/0636920021216.do provides a great explanation of MVC, even if you don't choose to use Robotlegs after you read it.

Upvotes: 0

Related Questions