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.

  • 2
    What problem are you trying to solve? Your solution may be appropriate for some problems, the Chain of Responsibility pattern may be appropriate for others. – Philip Kendall Feb 15 at 20:48
  • 1
    One motivation for the Chain of Responsibility pattern is when you already have an object structure that forms a chain or multiple chains (e.g., a tree of controls), and you want to leverage that fact to send a message down the chain to an unknown recipient (e.g., the parent form receives a click event, then passes it down to a child panel, that in turn passes it down to a button, that handles the click) – Filip Milovanović Feb 15 at 21:25
  • 1
    Because in a dictionary, links are not aware of the "next" link. If for any reason you want to break/stop the message propagation, you can not, you have to programme the first link as a sort of aggregator which besides its logic, also has to have some additional one to decide when it stops. Even how to pass the message from one link to another, what may imply coupling of all sort. – Laiv Feb 16 at 7:49
  • Who holds the dictionary? Who updates it? – pjc50 Feb 17 at 11:20

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.


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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.