Overview

The Factory Method pattern is used when a class cannot anticipate the class of objects it must create. It decouples the code that uses the objects from the code that creates them.

Key Components

  • Creator: Declares the factory method.
  • Concrete Creator: Overrides the factory method to return an instance of a Concrete Product.
  • Product: Defines the interface of objects the factory method creates.
  • Concrete Product: Implements the Product interface.

Benefits

  • Promotes loose coupling.
  • Follows the Open/Closed Principle.

Related Terms