C++ Loki Abstract Factory Interface














































C++ Loki Abstract Factory Interface



GENERIC INTERFACE

This article would show how to enforce a generic Abstract Factory interface. This could be done with the help of type lists. First, we create an abstract class that has a virtual function with type T and returns a T*. Then simply derive more factories and object from the ancestral factories by the new operator.
 I have explained about the GenScatterHierarchy earlier and we could use that, as it inherits all the objects provided by the user. This is of a great significance because, we create objects of one type and can carry out it in multiple types. 
Let's examine an example where GenScatterHierarchy<TList, Unit> inherits Unit<T> for every type T and is used in TypeList T.

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

The reason for carrying out Type2Type is because the abstract factory creates many DoCreate functions and to differentiate between all those instantiations of the AbstractFactoryUnit. It distinguishes it by which DoCreate() function is invoked by Create(). The Abstract Factory Unit and the GenScatterHierarchy go hand in hand as shown below:

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>());
}
};
Let's us utilize our Abstract Factory on our game with Soldier, Monster and SuperMonster classes. As per the rules of the GenScatterHierarchy, the abstract factory modularizes and the typelist created becomes independent units which can be readily used. The hierarchical graph is depicted below:



The AbstractEnemyFactory is mealy, as the object of it can be transformed into AbstractEnemyFactory<Soldier>, AbstractEnemyFactory<Monster> or AbstractEnemyFactory<SuperMonster>. So, if in an app only a particular object of AbstractEnemyFactory is required like only Soldiers, the references or pointers could be used of that particular module so that it is not mixes up with other abstract enemy types.

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 249 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 144 2
C++ iomanip ends 124 1
C++ iomanip flush 130 2
C++ iomanip ws 161 2

Comments