I want to implement code for executing a workflow, but I'd like to avoid chains of if-else statements. Which design pattern should I use? I looked at some of them, but couldn't find an appropriate one
Example of workflow.
A-> if(B) do C,
if(!B) do D-> E -> if(F) do G,
if(!H) do I-> J-> K
and so on..
The obvious choice to also consider here is Chain of Responsibility. The problem with CoR in cases like this is sometimes you don't really know what the paths will be in advance, and so laying out all the possible paths can be really painstaking. Of course, you could use a Builder to construct the chains (remember all the Maze building examples from the Gang of Four).
The great thing about CoR is that your code does not become one big god object that has powerless pawns doing each 'step.'
Also, CoR affords a lot of flexibility. If the client comes and says 'OMG, there are two crucial steps missing,' someone can probably add them without destabilizing a lot of other stuff. Single-sourced orchestration is the archetypal god object antipattern, so maybe start with CoR and plan to evacuate if it runs out of rope.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With