• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Difference between Decorator and Chain of Responsibility patterns

 
Raghavendra Desoju
Ranch Hand
Posts: 95
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ranchers,

I am quite confused with Decorator and Chain of Responsibilities patterns. Per definition, Decorator adds additional responsibilities and Chain of Responsibilities pattern lets classes to share the responsibilities.

Can Decorator be called a Chain of Responsibilities on the same Object? How to really judge and chose between these two design patterns?

Thanks, Raghu
 
Stefan Evans
Bartender
Posts: 1807
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a significant difference between the two patterns, even if they are implemented in much the same fashion.

With a decorator, you always do some work, and then always pass the message on to your "parent" object.
With chain of command, you might do some work.  Only in the event that you don't do the work do you pass the message on to your "parent" object.

The decorator is "adding value" as a message goes past.
The Chain of Command, either handles the message, or passes it on to the "boss" to handle it.
 
K. Tsang
Bartender
Posts: 3585
16
Android Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Going by the source (GoF book)

decorator pattern is structural and its intents are
* Attach additional responsibilities to an object dynamically
* Provide a flexible alternative to subclassing for extending functionality

An example of this is adding borders or scroll bars to a text view


For chain of responsibility pattern is behavioral and its intents are:
* Avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request
* Chain the receiving objects and pass the request along the chain until an object handles it

An example of this is purchasing authorization (branch manager → regional director → vice president → president). Another is java exception handling.

 
Raghavendra Desoju
Ranch Hand
Posts: 95
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you !!
 
AnkitKumar Singh
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Decorator is Structural whereas Chain of Responsibility is Behavioral Design pattern so they don't share similarities.

Decorator pattern works with the concept that code should be closed for modification and open for extension.
Take an example of Computer. Client can ask for
A computer
A computer and a disk
A computer and a disk and a monitor

If decorator design pattern is not followed then, we will have to change the computed implementation every time different combination is asked.
In decorator design pattern, one decorator is created which will be extended by Disk, monitor, etc. and main description method is not modified. And if you need say CDWriter then new class can be added extending this decorator.


In Chain of Responsibility design pattern, many objects are available to honor the request and anyone can server. If first object is not able to server then it passes the request to next one and till end.
As K. Tsang mentioned, Java exception handling is the best example.
 
AnkitKumar Singh
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
AnkitKumar Singh wrote:Decorator is Structural whereas Chain of Responsibility is Behavioral Design pattern so they don't share similarities.

Decorator pattern works with the concept that code should be closed for modification and open for extension.
Take an example of Computer. Client can ask for
A computer
A computer and a disk
A computer and a disk and a monitor

If decorator design pattern is not followed then, we will have to change the computer implementation every time different combination is asked.
In decorator design pattern, one decorator is created which will be extended by Disk, monitor, etc. and main description method is not modified. And if you need say CDWriter then new class can be added extending this decorator.


In Chain of Responsibility design pattern, many objects are available to honor the request and any object can serve. If first object is not able to server then it passes the request to next one and till end.
As K. Tsang mentioned, Java exception handling is the best example.
 
AnkitKumar Singh
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
AnkitKumar Singh wrote:
AnkitKumar Singh wrote:Decorator is Structural whereas Chain of Responsibility is Behavioral Design pattern so they don't share similarities.

Decorator pattern works with the concept that code should be closed for modification and open for extension.
Take an example of Computer. Client can ask for
A computer
A computer and a disk
A computer and a disk and a monitor

If decorator design pattern is not followed then, we will have to change the computer implementation every time different combination is asked.
In decorator design pattern, one decorator is created which will be extended by Disk, monitor, etc. and main description method is not modified. And if you need say CDWriter then new class can be added extending this decorator.


In Chain of Responsibility design pattern, many objects are available to honor the request and any object can serve. If first object is not able to server then it passes the request to next one until request is served.
As K. Tsang mentioned, Java exception handling is the best example.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic