我有一个必须完成的繁重计算算法。为了使其尽可能高效,我根据参数的数量将其分为五种方法。这使我能够尽可能地节省开支。也就是说,我不希望我的用户知道这一点。
public AnalysisResult[] PerformAnalysis(double[] Inputs, double[] Outputs, int[][] Coefficients)
{
AnalysisResult[] AR = new AnalysisResult[Coefficients.Length];
for (int i = 0; i < Coefficients.Length; i++)
AR[i] = GetResults(Inputs, Outputs, /* Based on length of Coefficients[i]*/ );
return AR;
}
private AnalysisResult GetResults(double[] Inputs, double[] Outputs, int A)
{
// Do stuff
}
private AnalysisResult GetResults(double[] Inputs, double[] Outputs, int A, int B)
{
// Do stuff
}
private AnalysisResult GetResults(double[] Inputs, double[] Outputs, int A, int B, int C)
{
// Do stuff
}
private AnalysisResult GetResults(double[] Inputs, double[] Outputs, int A, int B, int C, int D)
{
// Do stuff
}
private AnalysisResult GetResults(double[] Inputs, double[] Outputs, int A, int B, int C, int D, int E)
{
// Do stuff
}
如您所见,我只希望用户可以使用一种公共方法来调用算法。我想知道是否有一种简单的方法可以根据传入的数组的长度来确定要调用哪个私有方法PerformAnalysis
?
有什么建议么?
到目前为止,我的方法是基于数组长度的 switch 语句,但我认为有更好的方法。
public AnalysisResult[] PerformAnalysis(double[] Inputs, double[] Outputs, int[][] Coefficients)
{
AnalysisResult[] AR = new AnalysisResult[Coefficients.Length];
for (int i = 0; i < Coefficients.Length; i++)
switch (Coefficients[i].Length)
{
case 1:
AR[i] = GetResults(Inputs, Outputs, Coefficients[i][0]);
break;
case 2:
AR[i] = GetResults(Inputs, Outputs, Coefficients[i][0], Coefficients[i][1]);
break;
// Etc
}
return AR;
}