Context:

Creating objects by keyword, where types of objects to be created are not known in advance or are extensible during runtime.

Challenges:

  • To separate the capability to actually create specific objects from the factory.
  • To add the capability to create objects to the factory during runtime.
  • To fill factory with default population as part of product type definition.

Skill:

Participants:

Factory:

A Singleton. Registers Creator Plugs. Responsible for creating products by unique key.

Creator Plug:

Plugs into factory to create products of a type by unique key. (Typically - Factory Method. Also Class Object, Prototype.)

Concrete Creator Plug:

Registers itself (or is registered) in the factory. Responsible for creating a product of a fixed type and/or content known only to itself. Typically istantiates from a template.

Product:

What the factory is declared to create.

Concrete Product:

What the factory will create for a certain key.

Client:

Obtains products from the factory, supplying key.

Signature:

Factory references Creator Plugs by key and creates products for Client.

Used patterns and idioms:

Factory Method. Optionally: Static Registration idiom.

Used by:

Serialization mechanisms, visual selection mechanisms and generally: entry-points for objects from outside program memory.

Variations:

  • Virtual constructor. Creation method is a static method of the abstract base-class. (Note: the virtual constructor can be implemented by means of any factory, e.g. Dynamic Pluggable Factory.) Source: Stroustrup.
  • Product Trader . Factory loads Creator Plug if not found. Source: Lauder.

Scope:

General.