Chain of responsibility to teams in a queue or stack

Why do I need to use a chain of responsibility if I could load all the commands into a container and just execute the commands one by one. This will make the request processing chain in a row. BTW I feel this is better than CoR because you can delete some specific commands or replace another command at runtime, whereas for CoR you need to create another chain in order to change something in the chain, since you do not have access to elements in except for the first element. (This is, in fact, the only linked list.)

+5
source share
2 answers

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.

+1
source

I do not think CoR requires a separate list. This may be a general implementation; but a doubly linked list or almost any other collection / container can be used if it separates the sender of the message from the receiver (s) of the message. What you are describing is perfectly compatible with the CoR pattern. You just improved the ability to customize Chain. Itโ€™s important to note that the GoF definition instructs us to implement the chain,

without binding and priority of the hard wiring handler,

... but I do not think that this should limit the choice of collection for handlers.


Also note that the responsibility chain is not the same as the processing chain. The latter implies sequential operations (i.e., pipeline ), while the former is hierarchical, but not necessarily sequential. Any number and combination of handlers (or none of them) can act on one request.

Finally, I assume that using the word โ€œcommandsโ€ in a question is a reference to handlers in the chain, not a reference to the Command Template . CoR and Command patterns can be complementary in terms of chaining requests, but traditionally not in terms of commands as handlers.

0
source

All Articles