DEV Community

Cover image for Factory Method
Francesco Ciulla
Francesco Ciulla

Posted on • Edited on

Factory Method

Factory Pattern

Delegate object creation at runtime

Creational Design Pattern

GitHub Link: https://github.com/FrancescoXX/Design-Patterns-Factory-Method-Javascript


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


💡Intent

  • 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

✅Pro

  • 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: https://github.com/FrancescoXX/Design-Patterns-Factory-Method-Javascript

Top comments (0)