1

我有以下枚举(伪代码)

enum RunSequence : int 
{
ABCD = 1 , 
BCDA = 2 , 
DABC = 3 , 
....
}

你明白了......现在,如果每个字母代表大约 4 行代码,那么根据传递的 RunSequence 以所需的顺序构建运行这 16 行代码的逻辑的最聪明的方法是什么?

我完全迷失了......应该通过完全不同的方法来实现吗?我宁愿不使用 goto,而是使用一些 OO 方法......设计模式

4

3 回答 3

0
private static Dictionary<char, Action> funcDic = new Dictionary<char, Action>{
{ 'A', funcA }, { 'B', funcB }, { 'C', funcC }, { 'D', funcD } };

public static void Run(RunSequence sequence){
    foreach (char c in Enum.GetName(typeof(RunSequence), sequence))
        funcDic[c]();
}

其中 funcA..funcD 是包含代码片段的方法。如果需要在代码片段之间传递参数,请将它们放在容器类中并使用 Action<ContainerClass> 而不是 Action<>。

于 2009-07-09T08:33:30.620 回答
0

你们的意思是这样的吗?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ColorsUtility
{
    public abstract class Runner 
    {

        public abstract void Run (string param );
    }

    class ARunner : Runner , ICodeBlockRunner
    {
        public override void Run( string param)
        {
            Console.WriteLine("ARunner Run () ");
        } //eof method 
    } //eof class 

    class BRunner :Runner ,  ICodeBlockRunner
    {
        public override void Run(string param)
        {
            Console.WriteLine("BRunner Run () ");
        } //eof method 
    } //eof class 

    class CRunner : Runner , ICodeBlockRunner
    {
        public override void Run( string param)
        {
            Console.WriteLine("CRunner Run () ");
        } //eof method 
    } //eof class 

    class PermutationRunner
    {

        public PermutationRunner()
        {
            throw new Exception("Need integer for setting the running sequence");

        }

        public PermutationRunner(EnuExecSetter enu)
        {
            this.PopulateRunners(enu);

        }

        private static readonly log4net.ILog logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        public enum EnuExecSetter
        {
            ABC = 1,
            ACB = 2,
            BCA = 3,
            BAC = 4,
            CAB = 5,
            CBA = 6
        } //eof enum 

        static void Main(string[] args)
        {
            string param = "theParam";

            foreach (int val in Enum.GetValues(typeof(EnuExecSetter)))
            {
                Console.WriteLine("Volume Value: {0}\n Member: {1}",
                val, Enum.GetName(typeof(EnuExecSetter), val));
                PermutationRunner pr = new PermutationRunner((EnuExecSetter)val);
                pr.BigRun(param);
                Console.WriteLine("hit a key ");

                Console.ReadLine();


            }


        } //eof method 


        public List<Runner> Runners { get; set; }

        private void PopulateRunners(EnuExecSetter enu)
        {
            string param;
            List<Runner> runners = new List<Runner>();

            switch (enu)
            {
                case EnuExecSetter.ABC:
                    runners.Add(new ARunner());
                    runners.Add(new BRunner());
                    runners.Add(new CRunner());
                    break;
                case EnuExecSetter.ACB:
                    runners.Add(new ARunner());
                    runners.Add(new CRunner());
                    runners.Add(new BRunner());
                    break;
                case EnuExecSetter.BCA:
                    runners.Add(new BRunner());
                    runners.Add(new CRunner());
                    runners.Add(new ARunner());
                    break;
                case EnuExecSetter.BAC:
                    runners.Add(new BRunner());
                    runners.Add(new ARunner());
                    runners.Add(new CRunner());
                    break;
                case EnuExecSetter.CAB:
                    runners.Add(new CRunner());
                    runners.Add(new ARunner());
                    runners.Add(new BRunner());
                    break;
                case EnuExecSetter.CBA:
                    runners.Add(new CRunner());
                    runners.Add(new BRunner());
                    runners.Add(new ARunner());
                    break;
                default:
                    runners.Add(new CRunner());
                    runners.Add(new BRunner());
                    runners.Add(new ARunner());
                    break;

            }
            this.Runners = runners;

        } //eof method 


        public void BigRun(string param)
        {
            foreach (Runner r in this.Runners)
            {
                r.Run(param);
            }
        }

    } //eof class 

public interface ICodeBlockRunner
{
    void Run(string param);

}

} //eof namespace 
于 2009-07-09T11:59:00.953 回答
0

每 4 行代码块都应该在它自己的方法中。就像是

ExecuteCode_A(){/*...*/}

然后您可以创建一个没有参数的委托,称为 CodeExecutor。为每个指向正确方法的代码块实例化一个委托实例。将加载有委托实例的数组以正确的顺序(或列表或其他内容)传递到您的主函数中。遍历它,调用每个委托。


还有另一种类似的方法。创建一个名为 ICodeBlockRunner 的接口,它定义了一个名为 Run() 的方法。对于每个唯一的代码块,创建一个实现接口的类并实现 Run() 方法来执行 4 行代码。然后,您的函数将按所需顺序接受 ICodeBlockRunners(当然是实现 ICodeBlockrunner 的实例化类)的数组(或 Enumerable)。您将遍历这些值并在每个 ICodeBlockRunner 上调用 Run()。

于 2009-07-09T08:31:02.323 回答