Design Patterns Chain of Resposibility Vs Decorator
How Chain of Responsibility Pattern Di开发者_StackOverflow中文版ffers from Decorator Pattern..?
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.
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 :)
精彩评论