Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which design pattern to use on Java workflow

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..
like image 743
user1851366 Avatar asked Aug 31 '25 02:08

user1851366


1 Answers

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.

like image 96
Rob Avatar answered Sep 02 '25 15:09

Rob