Which design template is best used to control the sequence of steps?

I have a winform tool that interacts with hardware using a serial port.

The serial port is used to send commands to hardware that will acknowledge commands and sometimes return data.

To complete the task, several commands must be sent to the hardware in a specific order.

I have successfully implemented the tool using the switch statement to control the sending of commands. However, although this works, I cannot help but think that there is a better, easier way to do this - is there?

The current implementation is below:

Each case is a team that requires sending to the equipment. The Send method is passed the calling method and the identifier of the next step - which will be returned if the command was sent successfully.

  • The tool sends a session start command to the hardware.
  • Hardware confirms the command.
  • The tool sends a command to set the direction to the hardware.
  • Hardware will confirm the command.

and etc.

MessageID nextStep = MessageID.IMS;

while (nextStep != MessageID.Stop)
{
    switch (nextStep)
    {
        case MessageID.ISS:
            nextStep = Send( new ISS_StartSession(), MessageID.IE386);
            RaiseProgressEvent(10); //percentage complete
            break;

        case MessageID.IE386:
            nextStep = Send( new IE386_SetDirection(Direction.BOTH), MessageID.IE378);
            RaiseProgressEvent(20);
            break;

       //etc

       case MessageID.Error:
            HandleError(); //abort task if necessary
            break;
    }
 }

Is there a better way to do this ??

Is there a well-known design that I should look at?

+5
source share
4 answers

, / , . , , .

+1

I think State is the DP you are looking for.

+3
source

I would suggest a chain of custody scheme, as steps can be constrained and you can control the links in the chain at the client level.

0
source

All Articles