Reputation: 255
How Chain of Responsibility Pattern Differs from Decorator Pattern..?
Upvotes: 22
Views: 12919
Reputation: 57
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.
Upvotes: 0
Reputation: 161
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 :)
Upvotes: 16
Reputation: 58562
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.
Upvotes: 32