2

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.

asked Feb 15, 2021 at 20:36
4
  • 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. Commented Feb 15, 2021 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) Commented Feb 15, 2021 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. Commented Feb 16, 2021 at 7:49
  • Who holds the dictionary? Who updates it? Commented Feb 17, 2021 at 11:20

1 Answer 1

5

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.

answered Feb 15, 2021 at 21:18

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.