Factory Method Pattern

A subclass decides which concrete object to create, so the code that uses the object never names its class.

Overview

Factory Method is a creational pattern from the Gang of Four book (Gamma, Helm, Johnson and Vlissides, *Design Patterns*, 1994). The idea: a class needs an object but should not decide which concrete class to build. So it declares a method that returns the object, and leaves the choice to subclasses.

The class that declares the method is the Creator. The thing it returns is the Product, described by an interface. Each Concrete Creator overrides the method to return its own Concrete Product. The rest of the Creator's code calls the product through the interface and never learns which class it got.

Interviewers like this pattern because it is a small, clean example of the Open/Closed Principle: a new product means a new subclass, not an edit to existing code. A strong answer draws the two parallel hierarchies, writes the method, and then says honestly when the pattern is more than the problem needs.

A Python example that runs

The export method is the same for every exporter. Only create_writer differs.

Three factories that get mixed up

  • In the GoF book — No; a common idiom — Yes — Yes
  • What decides the class — An if or a lookup on a parameter — The subclass that overrides the method — The concrete factory object you were given
  • Adding a product — Edit the function — Add a subclass — A new family: one new factory. A new kind of part: a method on every factory
  • Returns — One product — One product — A family of related products
  • Good for — A handful of types that rarely change — A base class whose steps are fixed but one part varies — Sets of products that must match each other

Where you meet it for real

  • Java Iterable.iterator() — Every collection class returns its own Iterator implementation; for loops use only the Iterator interface
  • Framework hooks — A base view, handler or test case declares a method such as create_client() that your subclass overrides
  • Test doubles — A test subclass overrides the factory method to return a fake product; nothing else changes

What you will be able to do

  • Draw the two hierarchies and name the four roles without hesitating.
  • Write the pattern in Python from memory, with the fixed steps in the base class and the varying step in the subclass.
  • Tell Factory Method apart from a simple factory and from Abstract Factory, and say which one a given problem needs.
  • Explain how the pattern serves the Open/Closed and Dependency Inversion principles, and when a plain function does the same job with less code.

Interview questions

  • What is the difference between a simple factory and the Factory Method pattern?
  • How does Factory Method support the Open/Closed and Dependency Inversion principles?
  • How does Factory Method relate to Template Method and Abstract Factory?
  • When is Factory Method more than the problem needs?
  • How do you test a class that uses a Factory Method?

Sign up free to read the full topic

More Low-Level Design topics

Start free on Cariara · Coding problems · System design studio