The Factory Method pattern, also known as Virtual Constructor, is a creational GoF design pattern by Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides that defines an interface for creating an object but lets subclasses decide which class to instantiate. It suits a class that cannot anticipate which objects it must create, and it allows new types to be added without changing existing code.

GoF-Factory Method Pattern

Details
Full Name

GoF Factory Method Pattern (Creational)

Also known as

Virtual Constructor

Intent:

Define an interface for creating an object, but let subclasses decide which class to instantiate.

When to Use:

  • When a class can’t anticipate the class of objects it must create

  • When a class wants its subclasses to specify the objects it creates

Prompt Example:

"Extrahiere die Objekterzeugung in eine Factory Method nach GoF, damit neue Typen ohne Änderung des bestehenden Codes hinzugefügt werden können."