あなたは正しい道を進んでいるようですね。
WriterとConnectionの組み合わせであるクラスの作成は避け、代わりにWriterインターフェースとConnectionインターフェースを(属性として)含むある種のManagerクラスを作成する必要があります。次に、それぞれの適切な実装を作成し、それらを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();
}