Question

I am new to SE. Recently I read about the Chain of Responsibility pattern, so basically what it does is: it creates a class that has some methods and also next class (next chain block), so when it processes the data it passes the processed data to the stored class. I have seen many implementations of it, for example here: https://www.journaldev.com/1617/chain-of-responsibility-design-pattern-in-java

mt question is: why just don't create a dictionary of class? dict['0'] would be first-class, dict['1'] would be second class, and so on. by the way those classes are more general (they can also not inherit from one superclass, they might be totally unconnected classes), so this implementation is more general and also easier to implement.

Was it helpful?

Solution

A chain is a sequence of events that gets the process to the end of the chain. (done)

Each link just passes on to the next link, so a dictionary isn't really needed in that case.

One could certainly create an array/list/dictionary of generic command objects and then have an orchestrator run them.

orchestrator.run(commands)

A dictionary might be needed if you needed to quickly look up a particular command by key or needed a way to uniquely identify each command.

The orchestrator could run them in different ways. Sequentially like a chain, or asynchronously, or even multi-threaded, etc. This could also allow for much more complex command structures than a simple chain. Branches of commands, etc.

If you needed more complex processing directives than a simple chain of commands, maybe this would be a solution.

It depends what is needed. For patterns there is no right or wrong, just choices and potential implementations.

Licensed under: CC-BY-SA with attribution
scroll top