我的任务是重构一个写得不好的开关语句(它使循环复杂性尖峰化)。总之,有一个类为不同的值分析文件。将数据重构为不同类型数据的开关语句
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个以上的case语句(对于那些能够想出和界面或抽象类的人的赞誉)
我曾考虑过使用Dictionary<String, TValue>
,如John Sonmez所述,但那么TValue会是什么?
任何帮助将不胜感激。
我想你可能误会我了。如果策略是最好的方法,那么通过一切手段我会使用它! – 2010-11-28 23:42:50