当重载在数据类型上不同时,.NET代码重构

时间:2009-11-23 12:15:07

标签: .net refactoring

我有一段代码,其中有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;
    }

UPDATE:

行。看起来我并没有很好地解释我的代码。我将粘贴我所拥有的确切代码,以便您获得更好的图片:

    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;
                    }
            }
        }
    }

我不想修改我正在使用的枚举(所以基本上我想为它添加虚拟条目)。它甚至可能/值得重构这段代码吗?

4 个答案:

答案 0 :(得分:4)

如何在两种方法中使用//Some 20 lines of common codeTwentyLinesOfCommonCode()调用//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)

是否可以创建一个转换器,将TimeSpanEnum转换为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);
    }