什么是合适的瘦身方式多种相似的方法

本文关键字:相似 方法 方式多 瘦身 什么 | 更新日期: 2023-09-27 18:21:37

假设我有两个类似的方法,名为GetFiles()和PutFiles()

    public static void GetFiles(string source, string dest)
    {
        using (Session session = new Session())
        {
            session.Open(sessionOptions);
            TransferOptions transferOptions = new TransferOptions();
            transferOptions.TransferMode = TransferMode.Binary;
            TransferOperationResult transferResult;
            ////Different in here////
            transferResult = session.GetFiles(source, dest, false, transferOptions);
            transferResult.Check();
            foreach (TransferEventArgs transfer in transferResult.Transfers)
            {
                Console.WriteLine("Transfer file from {0} to {1}", source, dest);
            }
        }
    }
    public static void PutFiles(string source, string dest)
    {
        using (Session session = new Session())
        {
            session.Open(sessionOptions);
            TransferOptions transferOptions = new TransferOptions();
            transferOptions.TransferMode = TransferMode.Binary;
            TransferOperationResult transferResult;
            ////Different in here////
            transferResult = session.PutFiles(source, dest, false, transferOptions);
            transferResult.Check();
            foreach (TransferEventArgs transfer in transferResult.Transfers)
            {
                Console.WriteLine("Transfer file from {0} to {1}", source, dest);
            }
        }
    }

你可以看到它们完全相同,只是在方法内部调用了不同的函数我已经考虑添加一个名为Transfer File的新方法,它接受一个参数来控制put File/get File 的过程

    public static void TransferFile(string source, string dest, bool GetFile)
    {
        using (Session session = new Session())
        {
            session.Open(sessionOptions);
            TransferOptions transferOptions = new TransferOptions();
            transferOptions.TransferMode = TransferMode.Binary;
            TransferOperationResult transferResult;
            if(GetFile)
            transferResult = session.GetFiles(source, dest, false, transferOptions);
            else
            transferResult = session.PutFiles(source, dest, false, transferOptions);
            transferResult.Check();
            foreach (TransferEventArgs transfer in transferResult.Transfers)
            {
                Console.WriteLine("Transfer file from {0} to {1}", source, dest);
            }
        }
    }

但后来我发现这不是一个最好的解决方案,因为我不能处理超过2个条件(获取文件和放置文件)

什么是合适的瘦身方式多种相似的方法

你可以这样使用

public static void Get_Or_Put_Files(string source, string dest, bool isFilesAreGetting)
{
    using (Session session = new Session())
    {
        session.Open(sessionOptions);
        TransferOptions transferOptions = new TransferOptions();
        transferOptions.TransferMode = TransferMode.Binary;
        TransferOperationResult transferResult;
        if(isFilesAreGetting)
        {
            transferResult = session.GetFiles(source, dest, false, transferOptions);
        }
        else
        {
            transferResult = session.PutFiles(source, dest, false, transferOptions);
        }
        transferResult.Check();
        foreach (TransferEventArgs transfer in transferResult.Transfers)
        {
            Console.WriteLine("Transfer file from {0} to {1}", source, dest);
        }
    }
}

只需添加一个额外的参数来判断此方法是Getting还是Putting。