Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/269.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 代码重构以删除重复代码_C#_Duplicates_Refactoring - Fatal编程技术网

C# 代码重构以删除重复代码

C# 代码重构以删除重复代码,c#,duplicates,refactoring,C#,Duplicates,Refactoring,我在两个类中有三个方法,其中两个方法的代码相同,但继承不同。如何通过添加新类来减少重复代码?我试着用一个简单的例子来简化它。希望代码有意义。我如何重构这个?非常感谢您在这方面的任何帮助 GetConnection.cs public class GetConnection1 : LookupConnection1 { public override int GetConnectionCount() { /* This method

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

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());
    }
}
GetConnection.cs
公共类GetConnection1:LookupConnection1
{
公共覆盖int GetConnectionCount()
{
/*此方法与GetConnection2类文件的方法相同,但从其他类文件继承
名为LookUpConnection1的类
需要重构这个重复的方法*/
int conCount=base.GetConnectionCount();
int值=GetAvailableConnections(conCount);
返回值;
}
专用int GetAvailableConnections(int conCount)
{
/*此方法与GetConnection2中的方法相同。此方法是
GetConnection2类中的精确副本
需要重构这个重复的方法*/
int值=0;
for(int i=0;i

提前感谢,

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

//创建唯一的LookupConnection类
公共类查找连接:BaseConnection
{
//用于保存初始值的私有字段
私有只读整数计数;
//通过构造函数传递期望值。
//这将允许您在每个LookupConnection继承中根据需要传递值。
公共查找连接(整数计数)
{
_计数=计数;
}
//重写要使用的GetConnectionCount方法
//此类中定义的GetAvailableConnections
公共覆盖int GetConnectionCount()
{
返回GetAvailableConnections(\u计数);
}
//如果将GetAvailableConnections放入LookupConnection类中
//你不必担心它会做什么,因为它总是做同样的事情。
//如果以后要更改其行为,请将其设置为虚拟。
受保护的虚拟int GetAvailableConnections(int conCount)
{
int值=0;
for(int i=0;i
我给出的示例只是一个简单的示例,可以解释我面临的问题。在实时中,LookupConnection1和lookupConnection2类(组成名称)中还需要执行其他操作。