jeudi 23 février 2023

Translate workflow into state machine with respect to SOLID and OOP

Lets say you start with a method that accepts two complex objects. This method is some sort handler and it needs to process workflow (see workflow flowchart)

public Task Handler(Object A, List<object> lisftOfObjectsB)
{
    //execute workflow steps here 
}

You can do this by using statements like 'if', 'if-else', 'switch' etc. But if you program it that way you will end up with messy code and you will probably violate at least one of SOLID principles (open-close principle for example). How can you program a workflow with respect to SOLID principles and using OOP instead of using many different if, if-else, switch ect statements ? enter image description here

public Task Handler(Object A, List<object> lisftOfObjectsB)
{
    bool inDb = IsAInDatabase();
    if(inDb == false)
    {
         //Add to DB
    }
    else
    {
         bool hasLastState = CheckForLastState(A);
         if(hasLastState == false)
         {
             //Add laststate
         }
    }
    ....
 }

If you do it this way you will end up with many different if/else/for/for-each statements and imagine if workflow will have much more steps and YES/NO decisions.

Aucun commentaire:

Enregistrer un commentaire