范型抽象工厂模式


本节介绍范型编程中的抽象工厂模式。

一、AbstractFactory的定义

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

其中的Type2Type定义如下

template 
struct Type2Type{   
    typedef T OriginalType;
};

其中的AbstractFactoryUnit定义如下

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

其中GenScatterHierarchy的定义如下

template  class Unit> class GenScatterHierarchy;

// GenScatterHierarchy specialization: Typelist to Unit
template  class Unit>
class GenScatterHierarchy, Unit> : public GenScatterHierarchy , public GenScatterHierarchy{
public:   
    typedef typename Typelist TList;   
    typedef typename GenScatterHierarchy LeftBase;   
    typedef typename GenScatterHierarchy RightBase;
};

// Pass an atomic type (non-typelist) to Unit
template  class Unit>
class GenScatterHierarchy : public Unit{
    typedef typename Unit LeftBase;
};

// Do nothing for NullType
template