多步骤算法的设计模式 [英] Design Pattern for a multi-step algorithm
问题描述
我正在编写一个控制台应用程序,该应用程序通过N个步骤来完成算法。重要的是在执行步骤 N + 1
之前正确完成步骤 N
。否则,程序应停止处理并显示错误消息。
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.
我当然可以使用嵌套的如果
语句来做到这一点,并使用 try-catch-最终
(使用finally标志来决定程序是否应该处理)。但是我正在寻找一种更好的结构化设计模式或方法来做到这一点。有什么建议吗?
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?
推荐答案
我曾经创建了一个控制自动化的过程,并且使用了所有步骤的枚举
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:
}
您以后可以使用While运行每一步
You may later use a While to run every step
这篇关于多步骤算法的设计模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!