Model view controller - where to store simple logic

I often see very different implementations of the Model View controller template and fully understand that you should adapt and use what best suits your needs, but I was wondering what would be the advantages / disadvantages / best practices of storing a simple game of logic on the controller or models?

In essence, is this the right way I should do this?

for this simple example, the player takes damage, and I have listed three possible ways to deal with it:

1.

Controller or:

_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')); } 
+4
source share
1 answer

Of course, in the Model, because you can have several controllers (in the future) that affect things in the Model in a similar way. Controllers are just a mechanism for translating user interface events into business events. A model is a place that narrows logic.

You can find the following stackoverflow threads:

Although they are specific to Java, the ideas discussed here are platform independent.

Hope this helps.

+6
source

Source: https://habr.com/ru/post/1344255/


All Articles