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
Participants
Product
ConcreteProduct
- Defines the interface of objects the factory method creates
Creator
- Implements the Product interface
ConcreteCreator
- 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
- Overrides the factory method to return an instance of a ConcreteProduct
