WorkspaceLow-Level Design
← Course overview

Course navigation

On this page

Low-Level Design / Creational Patterns

pattern · 12 MIN READ

Abstract Factory

Abstract Factory creates related products as a coherent family. It is useful when the client should not mix implementations that must work together.

Reading completion is independent of quizzes and challenges.

What you’ll understand

  • Abstract Factory creates related products as a coherent family. It is useful when the client should not mix implementations that must work together.
  • A theme factory supplies both product creation operations and is selected once at the composition boundary.
  • Evaluate the tradeoff: Adding another family is easy, but adding a new product kind changes every factory contract. Use this pattern when family consistency is a real requirement.

Useful first: Classes and Objects

The idea and when to recognize it

Abstract Factory creates related products as a coherent family. It is useful when the client should not mix implementations that must work together.

  • Requirement: A UI requires a button and checkbox from the same theme family.
  • Change to anticipate: Switch the complete product family from light to dark without leaving one control in the old theme.
  • Design goal: A theme factory supplies both product creation operations and is selected once at the composition boundary.

Start with the concrete problem

A UI requires a button and checkbox from the same theme family.

Switch the complete product family from light to dark without leaving one control in the old theme.

Direct implementationRevised design
Selecting a button theme independently while always using a light checkbox creates an incompatible family.A theme factory supplies both product creation operations and is selected once at the composition boundary.
The baseline case can hide the problem. Compare the extension and boundary fixtures before accepting this design.Adding another family is easy, but adding a new product kind changes every factory contract. Use this pattern when family consistency is a real requirement.

Responsibilities and contracts

A theme factory supplies both product creation operations and is selected once at the composition boundary.

  • ThemeFactory: Creates compatible controls. Public operations: button(), checkbox()
  • Button: Renders one themed control. Public operations: theme
  • Checkbox: Uses the same family. Public operations: theme

Read the design

Select a participant to inspect its responsibility and public operations. Follow the labeled relationships; an arrow indicates direction, while the legend distinguishes ownership, substitution, and temporary use.

Preparing the design walkthrough…

Follow the interaction

Choose a supplied workload and compare the direct and revised implementations. Step through the same operation in both languages. Object values are recorded after each highlighted statement; previous steps remain unchanged.

Preparing the design walkthrough…

Implementation and expected output

Read the complete example alongside its input and expected output. The implementation selector changes the displayed program; the language selector preserves your current scenario and playback position.

Preparing the design walkthrough…

Try the idea in another setting

A document exporter can create a matching serializer and schema validator from one format factory.

  • Name the new client contract before adapting the example.
  • Identify which state belongs to each object and who controls its lifetime.
  • Write one normal case and one boundary case before extending the design.

Tradeoffs and interview takeaways

Adding another family is easy, but adding a new product kind changes every factory contract. Use this pattern when family consistency is a real requirement.

Common mistake: Selecting a button theme independently while always using a light checkbox creates an incompatible family.

  • Explain the choice: A theme factory supplies both product creation operations and is selected once at the composition boundary.
  • Challenge your choice: Adding another family is easy, but adding a new product kind changes every factory contract. Use this pattern when family consistency is a real requirement.
  • Describe a requirement that would make the simpler implementation sufficient.

Design challenge and knowledge check

Preparing the design walkthrough…

Make it your own

Saved on this device · 0/20,000

Reading completion is independent of quizzes and challenges.