Search code examples
actionscript-3model-view-controlleroopdesign-patterns

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


Solution

  • 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 :)