WorkspaceLow-Level Design
← Course overview

Course navigation

On this page

Low-Level Design / Creational Patterns

pattern · 12 MIN READ

Factory Method

Factory Method moves a product creation decision into an overridable operation. The creator’s workflow can use the product contract without knowing its concrete class.

Reading completion is independent of quizzes and challenges.

What you’ll understand

  • Factory Method moves a product creation decision into an overridable operation. The creator’s workflow can use the product contract without knowing its concrete class.
  • The base creator calls createTransport; specialized creators return products with the same deliver operation.
  • Evaluate the tradeoff: Creator subclasses add structure. A simple factory function may be enough when only construction varies and there is no creator workflow to specialize.

Useful first: Classes and Objects

The idea and when to recognize it

Factory Method moves a product creation decision into an overridable operation. The creator’s workflow can use the product contract without knowing its concrete class.

  • Requirement: A logistics workflow creates a transport and asks it to deliver a parcel.
  • Change to anticipate: Allow sea logistics to create a ship while retaining the same dispatch workflow.
  • Design goal: The base creator calls createTransport; specialized creators return products with the same deliver operation.

Start with the concrete problem

A logistics workflow creates a transport and asks it to deliver a parcel.

Allow sea logistics to create a ship while retaining the same dispatch workflow.

Direct implementationRevised design
Hard-coding Truck inside dispatch prevents a sea creator from providing its intended product.The base creator calls createTransport; specialized creators return products with the same deliver operation.
The baseline case can hide the problem. Compare the extension and boundary fixtures before accepting this design.Creator subclasses add structure. A simple factory function may be enough when only construction varies and there is no creator workflow to specialize.

Responsibilities and contracts

The base creator calls createTransport; specialized creators return products with the same deliver operation.

  • Logistics: Owns dispatch workflow. Public operations: createTransport(), dispatch()
  • SeaLogistics: Creates a ship product. Public operations: createTransport()
  • Ship: Fulfills delivery contract. Public operations: deliver()

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

An exporter creator can choose a CSV or JSON writer while retaining a common export workflow.

  • 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

Creator subclasses add structure. A simple factory function may be enough when only construction varies and there is no creator workflow to specialize.

Common mistake: Hard-coding Truck inside dispatch prevents a sea creator from providing its intended product.

  • Explain the choice: The base creator calls createTransport; specialized creators return products with the same deliver operation.
  • Challenge your choice: Creator subclasses add structure. A simple factory function may be enough when only construction varies and there is no creator workflow to specialize.
  • 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.