Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why would I ever use a Chain of Responsibility over a Decorator?

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?

like image 694
George Mauer Avatar asked Apr 14 '09 14:04

George Mauer


People also ask

What is the difference between chain of responsibility and decorator patterns?

The chain-of-responsibility pattern is structurally nearly identical to the decorator pattern, the difference being that for the decorator, all classes handle the request, while for the chain of responsibility, exactly one of the classes in the chain handles the request.

In what scenarios can chain of responsibility be used?

Where and When Chain of Responsibility pattern is applicable : When you want to decouple a request's sender and receiver. Multiple objects, determined at runtime, are candidates to handle a request. When you don't want to specify handlers explicitly in your code.

What is the purpose of chain of responsibility?

Chain of Responsibility is a behavioral design pattern that lets you pass requests along a chain of handlers. Upon receiving a request, each handler decides either to process the request or to pass it to the next handler in the chain.

What are the consequences of Chain of Responsibility pattern?

Consequences. Chain of Responsibility has the following benefits and liabilities: Reduced coupling. The pattern frees an object from knowing which other object handles a request.


1 Answers

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.

like image 138
William Brendel Avatar answered Oct 01 '22 10:10

William Brendel