Why would I ever use a Chain of Responsibility ove

2020-02-02 04:25发布

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?

11条回答
We Are One
2楼-- · 2020-02-02 05:01

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.

查看更多
冷血范
3楼-- · 2020-02-02 05:05

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.

查看更多
聊天终结者
4楼-- · 2020-02-02 05:10

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

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

enter image description here

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.

查看更多
【Aperson】
5楼-- · 2020-02-02 05:12

After reading the Gang of Four definitions, I'm not convinced there's a real difference. (included for convenience)

  • Decorator: Allows for the dynamic wrapping of objects in order to modify their existing responsibilities and behaviours
  • Chain of Responsibility: Gives more than one object an opportunity to handle a request by linking receiving objects together

Wikipedia fleshes them out a little, but some of it's kinda arbitrary.

  • Decorator is typically implemented as a Linked List. But I think that's too low-level to be considered "part" of the pattern.
  • Chain of Responsibility links only handle data if it's their responsibility; but determining responsibility and data handling are both part of behavior. Decorators can do this just as easily.
  • Decorator requires you to call the delegate.
  • A "pure" CoR link should only call the delegate if it doesn't handle the data.

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?)

查看更多
劳资没心,怎么记你
6楼-- · 2020-02-02 05:13
  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.
查看更多
登录 后发表回答