了解抽象工厂通用实现

时间:2015-04-05 09:24:06

标签: c++ templates

我正在阅读Andrei Alexandrescu的现代C ++设计并遇到一些问题:

以下是Generic AbstractFactory的外观

template <class T>
class AbstractFactoryUnit
{
public:
    virtual T* DoCreate(Type2Type<T>) = 0;
    virtual ~AbstractFactoryUnit() {}
};

template 
<
    class TList,
    template <class> class Unit = AbstractFactoryUnit
>
class AbstractFactory : public GenScatterHierarchy<TList, Unit>
{
public:
    typedef TList ProductList;
    template <class T> T* Create()
    {
        Unit <T>& unit = *this;
        return unit.DoCreate(Type2Type<T>());
    }
};

现在假设我们有三个班级:

class Soldier { };
class Monster { };
class SuperMonster { };

并定义以下类型定义:

typedef AbstractFactory
<
    TYPELIST_3(Soldier, Monster, SuperMonster)
>
AbstractEnemyFactory;

然后,在Alexandrescu的Modern C++ Design说:

  

您可以自动将引用转换为AbstractEnemyFactory   引用AbstractFactory<Soldier>AbstractFactory   <Monster>AbstractFactory<SuperMonster>

可能只是一个错字,他的意思是AbstractFactoryUnit<Soldier>等等......

因为对我而言,我们不清楚如何将对AbstractEnemyFactory的引用转换为AbstractFactory<Soldier>

0 个答案:

没有答案