首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >FTP文件清理器的清理实现

FTP文件清理器的清理实现
EN

Code Review用户
提问于 2018-05-24 15:52:05
回答 1查看 77关注 0票数 0

我正在重构测试项目中使用的一些实用程序代码模块,并希望应用我对干净的OOP设计模式和坚实原则的知识,使这些模块更有用,更易于使用。

我有一个名为IFtpFileCleaner的接口:

代码语言:javascript
复制
/// 
/// Responsible for deleting a single file located in a remote server
/// 
public interface IFtpFileCleaner
{
    /// 
    /// Deletes a remote file
    /// 
    /// 
    /// The full remote path.
    /// 
    void Clean(string remotePath);
}

我有一个名为RemoteFileCleaner的抽象类,它实现了IFtpFileCleaner

代码语言:javascript
复制
/// 
/// The file cleaner base class provides common code for all the implementations to reduce code duplication
/// 
public abstract class RemoteFileCleaner : IFtpFileCleaner
{
    /// 
    /// The FTP _client.
    /// 
    private readonly IFtpSecureClient _client;

    /// 
    /// Initializes a new instance of the  class.
    /// 
    /// 
    /// The FTP client.
    /// 
    protected RemoteFileCleaner(IFtpSecureClient client)
    {
        _client = client;
    }

    /// 
    /// Deletes a single file located on a remote server
    /// 
    /// 
    /// The full path of the remote file.
    /// 
    public void Clean(string remotePath)
    {
        try
        {
            _client.Init();
            _client.DeleteFile(remotePath);
        }
        catch (Exception e)
        {
            Console.WriteLine($"Failed to clean file {remotePath}.{Environment.NewLine}{e}");
            throw;
        }
        finally
        {
            _client.Disconnect();
        }
        Console.WriteLine($"Remote File {remotePath} was deleted.");
    }
}

现在,我可以有我的文件清理器的具体实现。比如SFTPFileCleanerFTPFileCleanerFTPSFileCleaner。它们看起来都很相似。例如,SFTPFileCleaner如下所示:

代码语言:javascript
复制
/// 
/// The SFTP implementation of .
/// 
public class SFTPFileCleaner : RemoteFileCleaner
{
    /// 
    /// Initializes a new instance of the  class.
    /// 
    public SFTPFileCleaner()
        : base(new FtpSecureClientTestFactory().CreateSFTPClient())
    {
    }
}

如您所见,我在具体实现中执行"Bastard注入“,因为我希望在测试方法中很容易地实例化这些清洁剂。下面是一个示例:

代码语言:javascript
复制
[TearDown]
public void Cleanup()
{
    _ftpCleaner = new SFTPFileCleaner();
    _ftpCleaner.Clean(_expectedRemotePath);
}
  1. 你们觉得这个设计怎么样?它干净易用吗?
  2. 还能再改进吗?
  3. 这个设计模式有名字吗?在我看来,似乎是正面模式(Bastard注入)和策略模式的混合。我说的对吗?
EN

回答 1

Code Review用户

发布于 2018-05-24 16:06:11

IRemoteFileCleaner作为基类,并将FTPFileCleaner (从IRemoteFileCleaner继承来作为所有其他类继承的类)难道不是更有意义的吗?

票数 1
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/195097

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档