Search code examples
design-patternsnettyhandlerschain-of-responsibility

Chain of Responsibility Scope


Hi I am wondering Chain of Responsibility Scope.

In general it is a common used pattern which has handlers in itself and each handler is passing the functionality action to it's supervisor.

What I see in the example scenarios is:

"Only one related handler is handling the functionality itself and all the other handlers 
 are just passing through to their supervisor handler."

Does it violate the chain of responsibility pattern in such case:

"Every handler has the responsibility to take an action instead and after that passing 
 to supervisor.

As an abstract:

Chain of Responsibility Recommended Scenario:
Handler1(Take No Action) --> Handler2 (Take No Action) --> Handler3(Take All Action)

Chain of Responsibility Wondering Scenario:
Handler1(Take Partial Action) --> Handler2(Take No Action) --> Handler(Take Partial Action)

Does the second scenario suits the chain of responsibility or violates it?

For Example Netty have handlers in itself and all have their responsible actions and they also pass the information in between themselves. Can we say that Netty handler mechanism suits the chain of responsibility?


Solution

  • In general the Chain of Responsibility involves the recommended scenario you given in your question. That is, given a 'command' object instance is passed from one handler to the next in the chain until a handler processes the command and your done.

    In terms of the second scenario (the Chain of Responsibility Wondering Scenario) in your question care must be taken because you have introduced serious complications into the pattern. How does handler1 know that it has taken only partial action? If it presumes that additional handlers will do processing it will be calling the additional handlers and there may be no need; which would be wasteful. The primary problem you will be faced with will be the Single Responsibility Principle however. If multiple handlers (in the case of your example Handler1 & Handler) are looking for the command to process this means that any changes to command would potentially impact both the handlers. This would raise a red flag with me that my responsibilities may not be defined properly and deserve another look. In general I am suspicious of circumstances where a change in one class has a potential ripple effect through a series of other classes. In this case my preference would be to extract out the code needed for execution of the command into classes other than handlers and define one handler to handle the command which could call all of the extracted out code.