gpt4 book ai didi

c++ - 对此处使用的适当设计模式感到困惑

转载 作者:行者123 更新时间:2023-11-28 04:18:58 25 4
gpt4 key购买 nike

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

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 并且根据协议(protocol),它可以构造适当的类。这样客户端代码 (main.cpp) 就不需要担心适当类的实例化。我阅读了有关工厂和构建器设计模式的信息,但对在这种情况下最适合使用哪种设计模式感到有些困惑?

最佳答案

最简单的方法是在 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
}
};

关于c++ - 对此处使用的适当设计模式感到困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55894039/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com