通过枚举器运行排列代码序列的最明智的方法是什么?
我有以下枚举(伪代码)
enum RunSequence : int
{
ABCD = 1 ,
BCDA = 2 ,
DABC = 3 ,
....
}
你明白了...... 现在,如果每个字母代表大约 4 行代码,那么构建根据传递的 RunSequence 以所需顺序运行这 16 行代码的逻辑的最聪明方法是什么?
我完全迷失了......这应该通过完全实现吗?不同的做法? 我宁愿不使用 goto,而是使用一些 OO 方法...设计模式
I have the following enum ( pseudo code )
enum RunSequence : int
{
ABCD = 1 ,
BCDA = 2 ,
DABC = 3 ,
....
}
You get the idea ...
Now if each letter represents some 4 lines of code , what would be the smartest way of building the logic for running those 16 lines of code in the desired sequence according to the RunSequence passed
Am I totally lost ... should this be achieved via totally different approach ?
I d rather not use goto but some OO approach ... design pattern
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
每 4 行代码块都应该在它自己的方法中。 然后
你可以创建一个不带参数的委托,称为 CodeExecutor。 为指向正确方法的每个代码块实例化委托实例。 将按正确顺序(或列表或其他内容)加载委托实例的数组传递到主函数中。 迭代它,调用每个委托。
还有一种类似的方法。 创建一个名为 ICodeBlockRunner 的接口,它定义一个名为 Run() 的方法。 对于每个唯一的代码块,创建一个实现接口的类并实现 Run() 方法来执行 4 行代码。 然后你的函数将按照所需的顺序接受 ICodeBlockRunners(当然是实现 ICodeBlockrunner 的实例化类)的数组(或可枚举的东西)。 您将迭代这些值并在每个 ICodeBlockRunner 上调用 Run()。
Each 4 line chunk of code should be in it's own method. Something like
Then you can create a Delegate with no parameters called CodeExecutor. Instantiate an instance of the delegate for each block of code pointing to the correct method. Pass an array loaded with the delegate instances in the correct order (or list, or something) into your main function. Iterate through it, invoking each delegate.
There is another similar method. Create an Interface called ICodeBlockRunner which defines a method called Run(). For each unique code block, create a class that implements the interface and implement the Run() method to execute the 4 lines of code. Then your function would accept an Array (or something Enumerable) of ICodeBlockRunners (instantiated classes that implement ICodeBlockrunner of course) in the desired order. You'd iterate over the values and call Run() on each ICodeBlockRunner.
其中 funcA..funcD 是包含代码片段的方法。 如果需要在代码片段之间传递参数,请将它们放在容器类中并使用 Action而不是 Action<>。
Where funcA..funcD are the methods containing the code snippets. If you need to pass parameters between the code snippets, place them in a container class and use Action<ContainerClass> instead of Action<>.
你们是不是有这样的意思?
Did you guys mean something like this ?