design-patterns - 关于该项目的表归档器的设计模式建议

标签 design-patterns architecture

所以我有以下项目要完成。我尚未决定如何设计它。希望得到一些建议。

它基本上是一个表归档器。在特定条件下,您需要将这些行导出到另一个位置。 这个地方可以是另一个数据库或FTP 服务器。 如果您选择数据库,则每次达到特定限制(例如每个表不超过 50k 行)时都需要创建一个表,如果您选择 (s)ftp 服务器,则需要编写 CSV 或 XML 并将文件在那里。

所以我们有这些组合:

  1. sql2CustomerSql
  2. sql2Oursql(为此我们已经创建了用于连接和获取一些基于系统配置的信息的类)
  3. csv2ftp
  4. csv2sftp
  5. xml2ftp
  6. xml2sftp

现在,我到处都能看到 AbstractFactory 模式,但是基于什么? 我的想法是,我应该拥有 SQLWriter、XMLWriter、CSVWriter,它们全部继承自抽象 Writer 类,该类实现一些通用策略,例如计算行数、获取通用配置参数等...... 我是否应该对 Connection 类/接口(interface)执行相同的操作(因为 sql 和 (s)ftp 确实不同?

如果您需要更多信息,请询问。

最佳答案

听起来您走在正确的道路上。

您应该避免创建 Writer 和 Connection 组合的类,而是创建某种包含(作为属性)Writer 接口(interface)和 Connection 接口(interface)的 Manager 类。然后创建每个的适当实现并将它们传递到管理器中。

这是策略设计模式的经典用法。

编辑:添加代码示例。您应该添加适当的错误检查。

class Writer
{
public:
    virtual void output(cons std::string &data) = 0;
};

class Format
{
public:
    virtual std::string serialize() = 0;
};

// Create concrete Writer and Format classes here

class OutputManager
{
public:
    // Notice there should be no Writer, Format creation logic here,
    // This class should focus on orchestrating the output
    OutputManager() : writer_(NULL), format_(NULL) {}
    OutputManager(Writer *w, Format *f) : writer_(w), format_(f) {}

    void setWriter(Writer *w) { writer_ = w; }
    Writer *getWriter()       { return writer_; }

    void setFormat(Format *f) { format_ = f; }
    Format *getFormat()       { return format_; }

    // Maybe this should have a different return type
    void doOutput()
    {
        // Not sure what else you would need here,
        // but this is an example usage
        writer_->output(format_->serialize());
    }

private:
    Writer *writer_;
    Format *format_;
};

//
// And now the factories
//
class OutputAbstractFactory
{
public:
    OutputAbstractFactory(Config *c) config_(c) {}
    void createFactories()
    {
        writerFactory_ = WriterAbstractFactory::getWriterFactory(config_);
        formatFactory_ = FormatAbstractFactory::getFormatFactory(config_);
    }

    Writer *getWriter() { return writerFactory_->getWriter(); }
    Format *getFormat() { return formatFactory_->getFormat(); }

private:
    WriterAbstractFactory *writerFactory_;
    FormatAbstractFactory *formatFactory_;
    Config *config_;
}

class WriterAbstractFactory
{
public:
    // Config is a class you will have to make with 
    // the info detailing the Writer and Format stuff
    static WriterAbstractFactory *getWriterFactory(Config *c);
    virtual Writer *getWriter() = 0;
};

class FormatAbstractFactory
{
public:
    // Config is a class you will have to make with
    // the info detailing the Writer and Format stuff
    static FormatAbstractFactory *getFormatFactory(Config *c);
    virtual Format *getFormat() = 0;
};

// Create concrete factories here

//
// And this ties it all together
//
int main(int argc, char **argv)
{
    Config c;
    // populate Config accordingly

    OutputAbstractFactory *factory(&c);
    factory.createFactories();

    Writer *w = factory->getWriter();
    Format *f = factory->getFormat();
    // Do whatever else you need to with the Writer/Format here

    OutputManager om(w, f);
    // Do whatever else you need with the OutputManager here
    om.doOutput();
}

关于design-patterns - 关于该项目的表归档器的设计模式建议,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10348115/

相关文章:

java - 我应该在哪个层,Dao 或 Service 中解析 Rest Client 响应?

asp.net-mvc - UnitOfWork 作为存储库的容器

.net - 连续加工

java - hibernate 基础知识

ios - CleanSwift - 演示者可以直接调用路由器功能吗?

oop - 每个公共(public)方法都被接口(interface)覆盖是一个好的做法吗?

c++ - 如何在 C++ 中透明地处理不同的协议(protocol)版本?

design-patterns - 具有不同方法签名的策略模式

security - 防止移动 API 客户端身份盗用

flutter - 在 Flutter BLoC 架构中放置自定义辅助类的位置