我不确定我是否滥用这里的枚举。也许这不是最好的设计方法。枚举过度使用?
我有一个枚举,它向执行批处理文件的方法声明了可能的参数。
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]);
}
我猜测后者是更好的方法。
下载者请留下评论。谢谢。 – 2009-06-09 05:55:18
在德国,这被称为“mit Kanonen auf Spatzenschießen”:) – VVS 2009-06-09 06:11:31