Hank Tuff
Hank Tuff

Reputation: 193

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?

Upvotes: 2

Views: 6837

Answers (2)

Olivier Jacot-Descombes
Olivier Jacot-Descombes

Reputation: 112342

A few method calls and an if-else-statement should do what you want.

Step1();
if (Step2()) {
    Step3();
} else {
    Step4();
}
Step5();

Just let Step2 return a Boolean to tell if it was successful. If step 2 returns some data, you can also return null or an empty collection if the operation was not successful instead.

public bool Step2()
{
    //do things
    return <whether successful>;
}

This is simple and easy to understand.

Of course these methods can be implemented in different services for a better separation of concerns. E.g.

_ui.ShowWaitCursor(); // Step 1.
var data = _fileService.GetData(); // Step 2.
if (data != null) { // Check success of step 2.
    _dbService.WriteData(data); // Step 3.
} else {
    _dbService.WriteDefaults(); // Step 4.
}
_ui.HideWaitCursor(); // Step 5.

See also: Dependency injection (Wikipedia). If you are looking for a better way to organize your code, this is worth looking at.

Upvotes: 3

alex_noname
alex_noname

Reputation: 32133

  • 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.

Upvotes: 12

Related Questions