Loki  0.1.7
 All Classes Namespaces Files Functions Variables Typedefs Friends Macros Modules
Factory

Implements a generic object factory. More...

Collaboration diagram for Factory:

Modules

 Factory Error Policies
 Manages the "Unknown Type" error in an object factory.
 

Classes

class  Loki::Factory< AbstractProduct, IdentifierType, CreatorParmTList, FactoryErrorPolicy >
 

Detailed Description

Implements a generic object factory.

The Factory Method pattern is an object-oriented design pattern. Like other creational patterns, it deals with the problem of creating objects (products) without specifying the exact class of object that will be created. Factory Method, one of the patterns from the Design Patterns book, handles this problem by defining a separate method for creating the objects, which subclasses can then override to specify the derived type of product that will be created.
More generally, the term Factory Method is often used to refer to any method whose main purpose is creation of objects.

Wikipedia

Loki proposes a generic version of the Factory. Here is a typical use.

  1. Factory< AbstractProduct, int > aFactory;
  2. aFactory.Register( 1, createProductNull );
  3. aFactory.CreateObject( 1 );


  • 1. The declaration
    You want a Factory that produces AbstractProduct.
    The client will refer to a creation method through an int.
  • 2.The registration
    The code that will contribute to the Factory will now need to declare its ProductCreator by registering them into the Factory.
    A ProductCreator is a just a function that will return the right object. ie
    Product* createProductNull()
    {
    return new Product
    }

  • 3. The use
    Now the client can create object by calling the Factory's CreateObject method with the right identifier. If the ProductCreator were to have arguments (ie :Product* createProductParm( int a, int b ))