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.
-
2What 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– Philip Kendall2021年02月15日 20:48:48 +00:00Commented Feb 15, 2021 at 20:48
-
1One 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ć– Filip Milovanović2021年02月15日 21:25:19 +00:00Commented Feb 15, 2021 at 21:25
-
1Because 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– Laiv2021年02月16日 07:49:51 +00:00Commented Feb 16, 2021 at 7:49
-
Who holds the dictionary? Who updates it?pjc50– pjc502021年02月17日 11:20:56 +00:00Commented Feb 17, 2021 at 11:20
1 Answer 1
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.
Explore related questions
See similar questions with these tags.