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

后端 未结 11 1885
[愿得一人]
[愿得一人] 2020-12-04 12:58

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

相关标签:
11条回答
  • 2020-12-04 13:31
    1. keyword 'extends' - static extension.
    2. Decorator pattern - dynamic extension.
    3. Chain Of Responsibility pattern - just processing of a command object with a set of processing objects and those objects don't know each other.
    0 讨论(0)
  • 2020-12-04 13:32

    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.

    0 讨论(0)
  • 2020-12-04 13:33

    Well I can think of 2 situations:

    • You don't have a core object, i.e. you don't know what to do with the request after it passed all the layers/filters. (something like an aspect like interceptor chains that don't really care where the request ends).
    • You need to selectively apply some pre or post processing to the request. Not in a general enhancement form as the decorator does. i.e. Filters may or maynot handle a specific request but adding a decorator always enhances your object with some functionality.

    Can't think of any more right now, would love to hear more in this topic.

    0 讨论(0)
  • 2020-12-04 13:42

    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.

    0 讨论(0)
  • 2020-12-04 13:44

    Decorator

    1. Decorator pattern allows behaviour to be added to an individual object dynamically.

    2. 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:

    1. With decoration it is also possible to remove the added functionalities dynamically.
    2. Decoration adds functionality to objects at runtime which would make debugging system functionality harder.

    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:

    1. More than one object can handle a command
    2. The handler is not known in advance
    3. The handler should be determined automatically
    4. It’s wished that the request is addressed to a group of objects without explicitly specifying its receiver
    5. The group of objects that may handle the command must be specified in a dynamic way

    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.

    0 讨论(0)
提交回复
热议问题