开发者

Model View Controller - where to keep simple logic

I often see very different implementations of the Model View Controller Pattern, and completely unde开发者_高级运维rstand that you should adapt and use what fits your needs the best, but I was wondering what would be the advantages/disadvantages/best practice of keeping simple game logic in ether the controller or model?

In essence which is the correct way I should be doing this?

for this simple example the player receives damage and I have listed three possible ways of dealing with it:

1.

contoller:

_model.playerDamage - 15;
if (_model.playerDamage <= 0){
    _model.playerLives --;
    _model.restartLevel();
}

2.

controller:

_model.playerDamage = 15;

model:

function set playerDamage(value:int){
     playerDamage = value;
     updatePlayer();
}

function updatePlayer():void{
    if (playerDamage<=0){
         palyerLives --;
         restartLevel();
    }
}

3.

controller:

_model.playerDamage = 15;
_model.addEventListener('playerChange', checkPlayerStatus);

function checkPlayerStatus(e:Event):void{
    if (_model.playerDamage<=0){
         _model.playerLives --;
         _model.restartLevel();
    }
}

model:

function set playerDamage(value:int){
     playerDamage = value;
     dispatchEvent(new Event('playerChange'));
}


Ofcourse in Model because you may have multiple controllers (in future) which affect things in Model in similar or same way. Controllers are just a mechanism to translate UI events into business events. Model is the place that crunches the logic.

You may find following stackoverflow threads useful:

  1. Is the MVC-pattern a pure presentation-tier pattern?
  2. What do I call the code that orchestrates my web application?

Though they are java specific but the ideas discussed here are platform independent.

Hope that helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜