代码之家  ›  专栏  ›  技术社区  ›  Coding Monkey

Enum是来这里的正确方式吗?

  •  2
  • Coding Monkey  · 技术社区  · 16 年前

    我不确定我是否在这里滥用枚举。也许这不是最好的设计方法。

    我有一个枚举,它声明了执行批处理文件的方法的可能参数。

    public enum BatchFile
    {
        batch1,
        batch2
    }
    

    然后我有了自己的方法:

    public void ExecuteBatch(BatchFile batchFile)
    {
        string batchFileName;
        ...
        switch (batchFile)
            {
                case BatchFile.batch1:
                    batchFileName = "Batch1.bat";
                    break;
                case BatchFile.batch2:
                    batchFileName = "Batch2.bat";
                    break;
                default:
                    break;
            }
        ...
        ExecuteBatchFile(batchFileName);
    }
    

    所以我想知道这是不是声音设计。

    Dictionary<BatchFile, String> batchFileName = new Dictionary<BatchFile, string>();
    batchFileName.Add(BatchFile.batch1, "batch1.bat");
    batchFileName.Add(BatchFile.batch2, "batch2.bat");
    

    然后,我不会使用switch语句,而是直接执行:

    public void ExecuteBatch(BatchFile batchFile)
    {
        ExecuteBatchFile(batchFileName[batchFile]);
    }
    

    我想后者是更好的方法。

    10 回复  |  直到 4 年前
        1
  •  8
  •   Mitch Wheat    16 年前

    我可能会选择这样的设计:

    public interface IBatchFile
    {
        void Execute();
    }
    
    public class BatchFileType1 : IBatchFile
    {
        private string _filename;
    
        public BatchFileType1(string filename)
        {
            _filename = filename;
        }
    
        ...
    
        public void Execute()
        {
            ...
        }
    }
    
    public class BatchFileType2 : IBatchFile
    {
        private string _filename;
    
        public BatchFileType2(string filename)
        {
            _filename = filename;
        }
    
        ...
    
        public void Execute()
        {
            ...
        }
    }
    

    事实上,我会将任何常见功能提取到 BatchFile

        2
  •  4
  •   VVS    16 年前

    如果你突然需要第三批文件怎么办?你必须修改你的代码,重新编译你的库,每个使用它的人都必须这样做。

        3
  •  3
  •   Mark Cidade    16 年前

    在这种情况下,我个人会使用静态常量类:

    public static class BatchFiles
     { 
       public const string batch1 = "batch1.bat";
       public const string batch2 = "batch2.bat"; 
     }
    
        4
  •  3
  •   Mark    16 年前

    using System;
    
    public enum BatchFile
    {
        [BatchFile("Batch1.bat")]
        batch1,
        [BatchFile("Batch2.bat")]
        batch2
    }
    
    public class BatchFileAttribute : Attribute
    {
        public string FileName;
        public BatchFileAttribute(string fileName) { FileName = fileName; }
    }
    
    public class Test
    {
        public static string GetFileName(Enum enumConstant)
        {
            if (enumConstant == null)
                return string.Empty;
    
            System.Reflection.FieldInfo fi = enumConstant.GetType().GetField(enumConstant.ToString());
            BatchFileAttribute[] aattr = ((BatchFileAttribute[])(fi.GetCustomAttributes(typeof(BatchFileAttribute), false)));
            if (aattr.Length > 0)
                return aattr[0].FileName;
            else
                return enumConstant.ToString();
        }
    }
    

    string fileName = Test.GetFileName(BatchFile.batch1);
    
        5
  •  2
  •   JaredPar    16 年前

    我认为后一种方法更好,因为它将担忧分开。您有一个方法专门用于将枚举值与物理路径相关联,还有一个单独的方法用于实际执行结果。第一次尝试将这两种方法稍微混合在一起。

    然而,我认为使用switch语句来获取路径也是一种有效的方法。枚举在很多方面都是用来打开的。

        6
  •  1
  •   Ken McCormack Ken McCormack    16 年前

    如果您不需要在不重新编译/重新部署应用程序的情况下添加新的批处理文件,则使用枚举是可以的。..然而,我认为最灵活的方法是在配置中定义一个键/文件名对列表。

        7
  •  0
  •   Erich Kitzmueller    16 年前

    ExecuteBatch真的有必要只处理有限数量的可能文件名吗? 你为什么不去呢

    public void ExecuteBatch(string batchFile)
    {
        ExecuteBatchFile(batchFile);
    }
    
        8
  •  0
  •   Jay Atkinson    16 年前

    后一种情况的问题是,如果传递了一个不在字典内的无效值。switch语句中的默认值提供了一种简单的方法。

    但是。..如果你是枚举,它将有很多条目。字典可能是更好的选择。

    无论哪种方式,我都会推荐一些方法来保护输入值,即使在ammoQ的答案中也不会导致严重错误。

        9
  •  0
  •   jrharshath    16 年前

    第二种方法更好,因为它将批处理文件对象(枚举)与字符串链接起来。。

    但说到设计,将枚举和字典分开并不是很好;您可以将其视为一种替代方案:

    public class BatchFile {
        private batchFileName;
    
        private BatchFile(String filename) {
            this.batchFileName = filename;
        }
        public const static BatchFile batch1 = new BatchFile("file1");
        public const static BatchFile batch2 = new BatchFile("file2");
    
        public String getFileName() { return batchFileName; }
    }
    

    您可以选择将构造函数保持为私有,也可以将其设置为公共。

        10
  •  0
  •   Guffa    16 年前

    第一种解决方案(开关)简单明了,你真的不必让它比这更复杂。

    public class BatchFile {
    
       private string _fileName;
    
       private BatchFile(string fileName) {
          _fileName = fileName;
       }
    
       public BatchFile Batch1 { get { return new BatchFile("Batch1.bat"); } }
       public BatchFile Batch2 { get { return new BatchFile("Batch2.bat"); } }
    
       public virtual void Execute() {
          ExecuteBatchFile(_fileName);
       }
    
    }
    

    用途:

    BatchFile.Batch1.Execute();