GenericFactory Class

template <typename BASE, typename IDENTIFIER = QString, typename... ARGUMENTS> class GenericFactory

The GenericFactory class implements a template-based generic factory. More...

Public Types

Public Functions

virtual ~GenericFactory()
BASE *create(const IDENTIFIER &id, ARGUMENTS... args) const

Protected Functions

Detailed Description

GenericFactory is an implementation of the factory pattern. It can be used to produce instances of different classes having a common superclass BASE. The user of the factory registers those producible classes in the factory by using the identifier IDENTIFIER. That identifier can then be used to produce as many instances of the registered product as the user wants.

One factory instance is able to produce instances of different types of DERIVED classes only when the constructor of DERIVED or the registered generator function have a common signature for all DERIVED classes. This signature is described by the declaration order of ARGUMENTS. It is referred to as SIGNATURE in the following paragraphs.

If a class derived from BASE does not contain a SIGNATURE matching the registered one for the constructor or the generator function, it is not possible to create instances of it using one instance of GenericFactory subclass. In that case, more than one GenericFactory subclass and instance are needed.

It is possible to register a subclass of BASE inside an instance of GenericFactory subclass using the registerProduct() function. At least one of the following conditions needs to be met:

  • A global or static function with SIGNATURE exists.
  • The DERIVED class has a constructor with SIGNATURE.
  • The DERIVED class has a static function with SIGNATURE.

To get a new instance of DERIVED, one needs to call the create() function. The value of IDENTIFIER determines the product's subclass registered in the factory, while the values of SIGNATURE are the actual arguments passed to the class constructor or the registered generator function.

Member Type Documentation

GenericFactory::FactoryFunction

This typedef defines a factory function producing an object of type BASE.

Member Function Documentation

[protected] GenericFactory::GenericFactory()

Creates the generic factory.

[virtual] GenericFactory::~GenericFactory()

Destroys the generic factory.

BASE *GenericFactory::create(const IDENTIFIER &id, ARGUMENTS... args) const

Creates and returns the type identified by id with variable number of arguments args passed to the object's constructor, but automatically upcasted to BASE. Ownership of the type is transferred to the caller.

© 2021 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. The Qt Company, Qt and their respective logos are trademarks of The Qt Company Ltd in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.