Forums Register Login

Confusion in Chain of responsibility and Decorator Pattern

+Pie Number of slices to send: Send
Hi All,
I am confused between chain of responsibility and decorator pattern in cases when intermediate receivers in chain of responsibililty may also handle the request.(Like in case of servlet filters).

In Chain of Responsibility pattern, receivers are chained and pass any request messages from object to object until it reaches an object capable of handling the message. The intermediate objects "may" (or may not) handle the request message and modify it and pass it on. In decorator, each decorator object will add additional functionality (handle) and pass it on to the next object.
Is the only difference a "safety net" in case of Chain of Responsibility to "catch" any requests which go unhandled.

So how do we differentiate looking at the code, which pattern is being used.
Please correct me if I am wrong in my understanding.

TIA,
- Tinu
+Pie Number of slices to send: Send
 

Originally posted by Tinu Sharma:
Hi All,
I am confused between chain of responsibility and decorator pattern in cases when intermediate receivers in chain of responsibililty may also handle the request.(Like in case of servlet filters).

In Chain of Responsibility pattern, receivers are chained and pass any request messages from object to object until it reaches an object capable of handling the message. The intermediate objects "may" (or may not) handle the request message and modify it and pass it on. In decorator, each decorator object will add additional functionality (handle) and pass it on to the next object.
Is the only difference a "safety net" in case of Chain of Responsibility to "catch" any requests which go unhandled.

So how do we differentiate looking at the code, which pattern is being used.
Please correct me if I am wrong in my understanding.

TIA,
- Tinu



Their purpose are different. Decorator only adds additional responisbility. It does not address automatic flow of control across a set of related objects.

Perhaps an example would clarify.

EJBHome, EJBObject are an instance of decorator. The stubs that the client use are really just stub or dummy. The container adds additional responsibility i.e. security, trans mgmt etc by virtue of decorator pattern. The advantage being the client is light weight and does not need to bother on implementations of how to do security, transaction mgmt, lifecycle mgmt etc.


Java Exception handling class hierarchy is an example of chain of responsibility. Exception being the most generic form. When you catch exceptions the exception object which is thrown can be caught in your code in a way you want to. As you know in java, the exception object when thrown the objects passes across the different chains to get handled. Chain or Resp. makes sure that someone is out there to out of the many who is responsible to take the appropriate action. i.e. There are many object involved.

the purpose is different. hope you get the picture now
+Pie Number of slices to send: Send
Thanks.
That clarifies.
catch it before it slithers away! Oh wait, it's a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 2514 times.
Similar Threads
Listeners Vs Filters
Help Identify the Design Pattern
What Design Pattern should I use?
help: comparison between Decorator and Chain of Responsibility
Removing Decorator From The Decorated Object
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 05:15:42.