Skip to content
StackDecode
System Design
15. Software Design PatternsAnimated walkthrough

Factory Pattern

A method that creates objects for you.

The idea

Callers ask a factory for an object instead of calling constructors, hiding which class is created.

Watch it work

The steps light up one at a time. Use the controls to pause or step through.

  1. 1Define an interface
  2. 2Implement several classes
  3. 3Factory chooses one
  4. 4Caller uses the interface
1/4

Step by step

  1. 1Define an interface
  2. 2Implement several classes
  3. 3Factory chooses one
  4. 4Caller uses the interface

Where it is used

Creating objects by type or config.

Watch out for

Too many factories add indirection.

Related