Abstract Factory Pattern

The Abstract Factory pattern: creating whole families of related objects through one interface, so a program can switch every product at once.

  • 8 cards
  • 4 questions
  • About 18 min
  • Intermediate to Advanced
Share

Some objects only work in matching sets: a storage bucket and a message queue from the same cloud provider, or a button and a checkbox from the same visual theme. The Abstract Factory pattern gives a program one object that creates every member of a set, so that swapping that object swaps the whole set and a mismatched pair cannot be made.

This topic starts with the problem of mixed products and the pattern's intent, then reads its participants off a class diagram that grows in two directions, one for product kinds and one for families. The template builds the pure form in Python, Java and TypeScript, with a dictionary of classes as the shorter Python form.

Two examples follow: an application that runs on more than one cloud provider, and the JDK's DocumentBuilderFactory, where the platform rather than your code decides which XML parser you get. The gotcha is the cost the pattern moves rather than removes, which falls on every new kind of product, and the exercise designs light and dark themes for a widget kit and counts what a fourth widget costs.

By the end you can recognise an abstract factory, say why it keeps a family consistent, and predict which change it makes cheap and which it makes expensive.

Contents

8 cardsnot started