The other day, I was just looking for a chain of responsibility, and I came across this example.
Basically, there is an abstract handler, and then specific handlers, each of which implements the handle method of the parent abstract handler. The implementation is such that there is a check first to check if this handler can process the current request, and if not, it passes the request to its successor.
Now I could do the same using a simple if-else conditional block. To take the first example from the link above, here is how I changed 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 same functionality using if-else blocks? Which one is better in terms of performance, memory consumption, maintainability, scalability?
source share