6

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

// 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;
};

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

4

2 回答 2

1

这个答案与我的第一个解决方案足够不同,它包括您可能认为的“主要更改”,我已将其作为单独的答案:

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

  • 创建者 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));
}
于 2018-03-11T08:12:08.767 回答
0

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

  1. 调用时CreateObject,作为参数传递的值的类型必须与注册的类型相同。您不能传入 a并调用使用 签名float注册的构造函数。double
  2. 由于 中的实现细节boost::bind,参数必须是const &.

因为我想使用的设计限制boost::factory是该类的对象必须包装在 a 中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));
}
于 2018-03-03T03:20:56.793 回答