Search code examples
c#design-patternssoftware-quality

Best Design Pattern to execute a Series of Steps


I have an application with which a user can initiate (fill out a form and click Run) a series of steps.

First step 1 must be executed and afterwards step 2. If step 2 was successful, execute step 3 otherwise execute step 4. Finally step 5 must be executed.

Each step has about 10 to 100 lines of code.

With which design pattern should I structure my code best in order to implement this logic? Is it the chain of responsibility pattern, template method pattern, or something else altogether?


Solution

    • If the steps in the execution of your code mean different states of the system, in which it reacts differently to incoming messages, then this can be implemented using a State pattern.

    • If you have or will have a hierarchy of classes, each of which performs the same set of steps, but with a slightly different implementation, then the Template pattern is suitable.

    • If it's just a flow of execution consisting of several function calls, then see Olivier’s answer.