【发布时间】:2011-09-30 20:26:53
【问题描述】:
我正在尝试制作一个应用程序,让用户根据他们的预测和实际结果填写预测和得分。
我有一个称为 GetPointsPrediction() 的方法。目前,预测具有以下形式:
Dictionary<int, Driver> predictions = new Dictionary<int, Driver>
{
{1, new Driver(10, "Michael Schumacher")},
{2, new Driver(8, "Jensen Button")},
{3, new Driver(7, "Felipe Massa")},
{4, new Driver(9, "Fernando Alonso")}
};
整数是用户认为驱动程序将完成的位置。现在,我需要能够根据预测和结果计算点数。要打分,我需要在结果中包含三个信息:位置、驱动程序和积分。
准确地在他们预测的位置完成比赛的车手获得满分。未在预测位置完成但进入前十名的车手将被扣分。
你看到的所有序号都是位置。
解决方案一:
有一个单独的结果集和一个字典,其中包含每个位置的点,将用作查找:
Dictionary<int, Driver> results = new Dictionary<int, Driver>
{
{1, new Driver(10, "Michael Schumacher")},
{2, new Driver(8, "Jensen Button")},
{3, new Driver(9, "Fernando Alonso")}
};
Dictionary<int, int> points = new Dictionary<int, int>
{
{1, 25},
{2, 18},
{3, 15},
{4, 12},
{5, 10}
};
解决方案 2:
合并点和结果。
Dictionary<int, Dictionary<int, Driver>> results = new Dictionary<int, Dictionary<int, Driver>>
解决方案 3:
想出某种可以容纳一切的类:
public class DriverResult
{
public Driver Driver { get; private set; }
public int Points { get; private set; }
public int StartPosition { get; private set; }
public int FinishPosition { get; private set; }
}
然后
IEnumerable<DriverResult> raceResults = new List<DriverResult>
我喜欢解决方案 3,但我觉得它不够连贯,名字也感觉不好。解决方案 2 可能很难真正使用,而解决方案 1 实际上为我提供了一种很好的方法,可以将正确预测的驱动程序与 Intersect 分开。
也许还有其他我没有想到的解决方案。关于这些设计决策的最佳实践是什么?
驱动类:
public class Driver : IEquatable<Driver>
{
public int DriverId { get; private set; }
public string Name { get; private set; }
public Driver(int driverId, string name)
{
DriverId = driverId;
Name = name;
}
public override bool Equals(object obj)
{
return Equals(obj as Driver);
}
public bool Equals(Driver other)
{
return other != null && other.DriverId == DriverId;
}
public override int GetHashCode()
{
return DriverId.GetHashCode();
}
}
【问题讨论】:
-
我不太清楚你在做什么。 Driver 类中有什么?为什么你所有的索引都是连续的?如果是,为什么不使用数组而不是字典呢?例如,int points[] = new int[] { 25, 18, 15, 12, 10 };
-
我添加了驱动程序类并解释了顺序的内容(它们是完成位置);我需要一本字典(我认为),因为我需要例如属于某个位置的点。
-
但如果位置是连续的,那么您将使用数组而不是字典。字典(或 SortedLists)适用于数据稀疏且需要查找的情况。例如,您可以为自己的驱动程序使用字典,这样您就可以按 ID 查找,因为 ID 可能是 1 或 1002。
-
这对于 Stack Overflow 来说有点过于本地化了。
标签: c# architecture c#-4.0 software-design