C++ Loki Abstract Factory Implementation

C++ Loki Abstract Factory Implementation


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

typedef ConcreteFactory
// The abstract factory to implement
// The policy for creating objects
// (for instance, use the new operator)
// The concrete classes that this factory creates
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;
ConcreteProduct* DoCreate(Type2Type<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

class AbstractFact,
template <class, class> class Creator = OPNewFactoryUnit
class TList = typename AbstractFact::ProductList
class ConcreteFactory
: 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 

One doubt might occur that the order of the typelist is reversed. Why is it still working? In the background, the ConcreteFactory reverses the typelist TList using the TL::Reverse functions before it is handed on the GenLinearHierarchy.

More Articles of Abhinav Verma:

Name Views Likes
C++ Loki Abstract Factory Implementation 255 1
C++ Loki SmallObject Compiler Working 152 1
C++ Loki SmallObject 180 1
C++ Loki Abstract Factory Interface 248 1
C++ Loki Double Dispatching 167 2
C++ Loki SmallObjAllocator 138 2
C++ Loki Abstract Factory 132 2
C++ Loki Small Objects Allocation Methods 144 1
C++ Loki The Fixed Size Allocator 202 1
C++ Loki Necessity of Multi-Methods 125 1
C++ Loki Chunks 132 1
C++ Loki Packing Order of Small Object Allocator 125 2
C++ Loki Multimethods Polymorphism 148 2
C++ Loki How does memory allocator works? 131 1
C++ Loki Default Storage Allocator 120 1
C++ Loki Multimethods 127 1
C++ Loki Small Object Allocator 118 1
C++ Loki Techniques Compile Time Affirmations 120 1
C++ Loki Typelist Linear Hierarchy 130 1
C++ Loki Techniques type_info 114 1
C++ Loki Traits Generating Tuples 121 1
C++ Loki Techniques Partial Template Specialization 127 1
C++ Loki Typelist Generating Scattered Hierarchy 123 1
C++ Loki Techniques Select 119 1
C++ Loki Clubbing TypeList 114 1
C++ Loki Techniques Traits 113 1
C++ Loki TypeList Substituting Elements 117 1
C++ Loki Techniques Local Classes 115 1
C++ Loki TypeList Deleting Duplicated 116 1
C++ Loki Techniques Introduction 119 1
C++ Loki Effacing type from typelist 118 1
C++ Loki Append TypeList 113 1
C++ Loki Mouldering Classes 117 1
C++ Loki Searching TypeList 121 1
C++ Loki Smart Pointer Compatibility of Policies 128 1
C++ Loki TypeList Indexed Access 121 1
C++ Loki Smart Pointer Structure Customization 118 1
C++ Loki Length of TypeList 123 1
C++ Loki Smart Pointer Threading Model and Checking Policy 118 1
C++ Loki Voluntary Functionality through Incomplete Initialization 113 1
C++ Loki Policy Class Destructor 128 1
C++ Loki Enriched Policies 115 1
C++ Loki Policy Classes with Template Template Parameter and Member functions 115 1
C++ Loki Kernel of Policy Classes 116 1
C++ Loki Templates in design 125 1
C++ Loki Do-it-All Interface & Multiple Inheritance 125 1
C++ Loki Policy Based Design 153 2
C++ Loki Introduction 143 2
C++ iomanip ends 124 1
C++ iomanip flush 129 2
C++ iomanip ws 161 2