简体   繁体   English

多步算法的设计模式

[英]Design Pattern for a multi-step algorithm

I'm writing a console application that goes through an algorithm with N number of steps.我正在编写一个控制台应用程序,它通过一个包含 N 步的算法。 It is important that step N is correctly done before step N+1 is executed.重要的是,步骤N在步骤之前正确地完成N+1被执行。 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).我当然可以使用嵌套的if语句来做到这一点,并使用try-catch-finally (在 finally 中使用 continue 标志来决定程序是否应该处理)。 But I am looking for a better structured design pattern or approach to do this.但我正在寻找一种更好的结构化设计模式或方法来做到这一点。 Any recommendations?有什么建议吗?

The Pipeline design pattern is precisely about this: carrying out a complex process in a strict sequence of steps. Pipeline设计模式正是关于这一点:以严格的步骤顺序执行一个复杂的过程。 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.是 MSDN 上面向编程的介绍性文章, 是一篇更具理论性的文章。

Chain of responsibility责任链

http://www.codeproject.com/Articles/455228/Design-Patterns-3-of-3-Behavioral-Design-Patterns#Chain 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 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.对于状态模式,需要在遇到错误时将状态更改为“Error”,并处理错误状态下的所有错误。

Hopefully, that helps.希望这会有所帮助。

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/case 来以不同的方式处理每一步

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您可以稍后使用 While 来运行每一步

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.在 Java 8 中,您可以使用 Java 8 功能接口来实现这一点。

One example from Java 8 APIs could be the use of a comparator interface. Java 8 API 的一个示例可能是使用比较器接口。

Below is an example of chaining functions using function composition:下面是一个使用函数组合的链接函数的例子:

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

Click here for a detailed article on this.单击此处查看有关此内容的详细文章。

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.这样,如果您的程序在中间被中断,您只需重新启动它,所有对象都可以从它们在此过程中停止的地方重新开始。

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.其中n将是您的工作数据集或当前设置项。 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.此外,23 的模数检查是为了显示何时中断递归检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM