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.

149 questions
108
votes
9 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…
72
votes
11 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
  • 103,465
  • 117
  • 349
  • 581
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
  • 55,232
  • 47
  • 119
  • 176
17
votes
4 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
  • 23,581
  • 8
  • 51
  • 82
16
votes
6 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
  • 26,873
  • 20
  • 76
  • 120
14
votes
2 answers

Design Patterns Chain of Resposibility Vs Decorator

How Chain of Responsibility Pattern Differs from Decorator Pattern..?
Mind Dead
  • 177
  • 1
  • 7
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,061
  • 3
  • 12
  • 27
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
  • 12,636
  • 9
  • 54
  • 88
11
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
  • 539
  • 2
  • 11
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
  • 998
  • 6
  • 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

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,239
  • 7
  • 39
  • 77
8
votes
2 answers

Java Generics: chaining together generic function object

I've been struggling with the following problem. I have a series of function objects, each with it's own input and output types defined via generic type arguments in java. I would like to arrange these in a chain so that raw data is input to the…
downer
  • 874
  • 2
  • 10
  • 24
8
votes
3 answers

Would this be a pipeline, a chain of responsibility, or something else?

I'm building a multiprocess architecture that seems to be a strange meld of a pipeline and a chain of responsibility. Essentially, I have a chain of handlers linked up by queues. Each handler will receive an object that represents the input data,…
Jason Baker
  • 171,942
  • 122
  • 354
  • 501
7
votes
3 answers

Design pattern for replacing nested if statements (arrow anti-pattern)

I noticed my code looks really ugly, and is hard to maintain. Basicaly i need do to some person check. Pseudo code is like this (BTW i can't "cut" anything in query, and it's not realy the point of my question): List persons =…
1
2 3
9 10