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 implementation is such that at first there is a check to see if this particular handler can process the current request, and if not, then it passes on the request to its successor.
Now, I could also do the same thing using a simple if-else conditional block. To take the first example from the link above, here is how I would change it:
class SingleHandler
{
if(request > 0 && request <= 10)
{
// Process request
}
else if(request > 10 && request <= 20)
{
// Process request differently
}
else if(request > 20 && request <= 30)
{
// Process request differently
}
}
Now, my question is, what is the fundamental difference between the two? Is there any specific reason I should use Chain Of Responsibility at all, if I can provide the exact same functionality using if-else blocks? Which one is better with regards to performance, memory consumption, maintainability, scalability?