Hello
Iāve recently started experimenting with the Model View Controller (MVC) pattern, and itās solved a lot of problems for me.
I do have one question about the best way to implement it, and I hope some experienced coders out there can point me in the right direction.
Hereās the setup:
Iām building a keyboard controlled spacehip. Iāve nested the model, view and controler class in one single class called SpaceShipMVC:
[AS]
package spaceElements.spaceShip
{
import utils.*;
import flash.events.Event;
import flash.display.MovieClip;
public class SpaceShipMVC extends MovieClip
{
private var _model:ShipModel;
private var _controller:ShipController;
private var _view:ShipView;
public function SpaceShipMVC()
{
_model = new ShipModel();
_controller = new ShipController(_model);
_view = new ShipView(_model, _controller);
addChild(_view);
}
public function update():void
{
_model.update();
}
}
}
[/AS]
As you can see, the public āupdateā method points to another public method also called āupdateā in the model.
I use this new class in the client (document class), like this:
[AS]
package
{
import utils.;
import spaceElements.spaceShip.;
import flash.events.Event;
import flash.display.MovieClip;
public class Mvc extends MovieClip
{
private var _spaceShip:SpaceShipMVC;
public function Mvc()
{
_spaceShip = new SpaceShipMVC();
addChild(_spaceShip);
addEventListener(Event.ENTER_FRAME,onEnterFrame);
}
private function onEnterFrame(event:Event):void
{
_spaceShip.update();
//This sends a method call to: _spaceShip._model.update();
}
}
}
[/AS]
As you can see, the client (the document class) is directly triggering the modelās āupdateā method through the SpaceShipMVC instance. (The update method moves the ship.)
I have some questions about this:
- Is this an OK structure to use or am I opening up a can of worms?
- Rather than referencing the modelās update method directly, should the the method call be sent through the controller?
And hereās a more complex question:
- Iām going to add collision detection. The model will need to know. the size and rotation of the view. My understanding of MVC is that the model should not reference the view, and that the view should not set properties in the model. Should information about the view be sent to the model through the controller?
I look forward to hearing your thoughts!