Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Confusion in Chain of responsibility and Decorator Pattern

 
Alka Sharma
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
suekar meredilko
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Alka Sharma
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.
That clarifies.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic