Search code examples
design-patternsdecoratorchain-of-responsibility

What pattern to use?: Chain of handlers where next handler needs to know the result of previos handler


I am working on a solution where I have implemented chain of responsibility pattern. All handlers in chain work independently and in some cases based on the input the chain is broken by a handler since there is no need to continue with the next handler. This has been working prefect until a new change request came in.:(

Now I have a case where I need to make a decision based on the result of my previous handler. Example: We have an object that based on FirstName and Last we build email and notify manager

First handler: Builds Email address (if it does not exists)

Second handler: if email has been created(and is not a new one) we need to notify manager

So the Second handler depends on the action/result of the First Handler

Choosing Chain of Responsibility: Am I breaking the pattern by wrapping the input and add some more data and use it as an input for my next handler?

Choosing Decorator: Am I breaking the pattern by breaking the chain in cases where I do not want the process to go further?

Or is there a third option?


Solution

  • Am I breaking the pattern by wrapping the input and add some more data and use it as an input for my next handler?

    Probably not. As long as you maintain the same contract for each handler, you're fine. The input can be mutable and each handler can modify it. That's no problem. The input can also have polymorphic behavior, so you can wrap it with a Decorator to modify that behavior, as long as the Decorator maintains the same contract between handlers.