DEV Community

Cover image for Factory Method
Francesco Ciulla
Francesco Ciulla

Posted on • Updated on

Factory Method

Factory Pattern

Delegate object creation at runtime

Creational Design Pattern

GitHub Link:

The Factory Method describes how to solve the recurring problem to design flexible, reusable, object-oriented software.

We need objects, created at runtime, that are easier to implement, change, test, and reuse.

Alt Text

⚡️Problem idea

Create different characters in our game


  • Define an interface for creating an object, but let subclasses decide which class to instantiate
  • It lets a class defer instantiation to subclasses

🔧 Apply when

  • Class can't expect the type of object it must create
  • Subclass must decide what types of objects are create


  • Delegate object creation
  • Hook for subclasses
  • Base class can provide a default implementation

🏆 Great for

  • Delegate object creation at runtime
  • When we don't know what type of class we need to create

GitHub Link:

Top comments (0)