22

How Chain of Responsibility Pattern Differs from Decorator Pattern..?

1

3 Answers 3

32

I generally think of the Decorator as "adding" to some thing, where as Chain of Responsiblity is more like handling something thing.

In comparing the two patterns (besides being apples and oranges) the biggest difference is the Chain of Responsibility can kill the chain at any point.

Think of decorators as a layered unit in which each layer always does pre/post processing. Chain of Responsibility is more like a linked list, and generally 1 thing handles processing.

The Chain of Responsibility pattern allows for multiple things to handle an event but it also gives them the opportunity to terminate the chain at any point.

5
  • Can you give me any scenario in what situations go for Chain of Responsibility or Decorator...?
    – Mind Dead
    Sep 15, 2010 at 20:57
  • @Mind: Like Nix said, apples and oranges. Instead write what you are supposed to do and we can help you from there.
    – Marcus
    Nov 29, 2010 at 14:38
  • I am confused as to why you are even comparing these seemingly very different paradigms in the first place? Aug 25, 2018 at 18:28
  • @Marcus Lets say you have a logging function. Now, here you would want to decorate the function because you'd want to execute the logging whether it succeeded or failed. On the flip side, lets say you want to create an object via some sort of builder. The builder function can be set through a chain, so that the entire building of the object can fail, if incase some thing broke. And chances are you don't really want to revert back to the original at the end. i.e chain pattern allows breaking. Decorator does not. PS:(I know I'm about a decade late, but I might as well add examples). Aug 27, 2020 at 11:09
  • With decorator calling base class you can perfectly terminate execution at certain decorator instance as well, so it doesn't differ from chain of command in this aspect. May 4, 2023 at 8:17
16

Scenario:

Think of a service request ( typically Admin access to your laptop ), which needs to be approved by your Manager, Director and VP. In this case, Decorator pattern would just act as if at each level there would just be comments from each of them and finally you would get an output. So, Manager would say 'Approved and forwarded', Simlarly Director 'Ok Approved and forwarded' and finally VP 'Approved'. And your final output would be combination of all the 3 comments.

Note: the chain is not going to break no matter your request was approved or Disapproved.

In the Chain Of responsibility, at each stage the Individual person has the authority to Approve or Reject. And if at any level the request is rejected, then your request doesn't proceed to the next level, instead just terminates with the result. Hope this helps :)

1
  • 1
    Nobody forbids certain decorator to not process any further. For example you could write in HRDecorator.ProcessApproval(...) { if (iApprove) { nextDecorator.ProcessApproval(...) } May 4, 2023 at 8:19
0

TLDR; In my eyes each chain of responsibility handler decides whether to handle the request or pass it along. Only one handler in the chain typically handles the request. Whereas with the decorator pattern, all decorators in the chain contribute to the modified behavior.

Example: Suppose you had a coffee class that had a cost function and ingredients function. A milk decorator could add to the cost and ingredients. A sugar decorator, a whipped cream decorator, and so on. The decorator wraps the whole class.

Now suppose the coffee class instead used the chain of responsibility pattern to calculate cost and get ingredients. It delegates these responsibilities through handler chains for cost calculation and ingredient retrieval. There would be a base handler coffee cost, a milk cost handler, a sugar cost handler, and a whipped cream cost handler. Here is the tricky part it would also need a milk ingredients handler, a sugar ingredients handler, whipped cream ingredients handler. A chain for each function.

I would argue that is the decorator pattern just with a different implementation. If we combine the milk cost handler and the milk ingredients handler, we are basically back to the decorator pattern. Chain of responsibility should have only one handler in the chain responsible not that the whole chain. The milk does not determine the cost, milk + sugar + whipped cream determines the cost, obviously the same concept applies to the ingredients.

PS. keep in mind that the decorator is a structural pattern while the chain of responsibility is a behavioral pattern.

Your Answer

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

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