Just to "keep it just stupid" :-)
Create a queue / stack when you need it, and you know when you need it, right?
For example, if you needed to perform some simple data manipulations, for example:
data.clean().format().save();
vs
commands.push(new Cleaner(data)); commands.push(new Formatter(data)); commands.push(new Updates(data)); commands.execute();
Also note, I had to write more code to create a new data structure (stack command).
But let's say that it was something more strategic, say, a shooter. The player can be shot, killed, etc., and the results depend on the chosen level of difficulty of the game.
headShot = new Command(); headShot.push(new FreezePlayer()); headShot.push(new BleedPlayer()); headShot.push(new KillPlayer()); bodyShot = new Command(); headShot.push(new FreezePlayer()); headShot.push(new BleedPlayer()); headShot.push(new LooseHealth()); player.onHit = function(hitPoint){ hitPoint.aboveNeck() ? headShot.on(this) : bodyShot.on(this); };
Please note that in this case we needed to create a strategy that can be transferred and launched at runtime.
Therefore, when in light mode, the bodyShot may not have the last element (LooseHealth), and the headShot may contain LooseHealth instead of KillPlayer as the last element.
Hope this helps.
source share