Why I need to use Chain of responsibility, if I could load all commands into some container and just execute commands one by one. This will make a chain of processing a request in a row. BTW I feel that it is better than CoR because you can remove some particular commands or replace with another command on runtime, whereas for CoR you need to create another chain to change something in the chain as you don't have access to elements in the chain except the first element. (It is, actually, a single linked list.)
Asked
Active
Viewed 2,395 times
4

Zermingore
- 743
- 2
- 11
- 28

Narek
- 38,779
- 79
- 233
- 389
-
Possible duplicate of [What are the advantages of chain-of-responsibility vs. lists of classes?](https://stackoverflow.com/questions/1055383/what-are-the-advantages-of-chain-of-responsibility-vs-lists-of-classes) – jaco0646 Feb 19 '19 at 15:53
1 Answers
0
Just to "keep it simple silly" :-)
Create a queue/stack when you specifically need it, and you would know when you need it, won't you ?
For example- if you had to do some simple data manipulation like :
data.clean().format().save();
vs
commands.push(new Cleaner(data));
commands.push(new Formatter(data));
commands.push(new Updates(data));
commands.execute();
also notice, I had to write more code to create new data structure (commands stack).
But lets say it was something more strategic, say a shooter game. The player can be shot, killed, etc, and the results depend on the choosen difficulty level 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);
};
Notice in this case, we had to create a strategy that can be passed around and happen at runtime.
So when in easy mode, the bodyShot may not have the last element (LooseHealth), and headShot may have LooseHealth instead of KillPlayer as last element.
Hope it helps.

Nishant
- 4,659
- 2
- 27
- 43
-
-
You have compared stack with static `data.clean().format().save();`. But I was asking stack vs COR, as runtime changes can be done in both, as in COR you can create new chains. – Narek Sep 14 '15 at 05:24