【问题标题】:Code Refactoring to remove the duplicate code代码重构以删除重复代码
【发布时间】:2021-02-10 17:28:08
【问题描述】:

我在两个类中有三个方法,其中两个方法具有相同的代码但具有不同的继承。如何通过添加新类来减少重复代码?我试图用一个简单的示例来简化它。希望代码有意义。我该如何重构呢?非常感谢您对此的任何帮助。

GetConnection.cs
public class GetConnection1 : LookupConnection1
    {
        public override int GetConnectionCount()
        {
             /* This method is same as that of the GetConnection2 class file but inherits from other 
                class named LookUpConnection1
                Need to refactor this duplicate method */

            int conCount = base.GetConnectionCount();
            int value = GetAvailableConnections(conCount);
            return value;
        }
        private int GetAvailableConnections(int conCount)
        {
            /* This method is same as that in GetConnection2. This method is 
               exact replica that is in GetConnection2 class
               Need to refactor this duplicate method */

            int value = 0;
            for (int i = 0; i < conCount; i++)
                value = GetConnection(value);
            return value;
        }
        private int GetConnection(int value)
        {
            /* This is the method which differs from the GetConnection2 class. */

            return value + 10;
        }
    }

GetConnection2 class:
    public class GetConnection2 : LookUpConnection2
    {
        public override int GetConnectionCount()
        {
          /* This method is same as that of the GetConnection1 class file but inherits from other 
             class named LookUpConnection2
             Need to refactor this method*/

        int conCount = base.GetConnectionCount();
        int value = GetAvailableConnections(conCount);
        return value;
    }

    private int GetAvailableConnections(int conCount)
    {
        /* This method is same as that in GetConnection1. This method is 
           exact replica that is in GetConnection1 class
           Need to refactor this duplicate method */

        int value = 0;
        for (int i = 0; i < conCount; i++)
            value = GetConnection(value);
        return value;
    }

    private int GetConnection(int value)
    {
        /* This is the method which differs from the GetConnection2 class. */
        return value + 30;
    }
}

LookupConnection1 class file
public class LookupConnection1 : BaseConnection
{
    public override int GetConnectionCount()
    {
        return 20;
    }
}

LookUpConnection2 class file
public class LookUpConnection2 : BaseConnection
{
    public override int GetConnectionCount()
    {
        return 10;
    }
}

BaseConnection class file
public abstract class BaseConnection
{
    public abstract int GetConnectionCount();
}

public class Program
{
    static void Main()
    {
        GetConnection1 connection1 = new GetConnection1();
        GetConnection2 connection2 = new GetConnection2();
        Console.Write(connection1.GetConnectionCount());
        Console.Write(connection2.GetConnectionCount());
    }
}

提前致谢,

【问题讨论】:

  • 我提供的示例只是一个简单的示例,可以解释我面临的问题。实时在 LookupConnection1 和 lookupConnection2 类(组成名称)中完成了其他需要的操作。

标签: c# duplicates refactoring


【解决方案1】:

您必须创建一个唯一的 LookupConnection 类并将 GetAvailableConnectionsGetConnection 方法移动到 LookupConnection 类。下面是重构后的代码,希望对你有帮助!!!

// Create a unique LookupConnection class
public class LookupConnection : BaseConnection
{
  // Private field to hold the initial value
  private readonly int _count;

  // Pass the expected value through the constructor.
  // That will allow you to pass values as needed in each LookupConnection inheritance.
  public LookupConnection(int count)
  {
    _count = count;
  }

  // Override GetConnectionCount method  to use 
  // the GetAvailableConnections defined in this class
  public override int GetConnectionCount()
  {
    return GetAvailableConnections(_count);
  }

  // If you put GetAvailableConnections in the LookupConnection class
  // you don't need to worry about what it does, because always it does the same.
  // Make it virtual if later you want change its behavior.
  protected virtual int GetAvailableConnections(int conCount)
  {
    int value = 0;
    
    for (int i = 0; i < conCount; i++)
        value = GetConnection(value);

    return value;
  }
  
  // Add the GetConnection to the LookupConnection and make it 
  // virtual in order to override in the different GetConnection classes
  protected virtual int GetConnection(int value)
  {
      return value;
  }
}

// This is how your class will look like
public class GetConnection1 : LookupConnection
{
  // Pass the value to the base constructor as needed
  public GetConnection1 () : base(20)
  {

  }

  // Here you can override the GetConnection and add the particular behavior for this class
  protected override int GetConnection(int value)
  {
      return value + 10;
  }
}

// This is how your class will look like
public class GetConnection2 : LookUpConnection
{
  // Pass the value to the base constructor as needed
  public GetConnection2 () : base(10)
  {

  }

  // Here you can override the GetConnection and add the particular behavior for this class
  protected override int GetConnection(int value)
  {
      return value + 30;
  }
}

public class Program
{
    static void Main()
    {
        GetConnection1 connection1 = new GetConnection1();
        GetConnection2 connection2 = new GetConnection2();
        Console.Write(connection1.GetConnectionCount());
        Console.Write(connection2.GetConnectionCount());
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-12
    • 1970-01-01
    • 2021-09-11
    • 1970-01-01
    • 2013-03-26
    • 1970-01-01
    相关资源
    最近更新 更多