多态工厂

Polymorphic factory

考虑以下代码:

template <typename T>
class DrawerFactory
{
protected:
    DrawerFactory() {};
private:
    virtual shared_ptr<IDrawer> GetDrawer(T settings) = 0;
};

class ConcreteDrawerFactoryA : public DrawerFactory<SettingsA>
{
public:
    shared_ptr<IDrawer> GetDrawer(SettingsA settingsA) override
    {
        if (settingsA.style == A) return make_shared<ConcreteDrawerA>(settingsA.length, settingsA.stroke, settingsA.opacity);
        else return make_shared<ConcreteDrawerB>(20, .5);
    };
};

class ConcreteDrawerFactoryB : public DrawerFactory<SettingsB>
{
public:
    shared_ptr<IDrawer> GetDrawer(SettingsB settingsB) override
    {
        if (settingsB.type == TYPEC) return make_shared<ConcreteDrawerC>(settingsB.width, settingsB.height);
        else return make_shared<ConcreteDrawerD>(10, 2);
    };
};

我可以通过以下方式获得抽屉:

ConcreteDrawerFactoryA().GetDrawer(settingsa);

ConcreteDrawerFactoryB().GetDrawer(settingsb);

我想做的是:

DrawerFactory().GetDrawer(settingsa);
DrawerFactory().GetDrawer(settingsb);

有没有一种方法可以设置它,而不必为我想添加的每个混凝土工厂不断地向 DrawerFactory 添加重载?

在你的例子中,你的工厂好像没有状态,那么没有多态工厂你能实现你想要的吗?例如像这样的东西...

template<class T>
std::shared_ptr<IDrawer> MakeDrawer(T settings);

template<>
std::shared_ptr<IDrawer> MakeDrawer<SettingsA>(SettingsA settings)
{
    return std::make_shared<ConcreteDrawerA>(); // use settings really
}

template<>
std::shared_ptr<IDrawer> MakeDrawer<SettingsB>(SettingsB settings)
{
    return std::make_shared<ConcreteDrawerB>(); //use settings here
}


void main()
{

   SettingsA setA;
   std::shared_ptr<IDrawer> pA = MakeDrawer(setA);

   SettingsB setB;
   std::shared_ptr<IDrawer> pB = MakeDrawer(setB);
}

您可以使用重载而不是模板。

您可以使用模板和专业化代替工厂层次结构和虚拟分派:

#include <memory>

struct IDrawer { };
struct Drawer1: IDrawer { };
struct Drawer2: IDrawer { };
struct Drawer3: IDrawer { };
struct Drawer4: IDrawer { };

template <class T>
struct DrawerGetterImpl;

struct DrawerFactory {
    template <class T>
    std::shared_ptr<IDrawer> GetDrawer(T settings) {
        return DrawerGetterImpl<T>::GetDrawer(settings);
    }
};

struct SettingsA { int style; };

template <>
struct DrawerGetterImpl<SettingsA> {
    static std::shared_ptr<IDrawer> GetDrawer(SettingsA settings) {
        if (settings.style == 1) {
            return std::make_shared<Drawer1>();
        }
        return std::make_shared<Drawer2>();
    }
};

struct SettingsB { int type; };

template <>
struct DrawerGetterImpl<SettingsB> {
    static std::shared_ptr<IDrawer> GetDrawer(SettingsB settings) {
        if (settings.type == 1) {
            return std::make_shared<Drawer3>();
        }
        return std::make_shared<Drawer4>();
    }
};

int main() {
    DrawerFactory().GetDrawer(SettingsA{1});
}

[live demo]