Reputation: 122172
I'm just reading up on the Chain of Responsibility pattern and I'm having trouble imagining a scenario when I would prefer its use over that of decorator.
What do you think? Does CoR have a niche use?
Upvotes: 75
Views: 34404
Reputation: 1
Chain of Responsibility suits scenarios where multiple handlers might process the request, and you want flexible control over which one handles it (or if none do). It’s ideal when each handler can decide to pass the request along the chain.
Decorator, on the other hand, is purely for enhancing functionality—adding new behaviors or responsibilities directly to objects without affecting others of the same class. It’s best when you need layered, composable behaviors on a single object.
Upvotes: 0
Reputation: 38940
Decorator
Decorator pattern allows behaviour to be added to an individual object dynamically.
It provides a flexible alternative to sub classing for extending functionality. Even though it uses inheritance, it inherit from Lowest Common Denominator ( LCD ) interface.
UML diagram for Decorator
Consequences:
Useful links:
When to Use the Decorator Pattern?
Decorator_pattern from wikipedia
decorator from sourcemaking
Chain of responsibility:
Chain-of-responsibility pattern is a design pattern consisting of a source of command objects and a series of processing objects. Each processing object contains logic that defines the types of command objects that it can handle; the rest are passed to the next processing object in the chain
UML Diagram
This pattern is more effective when:
Useful links:
Chain-of-responsibility_pattern from wikipedia
chain-of-responsibility-pattern from oodesign
chain_of_responsibility from sourcemaking
Real world example : In a company, a designated role have particular limits to process purchase request. If person with a designated role does not have enough power to approve purchase bill, he will forward the command/request to his successor, who have more power. This chain will continue until the command is processed.
Upvotes: 2
Reputation: 49
After reading the Gang of Four definitions, I'm not convinced there's a real difference. (included for convenience)
Wikipedia fleshes them out a little, but some of it's kinda arbitrary.
The first two attributes don't really distinguish the patterns. The second two do, but the way Decorator and CoR are usually implemented don't enforce those attributes--the designer just hopes no one writes a Decorator that breaks the chain or a CoRLink that continues the chain after handling the data.
To actually implement these attributes, you'd need something like the following.
Enforced Decorator:
abstract class Decorated {
public Decorated delegate;
public final Object doIt(Object args) {
Object returnVal = behavior(arg);
if(delegate != null) returnVal = delegate.doit(returnVal);
return returnVal;
}
protected abstract Object behavior(Object args); //base or subclass behavior
}
Enforced Chain of Responsibility:
abstract class Link {
public Link delegate;
public final Object processIt(Obect args) {
Object returnVal = args;
if(isMyResponsibility) returnVal = processingBehavior(returnVal);
else returnVal = delegate.processIt(returnVal);
return returnVal;
}
protected abstract Boolean isMyResponsibility(Object args);
protected abstract Object processingBehavior(Object args);
}
(Alternately, you could just add a line to the javadoc, if all you want is to absolve yourself of the responsibiity in case someone else screws up your design--but why leave it to chance?)
Upvotes: 1
Reputation: 33
Upvotes: 1
Reputation: 11
I think the situations to apply these two patterns are different. And by the way, for decorator pattern, the decorator should know the component which it wrapped. And for CoR, the different interceptors could know nothing of each other.
Upvotes: 0
Reputation: 865
I agree that from structural standpoint this two patterns are very similar. My thought is about the final behavior:
In the classic interpretation of CoR element which handles the request breaks the chain.
If any element in decorator breaks the chain then it will be wrong implementation of decorator, because base part of behavior will be lost. And the idea of decorator is transparent addition of new behavior when the base behavior remains untouched.
Upvotes: 4
Reputation: 32189
The fact that you can break the chain at any point differentiates the Chain of Responsibility pattern from the Decorator pattern. Decorators can be thought of as executing all at once without any interaction with the other decorators. Links in a chain can be thought of as executing one at a time, because they each depend on the previous link.
Use the Chain of Responsibility pattern when you can conceptualize your program as a chain made up of links, where each link can either handle a request or pass it up the chain.
When I used to work with the Win32 API, I would sometimes need to use the hooking functionality it provides. Hooking a Windows message roughly follows the Chain of Responsibility pattern. When you hooked a message such as WM_MOUSEMOVE, your callback function would be called. Think of the callback function as the last link in the chain. Each link in the chain can decide whether to throw away the WM_MOUSEMOVE message or pass it up the chain to the next link.
If the Decorator pattern had been used in that example, you would have been notified of the WM_MOUSEMOVE message, but you would be powerless to prevent other hooks from handling it as well.
Another place the Chain of Command pattern is used is in game engines. Again, you can hook engine functions, events, and other things. In the case of a game engine, you don't want to simply add functionality. You want to add functionality and prevent the game engine from performing its default action.
Upvotes: 77
Reputation: 2937
Decorator is used when you want to add functionality to an object.
COR is used when one of many actors might take action on an object.
A particular Decorator is called to take an action, based on the type; while COR passes the object along a defined chain until one of the actors decides the action is complete.
COR might be used when there are multiple levels of escalation to different handlers -- for instance, a call center where the customer's value to the company determines if the call goes to a particular level of support.
Upvotes: 8
Reputation: 4930
Chain
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.
vs
Decorator
Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality.
I'd say its around the order in which things will happen. If you chain them, the will be called along the chain. With a decorator you're not guaranteed this order, only that additional responsibilities can be attached.
Upvotes: 15
Reputation: 11184
Well I can think of 2 situations:
Can't think of any more right now, would love to hear more in this topic.
Upvotes: 4
Reputation: 117567
I'd say that a Chain of Responsibility is a particular form of Decorator.
Upvotes: 17