Questions tagged [chain-of-responsibility]

Design pattern consisting of a source of command objects and a series of processing objects. One of the Gang of Four's behavioral design patterns.

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. A mechanism also exists for adding new processing objects to the end of this chain.

This is one of the Gang of Four's behavioral , first published in Gamma et al.'s book "Design Patterns: Elements of Reusable Object-Oriented Software".

More information is available on Wikipedia.

160 questions
113
votes
10 answers

Avoiding instanceof in Java

Having a chain of "instanceof" operations is considered a "code smell". The standard answer is "use polymorphism". How would I do it in this case? There are a number of subclasses of a base class; none of them are under my control. An analogous…
74
votes
10 answers

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?
George Mauer
  • 117,483
  • 131
  • 382
  • 612
20
votes
2 answers

Design Patterns Chain of Resposibility Vs Decorator

How Chain of Responsibility Pattern Differs from Decorator Pattern..?
Mind Dead
  • 235
  • 1
  • 2
  • 7
19
votes
8 answers

What's the difference between "Chain of responsibility" and "Strategy" patterns?

I'm raising this question because of another question I asked here on SO some days ago. I had to solve an specific problem, and after two replies I got, I realized two patterns can help to solve that problem (and any other similar). Chain of…
Oscar Mederos
  • 29,016
  • 22
  • 84
  • 124
18
votes
5 answers

What are the advantages of chain-of-responsibility vs. lists of classes?

Recently, I was discussing with another programmer the best way to refactor a huge(1000 lines) method full of "if" statements. The code is written in Java, but I guess this issue could happen in other languages such as C# as well. To solve this…
luiscubal
  • 24,773
  • 9
  • 57
  • 83
17
votes
6 answers

Replace giant switch statement with what?

I have a code that parses some template files and when it finds a placeholder, it replaces it with a value. Something like: %title% ...etc. In code, the parser finds those, calls…
Milan Babuškov
  • 59,775
  • 49
  • 126
  • 179
14
votes
2 answers

For a large validation task is chain of responsibility pattern a good bet?

I need to build a process which will validate a record against ~200 validation rules. A record can be one of ~10 types. There is some segmentation from validation rules to record types but there exists a lot of overlap which prevents me from…
yamori
  • 1,213
  • 4
  • 14
  • 27
12
votes
3 answers

How to inject the dependency of the next handler in a chain of responsibility?

In my current project, I'm using quite a few Chain of Responsibility patterns. However, I find it a bit awkward to configure the chain via dependency injection. Given this model: public interface IChainOfResponsibility { IChainOfResponsibility…
12
votes
2 answers

What are the known "gotchas" with regards to the Chain of Responsibilty pattern?

I have been finding myself using the Chain of Responsibility pattern often (3 times is often for me) in my current project and I'm wondering if I have become a little over-enthusiastic about the solution. Specifically, I have been using the Apache…
Elijah
  • 13,368
  • 10
  • 57
  • 89
12
votes
3 answers

Chain of responsibility: loop or next?

I am implementing a chain of responsibility pattern. I have different policies that can be combined in a list and I have a Processor that processes the list of policies. Each policy can process the CustomInput and can choose if the rest of the…
Boris
  • 601
  • 3
  • 13
10
votes
1 answer

What is the difference between Chain Of Responsibility design pattern and using a simple if-elseif-else block?

I was just looking up Chain Of Responsibility the other day, and I came across this example. Basically, there is an abstract handler, and then are concrete handlers, each of which implement the handle method of the parent abstract handler. The…
Bhushan Shah
  • 1,028
  • 8
  • 20
9
votes
2 answers

What's the sequence of middleware execution in django when error occurs in process_request?

I am looking into django middleware codebase. I looked into following diagram So, the diagram is quite clear. But I have some questions What happens when exception comes in process_request() middleware ? How is it handled ? Will the…
9
votes
2 answers

How do I implement the Chain of Responsibility pattern using a chain of trait objects?

I'm trying to implement the Chain of Responsibility design pattern in Rust: pub trait Policeman<'a> { fn set_next(&'a mut self, next: &'a Policeman<'a>); } pub struct Officer<'a> { deduction: u8, next: Option<&'a…
9
votes
2 answers

Chain of responsibility vs Finite State Machine - differences

So as I am a bit of electrician and programmer I thought I knew FSM design pattern very well. It is: We have set of Nodes, Each Node knows, what to do, when program is in this node, Each Node contains references to another chosen nodes, and knows…
DawidPi
  • 2,285
  • 2
  • 19
  • 41
9
votes
2 answers

Why would I use a chain of responsibility over a switch-statement

Consider you got several validations. Those validations should only take effect if the object to be inspected is of a certain type. Why would I use a chain of responsibility over a switch-statement? Example with chain of responsibility public class…
Chris311
  • 3,794
  • 9
  • 46
  • 80
1
2 3
10 11