简体   繁体   English

在多个库中拆分工厂(模式)

[英]Split Factory (pattern) over multiple libraries

As I can't use the real code, I'll try to sketch the situation using a simplified pseudo-code example: 由于我无法使用实际代码,因此我将尝试使用简化的伪代码示例来概述情况:

class Factory {
public:
    System& build(const Input& input) {
        if (input.getType() == "Airplane") return Airplane(input.getNrWheels);
        if (input.getType() == "Car") return Car(input.getNrWheels);
    }
}

Both Car and Airplane inherit from System; 汽车和飞机均从系统继承;

We use this factory in two executables. 我们在两个可执行文件中使用此工厂。 As executable 1 can be used with Cars and executable 2 can be used with Airplanes. 由于可执行文件1可以与汽车一起使用,而可执行文件2可以与飞机一起使用。 We would like to be able to compile and link executable 1 with Factory and Car and executable 2 with Factory and Airplane. 我们希望能够将可执行文件1与Factory和Car以及可执行文件2与Factory和Airplane进行编译和链接。 What should we do with the Factory class to enable this? 为此,我们应该对Factory类做什么呢?

EDIT: Change after comments/answers 编辑:评论/答案后更改

This is less simplified example showing my problem more clearly: 这个不太简化的示例更清楚地显示了我的问题:

class Factory {
public:
    System& build(const Input& input) {
        if (input.Airplane()) {
            Pilot& pilot         = *new Pilot();
            AutoPilot& autoPilot = *new AutoPilot();
            return Airplane(input.Airplane()->NrWheels(), pilot);
        }
        if (input.Car()) {
            Driver& driver = *new Driver();
            return Car(input.Car()->NrWheels, driver);
        }
    }
}

I like the answer below which suggests the constructor map, but I (think I) need a version which allows different arguments for the constructors. 我喜欢下面的建议构造函数映射的答案,但我(认为我)需要一个允许为构造函数使用不同参数的版本。 I also would like to keep Car and Airplane free of dependency on Input. 我也想让Car和Airplane不受Input的依赖。

Use something like a std::map<std::string, std::function<System(T)> ctorMap; 使用类似std::map<std::string, std::function<System(T)> ctorMap; where T is the type of Input::getNrWheels , inside Factory to hold the appropriate constructors, indexed by strings. 其中TFactory内部的Input::getNrWheels的类型,用于容纳适当的构造函数,该构造函数由字符串索引。 Also provide a method, say Factory::Register(std::string, std::function<System(T)) which any class can use to register its constructor. 还提供一种方法,例如Factory::Register(std::string, std::function<System(T)) ,任何类都可以使用该方法来注册其构造函数。 So, the TU which defines Airplane should ensure that there is a call, during static initialization, for example, Factory::Register("Airplane", std::make_function(&Airplane::Airplane)) . 因此,定义Airplane的TU应该确保在静态初始化期间有一个呼叫,例如Factory::Register("Airplane", std::make_function(&Airplane::Airplane))

Also, Factory::build would now look like this: 另外, Factory::build现在看起来像这样:

System& build(const Input& input) {
     return ctorMap[input.getType()](input.getNrWheels);
    }

Cut it out completely. 完全切掉。 The Factory class serves literally no benefit at all when it can only create one kind of thing. 当工厂类只能创建一种东西时,它根本没有任何好处。 Just use a typedef instead to be flexible about what kind of System you are creating. 只需使用typedef来灵活选择要创建的系统类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM