【发布时间】:2010-11-28 23:32:00
【问题描述】:
我的任务是重构一个写得不好的 switch 语句(它会使圈复杂度飙升)。简而言之,有一个类可以解析文件以获取各种值。
class foo
{
//a sampling of the fields. Each have their appropriate property
private string _name;
private short _location;
private int _lineNumber;
private List<string> _siblings;
internal foo (StreamReader reader)
{
_siblings = new List<string>()
while (!reader.EndofFile)
{
switch (reader.ReadLine())
{
case "Name":
_name = reader.ReadLine();
break;
case "Location":
_location = short.Parse(reader.ReadLine());
break;
case "Line Number":
_lineNumber = int.Parse(reader.ReadLine());
break;
case "Brother":
case "Sister":
_siblings.Add(reader.ReadLine());
break;
//etc
}
}
}
//Other methods and such
}
我已经阅读了该主题,虽然似乎有很多帮助,但似乎都指向策略设计模式,(我相信)这会过度解决我的问题。在我的项目中,有多个这样的类,其中一些有超过 25 个案例陈述(所以对那些能够提出接口或抽象类的想法的人表示敬意)
我曾考虑过使用John Sonmez 所描述的Dictionary<String, TValue>,但是TValue 会是什么?
任何帮助将不胜感激。
【问题讨论】:
标签: c# dictionary refactoring switch-statement