如何在工厂设计中支持多个施工签名

How to support multiple construction signatures in a factory design?

本文关键字:支持 施工签 工厂      更新时间:2023-10-16

我正在使用以下(简化的)工厂设计来创建某些继承层次结构的对象,应该没有什么特别的:

// class to create
class Class
{
public:
  Class(Type type, Foo foo);
};
// Simple creator class.
// Used in practice to do some runtime checks about whether or not construction is allowed.
class Creator
{
public:
  Class* create( Type type, Foo foo ) const
  {
    return new Class( type, foo );
  }
};
class Factory
{
public:
  Factory
  {
    // fill object creator map on construction
    _map[ "name" ] = new Creator<Class>;
  }
  Class* create( const std::string& name, Type type, Foo foo )
  {
    // fowards to map entry
    return _map[name]->create( type, foo );
  }
private:
 std::map<std::string, Creator*> _map;
}
// client code
int main()
{
  Factory f;
  factory.create(name, type, foo);
}

现在,一旦我想创建具有不同构造函数签名的子类,我就遇到了问题,因为工厂在整个继承层次结构上强加了固定签名。 即对于下面的类,我无法通过工厂构造指定新的第三个参数,而不会再次将此扩展签名强加于层次结构的所有其他类。

class ExtClass : public Class
{
public:
  Class(Type type, Foo foo, NewMember nm)
    : Class(type, foo),
      _nm(nm)
private:
  NewMember _nm;
};

有没有办法在不进行 pricinpal 更改的情况下使用我当前的设计来使其工作?我正在考虑使用模板或绑定对象来使各种参数调用成为可能。或者在这种情况下,您会建议与工厂设计不同的解决方案吗?

这个答案与我的第一个解决方案非常不同,它包括您可能认为的"主要变化",我将其作为单独的答案:

在我看来,它优于我之前的解决方案,但这取决于您的确切要求是什么。这里的功能是:

  • 创建者 ID 是唯一的。
  • CreateObject支持参数的隐式转换。

构造函数必须采用const&参数的相同限制也存在。这可能无关紧要,但此解决方案只需要 C++11。当然,使用新的 C++17 元组功能会更简单一些。

#include <boost/functional/factory.hpp>
#include <boost/function.hpp>
#include <boost/variant.hpp>
#include <map>
#include <stdexcept>
#include <tuple>
#include <type_traits>
#include <utility>
// Just for debugging.
#include <iostream>
#include <typeinfo>
#include <cxxabi.h>
// Tuple manipulation.
template <typename Signature>
struct signature_impl;
template <typename ReturnType, typename... Args>
struct signature_impl<ReturnType(Args...)>
{
    using return_type = ReturnType;
    using param_types = std::tuple<Args...>;
};
template <typename T>
using signature_t = signature_impl<T>;

template <std::size_t... Ints>
struct indices {};
template <std::size_t N, std::size_t... Ints>
struct build_indices : build_indices<N-1, N-1, Ints...> {};
template <std::size_t... Ints>
struct build_indices<0, Ints...> : indices<Ints...> {};
template <typename Tuple>
using make_tuple_indices = build_indices<std::tuple_size<typename std::remove_reference<Tuple>::type>::value>;
// The multiple-signature factory.
template <class AbstractProduct, typename IdentifierType, typename... ProductCreators>
class multifactory
{
    using functions = boost::variant<boost::function<ProductCreators>...>;
    std::map<IdentifierType, functions> associations_;
    template <typename Signature>
    struct dispatch_foo
    {
        template <typename CreateArgs, std::size_t... Indices>
        typename std::enable_if<std::is_convertible<CreateArgs, typename signature_t<Signature>::param_types>::value, AbstractProduct>::type
        static apply(boost::function<Signature> const &f, CreateArgs && t, indices<Indices...>)
        {
            return f(std::get<Indices>(std::forward<CreateArgs>(t))...);
        }
        template <typename CreateArgs, std::size_t... Indices>
        typename std::enable_if<!std::is_convertible<CreateArgs, typename signature_t<Signature>::param_types>::value, AbstractProduct>::type
        static apply(boost::function<Signature> const &, CreateArgs &&, indices<Indices...>)
        {
            return nullptr;
        }
    };
    template <typename... CreateArguments>
    struct dispatcher : boost::static_visitor<AbstractProduct>
    {
        std::tuple<CreateArguments...> args;
        dispatcher(CreateArguments const&... args) : args{std::forward_as_tuple(args...)} {}
        template <typename Signature>
        AbstractProduct operator()(boost::function<Signature> const &f) const
        {
            int status;
            std::cout << "visitor: " << abi::__cxa_demangle(typeid(Signature).name(), nullptr, 0, &status) << "n";
            return dispatch_foo<Signature>::apply(f, args, make_tuple_indices<std::tuple<CreateArguments...>>{});
        }
    };
public:
    template <typename ProductCreator>
    bool Register(IdentifierType id, ProductCreator &&creator) {
        return associations_.emplace(id, std::forward<ProductCreator>(creator)).second;
    }
    bool Unregister(const IdentifierType& id) {
        return associations_.erase(id) == 1;
    }
    template <typename... Arguments>
    AbstractProduct CreateObject(const IdentifierType& id, Arguments const& ... args) {
        auto i = associations_.find(id);
        if (i != associations_.end()) {
            dispatcher<Arguments...> impl(args...);
            return boost::apply_visitor(impl, i->second);
        }
        throw std::runtime_error("Creator not found.");
    }
};

struct Arity {
    virtual ~Arity() = default;
};
struct Nullary : Arity {};
struct Unary : Arity {
    Unary() {} // Also has nullary ctor.
    Unary(int) {}
};

int main(void)
{
    multifactory<Arity*, int, Arity*(), Arity*(const int&)> factory;
    factory.Register(0, boost::function<Arity*()>( boost::factory<Nullary*>() ));
    factory.Register(1, boost::function<Arity*(const int&)>(boost::factory<Unary*>()) );
    auto a = factory.CreateObject(0);
    assert(a);
    assert(typeid(*a) == typeid(Nullary));
    auto b = factory.CreateObject(1, 2);
    assert(b);
    assert(typeid(*b) == typeid(Unary));
}

对不同的命名约定表示歉意,但这是我目前使用的 C++14 解决方案。两个主要缺点是

  1. 调用 CreateObject 时,作为参数必须与注册的类型相同。你不能通过float并调用向double注册的构造函数签名。
  2. 由于boost::bind中的实现细节,参数必须const &

因为我想使用boost::factory而设计的限制是,该类的对象必须包装在boost::function中(以消除函数签名的歧义)。

所以它有效,但肯定可以通过更多的元编程智慧来改进:

#include <boost/functional/factory.hpp>
#include <boost/function.hpp>
#include <boost/bind.hpp>
#include <cassert>
#include <map>
#include <tuple>
#include <type_traits>
#include <utility>

template <class AbstractProduct, typename IdentifierType, typename... ProductCreators>
class Factory
{
    using AssociativeContainers = std::tuple<std::map<IdentifierType, boost::function<ProductCreators>>...>;
public:
    template <typename Product, typename... Arguments>
    bool Register(const IdentifierType& id, boost::function<Product(Arguments...)> creator) {
        auto &foo = std::get<std::map<IdentifierType, boost::function<AbstractProduct(const Arguments&...)>>>(associations_);
        return foo.emplace(id, creator).second;
    }
    // This function left as an exercise to the reader...
    bool Unregister(const IdentifierType& id) {
        return associations_.erase(id) == 1;
    }
    template <typename... Arguments>
    AbstractProduct CreateObject(const IdentifierType& id, Arguments&& ... args) const {
        auto const &foo = std::get<std::map<IdentifierType, boost::function<AbstractProduct(const Arguments&...)>>>(associations_);
        auto const i = foo.find(id);
        if (i != foo.end()) {
            return (i->second)(std::forward<Arguments...>(args)...);
        }
        throw std::runtime_error("Creator not found.");
    }
private:
    AssociativeContainers associations_;
};

struct Arity {
    virtual ~Arity() = default;
};
struct Nullary : Arity {};
struct Unary : Arity {
    Unary() {}
    Unary(double x) : x(x) {}
    double x;
};

int main(void)
{
    Factory<Arity*, int, Arity*(), Arity*(const double&)> factory;
    factory.Register(0, boost::function<Arity*()>{boost::factory<Nullary*>()} );
    factory.Register(1, boost::function<Arity*(const double&)>{boost::bind(boost::factory<Unary*>(), _1)});
    auto x = factory.CreateObject(1, 2.0);
    assert(typeid(*x) == typeid(Unary));
    x = factory.CreateObject(0);
    assert(typeid(*x) == typeid(Nullary));
}