The last Abstract factory was about the generic interface designing. Now, the implementation of the Abstract Factory would be shown. Typelist of the Silly Soldier, Silly Monster and Silly SuperMonster can be enforced to create the concrete factory for the easy mode and the similarly BadSoldier, BadMonster, BadSuperMonster for the hard mode. The code snippet for the easy mode is given below
TYPELIST_3(SillySoldier, SillyMonster, SillySuperMonster)
The OpNewFactoryUnit is the policy that tells how the objects are created.
The TYPELIST_3 gives the list of the products which are going to be created. The index of the list is matched accordingly with the AbstractEnemyFactory.
AbstractFactory as previously discussed provides the list of products and aids us in making the interface.
The implementations would require the virtual functions as many as the definitions. The Concrete factory should inherit the OpNewFactoryUnit policy( which include DoCreate() function) objects with every type in the typelist.
The policy definition is as follows:
template <class ConcreteProduct, class Base>
class OpNewFactoryUnit : public Base
typedef typename Base::ProductList BaseProductList;
typedef typename BaseProductList::Tail ProductList;
typedef typename BaseProductList::Head AbstractProduct;
return new ConcreteProduct;
The GenScattterHierarchy can be used as it makes use of all the types. Abstract Factory must serve as the base. All the instances and implementations must derive from it.
Lets know about the OpNewFactoryUnit policy instantiations. Each instantiation processes the head of the product list by the DoCreate() function and then passes down the hierarchy. This can be seen from the definition of the policy above.
The concrete factory generates a hierarchy using the GenLinearHierarchy. It is defined as follows
template <class, class> class Creator = OPNewFactoryUnit
class TList = typename AbstractFact::ProductList
: public GenLinearHierarchy<
typename TL::Reverse<TList>::Result, Creator, AbstractFact>
typedef typename AbstractFact::ProductList ProductList;
typedef TList ConcreteProductList;
The hierarchical diagram which would be generated can be seen below