通过枚举器运行排列代码序列的最明智的方法是什么?

发布于 2024-07-26 17:41:01 字数 280 浏览 2 评论 0原文

我有以下枚举(伪代码)

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

屋顶上的小猫咪 2024-08-02 17:41:02

每 4 行代码块都应该在它自己的方法中。 然后

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

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


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

Each 4 line chunk of code should be in it's own method. Something like

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

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.

客…行舟 2024-08-02 17:41:02
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而不是 Action<>。

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]();
}

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<>.

七秒鱼° 2024-08-02 17:41:02

你们是不是有这样的意思?

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 

Did you guys mean something like this ?

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 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文