首页 > 解决方案 > 对此处使用的适当设计模式感到困惑

问题描述

我正在实现一个可以从各种来源(如 ftp、http 等)下载文件的类。我从以下界面开始

class IInternetFileDownloader
{
public:
    IInternetFileDownloader() = default;

    virtual void Download(const std::string& urlToDownloadFrom) = 0;

};

然后我实现了从适当的端点执行实际下载的类。所以我有一个 HttpFileDownloader.h 如下

#include "IInternetFileDownloader.h"
class HttpFileDownloader : public IInternetFileDownloader
{
public:
    HttpFileDownloader() = default;

    virtual void Download(const std::string& urlToDownloadFrom)
    {
        // download implementation
    }

};

所以我有一个 FtpFileDownloader .h 如下

#include "IInternetFileDownloader.h"
class FtpFileDownloader : public IInternetFileDownloader
{
public:
    FtpFileDownloader() = default;

    virtual void Download(const std::string& urlToDownloadFrom)
    {
        // download implementation
    }

};

我可以调用适当的类,如下所示

#include "IInternetFileDownloader.h"
#include "HttpFileDownloader.h"

int main()
{
    std::string downloadFromUrl = "http://www.example.org/xyz.zip";
    IInternetFileDownloader* download = new HttpFileDownloader();
    download->Download(downloadFromUrl);
}

但是,我不想在这里实例化特定的 HttpFileDownloader 或 FtpFileDownloader。在我看来,应该有另一个类可以只获取 url,并且根据协议,它可以构造适当的类。这样客户端代码(main.cpp)就不需要担心适当类的实例化。我阅读了有关工厂和构建器设计模式的信息,但对于在这种情况下哪个最适合使用有点困惑?

标签: c++design-patternsfactorybuilder

解决方案


最简单的方法是在 IInternetFileDownloader 上有一个静态函数来实例化正确的子类。

另外,我认为您不需要基类中的默认构造函数,但您可能需要基类中的默认析构函数是虚拟的。这样,CreateDownloader我建议的工厂函数 可以将指针(或 shared_ptr)返回到IInternetFileDownloader您稍后删除的实例。

class IInternetFileDownloader
{
public:
    virtual ~IInternetFileDownloader() = default;
    virtual void Download(const std::string& urlToDownloadFrom) = 0;

    // parses the url to infer the protocol and construct an instance of a derived class
    static IInternetFileDownloader* CreateDownloader(const std:string& url);
};

class HttpFileDownloader : public HttpFileDownloader 
{
public:
    virtual void Download(const std::string& urlToDownloadFrom) override;
};

class FtpFileDownloader : public IInternetFileDownloader
{
public:
    virtual void Download(const std::string& urlToDownloadFrom) override;
};

这可能就是我要采用的方法。

另一个变体是将“工厂”作为一个单独的类可能是有意义的。一个优点是它可以更好地在单元测试中模拟下载器的实例。

class IInternetFileDownloader
{
public:
    virtual ~IInternetFileDownloader() = default;
    virtual void Download(const std::string& urlToDownloadFrom) = 0;
};

class InternetFileDownloaderFactory
{
public:
    // parses the url to infer the protocol and construct an instance of a derived class
    virtual IInternetFileDownloader* CreateDownloader(const std:string& url);
};

class InternetFileDownloaderFactoryMock : public InternetFileDownloaderFactory
{
public:
    IInternetFileDownloader* CreateDownloader(const std:string& url) override
    {
         return new MockFileDownloaderFactoryMock();
    }
};

class MockFileDownloaderFactoryMock : public IInternetFileDownloader
{
public:
    virtual void Download(const std::string& urlToDownloadFrom)
    {
       // do nothing or simulate a download
    }
};

推荐阅读