disasterkid
disasterkid

Reputation: 7278

Design Pattern for a multi-step algorithm

I'm writing a console application that goes through an algorithm with N number of steps. It is important that step N is correctly done before step N+1 is executed. Otherwise the program should stop working with an error message.

I can do this with nested if statements of course and use try-catch-finally (using a continue flag in finally to decided if the program should process). But I am looking for a better structured design pattern or approach to do this. Any recommendations?

Upvotes: 6

Views: 17348

Answers (6)

Bikas Katwal
Bikas Katwal

Reputation: 2035

You can use either:

  1. Chain Of Responsibility. OR
  2. Maintain a List of Processes and loop through them to process your task. OR
  3. Use Function composition and chain your functions. In java 8 you can achieve this using Java 8 functional interfaces.

One example from Java 8 APIs could be the use of a comparator interface.

Below is an example of chaining functions using function composition:

Comparator.comparing(name).thenComparing(age).

Click here for a detailed article on this.

Upvotes: 2

Tim Hong
Tim Hong

Reputation: 2764

Chain of responsibility

http://www.codeproject.com/Articles/455228/Design-Patterns-3-of-3-Behavioral-Design-Patterns#Chain

or State pattern

http://www.codeproject.com/Articles/455228/Design-Patterns-3-of-3-Behavioral-Design-Patterns#State

may be the solutions of your problem.

For chain of responsibility pattern, when you detect error, you just need set the "error message (handling)" process as the next process in the chain.

For state pattern, you need to change the state to "Error" when encountering error, and handle all the errors in the error state.

Hopefully, that helps.

Upvotes: 7

CesarGon
CesarGon

Reputation: 15335

The Pipeline design pattern is precisely about this: carrying out a complex process in a strict sequence of steps. Google "pipeline design pattern" and you'll find plenty of resources.

This is a programming-oriented introductory article on MSDN, and this is a more theoretical post.

Upvotes: 11

Menelaos Vergis
Menelaos Vergis

Reputation: 3955

I have once created an process that was controlling an automation and I used an enumeration with all the steps

enum AutomationStep{Requested, Started, Waiting, Processing, Terminating};

Later I created a switch/case to process every step differently

switch (currentStep)
{
  case AutomationStep.Requested : InitializeProcess(); currentstep = AutomationStep.Started; break;
  case AutomationStep.Started : StartTheEngines(); currentstep = AutomationStep.Waiting; break;
  case AutomationStep.Waiting : //etc
   break;
   default:
}

You may later use a While to run every step

Upvotes: 3

Travis J
Travis J

Reputation: 82277

Use recursion and return or stop when you hit an incorrect step

public void Process(int n)
{
 if( n % 23 != 0 )return;
 Process(n+1);
}

Where n is going to be your working data set or current set item. It is up to you to determine a data structure to use for that. Also, the modulus check for 23 is to show when to break from the recursive checks.

Upvotes: 0

mbeckish
mbeckish

Reputation: 10579

One pattern I like is to update the object's state as it progresses from one step to the next, to indicate where it is in the process.

Rather than process one object from beginning to end, I have each step of the algorithm select all of the objects at a given state, process them, and update their state to be ready for the next step.

I make each step of the process a transaction, so that an object has either fully progressed to the next step, or is rolled back to its prior state and is ready to go through this step again.

That way, if your program is interrupted in the middle, you can just start it back up, and all objects can pick up right where they left off in the process.

Upvotes: 0

Related Questions