我有一段代码,其中有2个重载因参数的数据类型而不同,并且具有不同的参数数量。我希望减少代码重复。有什么方法可以做到这一点?
我的代码如下所示:
public void publicMethod(double[,] data, double increment)
{
helperMethod1(data, increment);
}
public void publicMethod(double[,] data, TimeSpan increment, Enum someEnum)
{
helperMethod2(data, increment, someEnum);
}
//The helper methods are in a different class
internal void helperMethod1(double[,] data, double increment)
{
//Some 20 lines of common code
for (int i = 0; i < data.GetLength(0); ++i)
{
TargetFunction((double[])GetRow(i, data), increment);
}
//Some more common code
}
internal void helperMethod1(double[,] data, TimeSpan increment, Enum someEnum)
{
//Some 20 lines of common code
for (int i = 0; i < data.GetLength(0); ++i)
{
TargetFunction((double[])GetRow(i, data), increment, someEnum);
}
//Some more common code
}
public static double[] GetRow(int rowIndex, double[,] array)
{
double[] row = new double[array.GetLength(1)];
Buffer.BlockCopy(array, array.GetLength(1) * rowIndex * 8, row, 0, array.GetLength(1) * 8);
return row;
}
行。看起来我并没有很好地解释我的代码。我将粘贴我所拥有的确切代码,以便您获得更好的图片:
public class PublicClass
{
public void PlotXAppendMultiple(double[,] xData, DataOrientation orientation, TimeSpan increment, PrecisionMode precisionMode)
{
HelperClass.PlotXAppendMultiple(xData, orientation, increment, precisionMode);
}
public void PlotXAppendMultiple(double[,] xData, DataOrientation orientation, double increment)
{
HelperClass.PlotXAppendMultiple(xData, orientation, increment);
}
}
internal class HelperClass
{
public virtual void PlotXAppendMultiple(double[,] xData, DataOrientation orientation, double increment)
{
switch (orientation)
{
case (DataOrientation.DataInRows):
{
for (int i = 0; i < xData.GetLength(0); ++i)
{
TargetFunction((double[])GetRow(i, xData), increment);
}
break;
}
case (DataOrientation.DataInColumns):
{
for (int i = 0; i < xData.GetLength(1); ++i)
{
TargetFunction((double[])GetColumn(i, xData), increment);
}
break;
}
default:
{
break;
}
}
}
public virtual void PlotXAppendMultiple(double[,] xData, DataOrientation orientation, TimeSpan increment, PrecisionMode precisionMode )
{
switch (orientation)
{
case (DataOrientation.DataInRows):
{
for (int i = 0; i < xData.GetLength(0); ++i)
{
TargetFunction((double[])GetRow(i, xData), increment, precisionMode);
}
break;
}
case (DataOrientation.DataInColumns):
{
for (int i = 0; i < xData.GetLength(1); ++i)
{
TargetFunction((double[])GetColumn(i, xData), increment, precisionMode);
}
break;
}
default:
{
break;
}
}
}
}
我不想修改我正在使用的枚举(所以基本上我想为它添加虚拟条目)。它甚至可能/值得重构这段代码吗?
答案 0 :(得分:4)
如何在两种方法中使用//Some 20 lines of common code
和TwentyLinesOfCommonCode()
调用//Some more common code
替换SomeMoreCommonCode()
?你只会保留非公共代码。
答案 1 :(得分:0)
我将从decimal参数创建一个TimeSpan
对象,并为该新TimeSpan
对象提供一个'默认'Enum值,该方法采用3个参数。
答案 2 :(得分:0)
在枚举中输入“NotSet”或“Unknow”。 TargetFunction2必须意识到它并调整它的行为。只保留helperMethod1和3个args签名并调整第一个方法
public void publicMethod(double[,] data, double increment)
{
helperMethod1(data, increment, Enum.Unknow);
}
答案 3 :(得分:0)
是否可以创建一个转换器,将TimeSpan
和Enum
转换为Double
参数,反之亦然?
如果是,那么您可以先定义这样的辅助方法
public double Convertor(TimeSpan ts, Enum enum)
{
}
然后编写以下代码:
//The helper methods are in a different class
internal void helperMethod1(double[,] data, double increment)
{
//Some 20 lines of common code
for (int i = 0; i < data.GetLength(0); ++i)
{
TargetFunction1(data, increment);
}
//Some more common code
}
internal void helperMethod1(double[,] data, TimeSpan increment, Enum someEnum)
{
helperMethod1(data, Convertor(increment, someEnum);
}