Factory Method Pattern Role Division

The Factory Method Pattern is a creational design pattern that defines an interface for creating objects, but defers the actual instantiation to subclasses. The factory method pattern is typically considered a standard implementation of the broader factory paradigm.

In the context of the factory method pattern, there is usually an abstract factory class that declares an abstract factory method used to create product instances. Concrete product classes implement either the interface or abstract class defined by this factory method and carry out the creation of specific products.

The implementation of the factory method pattern typically involves four key roles:

  1. Abstract Factory Class (Creator): Defines an abstract factory method for creating product objects.
  2. Concrete Creator: Implements the factory methods declared in the abstract factory class to perform concrete object instantiation.
  3. Abstract Product Class (Product): Declares either an interface or an abstract class representing the product; concrete product classes implement these interfaces or inherit from these abstract classes.
  4. Concrete Product: Implements the interface defined by the abstract product class or inherits from it to perform specific object creation.

The advantages of the factory method pattern include encapsulating the object creation process, thereby decoupling client code from concrete implementations. Furthermore, this pattern provides a consistent interface for creating different types of objects, which simplifies the development and maintenance of client-side logic.