Factory Method


Usage
  • Also known as: Virtual Constructor
  • Create an instance of several derived classes
  • Define an interface for creating an object, but let subclasses decide which class to instantiate
  • Let a class defer instantiation to subclasses
Diagram
Factory Method
Participants
Product
  • Defines the interface of objects the factory method creates
ConcreteProduct
  • Implements the Product interface
Creator
  • Declares the factory method, which returns an object of type Product
  • Creator may also define a default implementation of the factory method that returns a default ConcreteProduct object
  • May call the factory method to create a Product object
ConcreteCreator
  • Overrides the factory method to return an instance of a ConcreteProduct