2

我使用递归模板习语在工厂中自动注册基类的所有子类。然而,在我的设计中,子类必须将基类作为朋友类。由于我的基类的构造函数应该是私有的,以避免通过工厂以外的方式实例化此类。

总体目标是工厂的注册在 BaseSolver 中完成,并且 ChildClasses 不能通过工厂以外的方式实例化。

这是我的基类的代码,它自动在 SolverFactory 中注册所有子类。

template<class T>
struct BaseSolver: AbstractSolver
{
protected:
    BaseSolver()
    {
        reg=reg;//force specialization
    }
    virtual ~BaseSolver(){}
    /**
     * create Static constructor.
     */
    static AbstractSolver* create()
    {
        return new T;
    }

    static bool reg;
    /**
     * init Registers the class in the Solver Factory
     */
    static bool init()
    {
        SolverFactory::instance().registerType(T::name, BaseSolver::create);
        return true;
    }
};

template<class T>
bool BaseSolver<T>::reg = BaseSolver<T>::init();

这里是我的子类的头文件:

class SolverD2Q5 : public BaseSolver<SolverD2Q5>{

private:
  //how can I avoid this?
  friend class BaseSolver;

  SolverD2Q5();

  static const std::string name;

}

这工作正常。但是我真的不喜欢将 BaseSolver 添加为友元类,但是我希望构造函数和静态成员名称是公开的。

是否有更优雅的解决方案或更好的布局来避免这种情况?

4

1 回答 1

0

更新: 我相信这个技巧还没有被理解,因此我现在创建了完整的解决方案。这只是对 OP 代码的一点点改动。只需将 中的 T 替换为BaseSolver派生自 的空类定义T

原文: 我认为您可以通过将友谊委托给基础 Solver 私有的包装类来做到这一点。该类将从要为其创建实例的任何类继承。编译器应该优化包装类。

#include <iostream>
#include <map>

struct AbstractSolver { virtual double solve() = 0; };

class SolverFactory
{
    std::map<char const * const, AbstractSolver * (*)()> creators;
    std::map<char const * const, AbstractSolver *> solvers;
public:
    static SolverFactory & instance()
    {
        static SolverFactory x;
        return x;
    }
    void registerType(char const * const name, AbstractSolver *(*create)())
    {
        creators[name] = create;
    }
    AbstractSolver * getSolver(char const * const name)
    {
        auto x = solvers.find(name);
        if (x == solvers.end())
        {
            auto solver = creators[name]();
            solvers[name] = solver;
            return solver;
        }
        else
        {
            return x->second;
        }
    }
};

template<class T> class BaseSolver : public AbstractSolver
{
    struct Wrapper : public T { // This wrapper makes the difference
        static char const * const get_name() { return T::name; }
    };
protected:
    static bool reg;
    BaseSolver() {
        reg = reg;
    }
    virtual ~BaseSolver() {}
    static T * create() {
        return new Wrapper; // Instantiating wrapper instead of T
    }
    static bool init()
    {
        SolverFactory::instance().registerType(Wrapper::get_name(), (AbstractSolver * (*)())BaseSolver::create);
        return true;
    }
};

template<class T>
bool BaseSolver<T>::reg = BaseSolver<T>::init();

struct SolverD2Q5 : public BaseSolver<SolverD2Q5>
{
public:   
    double solve() { return 1.1; }
protected: 
    SolverD2Q5() {} // replaced private with protected
    static char const * const name;
};
char const * const SolverD2Q5::name = "SolverD2Q5";

struct SolverX : public BaseSolver<SolverX>
{
public:    
    double solve() { return 2.2; }
protected: 
    SolverX() {} // replaced private with protected
    static char const * const name;
};
char const * const SolverX::name = "SolverX";

int main()
{
    std::cout << SolverFactory::instance().getSolver("SolverD2Q5")->solve() << std::endl;
    std::cout << SolverFactory::instance().getSolver("SolverX")->solve() << std::endl;
    std::cout << SolverFactory::instance().getSolver("SolverD2Q5")->solve() << std::endl;
    std::cout << SolverFactory::instance().getSolver("SolverX")->solve() << std::endl;

    char x;
    std::cin >> x;
    return 0;
}
于 2013-09-10T03:34:38.400 回答