dimanche 28 avril 2019

Confused about the appropriate design pattern to be used here

I am implementing a class that can download a file from various sources such as ftp, http etc. I started with the following interface

class IInternetFileDownloader
{
public:
    IInternetFileDownloader() = default;

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

};

I then implemented the classes that will perform the actual download from the appropriate endpoint. So I have a HttpFileDownloader.h as follows

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

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

};

So I have a FtpFileDownloader .h as follows

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

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

};

I can invoke the appropriate class as below

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

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

However, I don't want to instantiate specific HttpFileDownloader or FtpFileDownloader here. In my mind, there should be another class that can just take the url and depending upon the protocol, it can construct the appropriate class. This way the client code (main.cpp) does not need to worry about the instantiation of the appropriate class. I read about the factory and builder design patterns and a little confused about which one will be best to use in this scenario?

Aucun commentaire:

Enregistrer un commentaire