8.10 Factory Class Template Quick Facts
Factory declaration:
template
<
class AbstractProduct,
class IdentifierType,
class ProductCreator = AbstractProduct* (*)(),
template<typename, class>
class FactoryErrorPolicy = DefaultFactoryError
>
class Factory;
AbstractProduct is the base class of the hierarchy for which you provide the object factory.
IdentifierType is the type of the "cookie" that represents a type in the hierarchy. It has to be an ordered type (able to be stored in a std::map). Commonly used identifier types are strings and integral types.
ProductCreator is the callable entity that creates objects. This type must support operator(), taking no parameters and returning a pointer to AbstractProduct. A ProductCreator object is always registered together with a type identifier.
Factory implements the following primitives:
bool Register(const IdentifierType& id, ProductCreator creator);
Registers a creator with a type identifier. Returns true if the registration was successful; false otherwise (if there already was a creator registered with the same type identifier).
bool Unregister(const IdentifierType& id);
Unregisters the creator for the given type identifier. If the type identifier was previously registered, the function returns true.
AbstractProduct* CreateObject(const IdentifierType& id);
Looks up the type identifier in the internal map. If it is found, it invokes the corresponding creator for the type identifier and returns its result. If the type identifier is not found, the result of FactoryErrorPolicy<IdentifierType, AbstractProduct>:: OnUnknownType is returned. The default implementation of FactoryErrorPolicy throws an exception of its nested type Exception.
|