Standalone conceptPatternChapter I · Domain · Building block 11 of 18

Factory

A factory groups complex or business-significant creation without taking over later state changes.

  1. InputsCustomer, line items and address.
  2. FactoryChecks preconditions and creates.
  3. AggregateStarts in a valid state.

Context

Why this is important

Create valid domain objects with explicit construction rules.

Creation of new entities or aggregates and validation of their initial conditions.

Shop example · Mapping

Factory and direct construction

The order factory creates the aggregate; the repository stores it. Both belong to the Ordering module.

This is how you read the picture

  1. Inputs Order data
  2. Factory Construction
  3. Aggregate valid start
Factory and direct construction
CriterionSituationSuitable form
Simple valuesConstructorFew rules
Aggregates with rulesFactoryChecked preconditions
The matrix helps assess construction effort and responsibility.

Benefits & limitations

Helps if:

  • Construction groups several domain preconditions or assembles an aggregate from several valid parts.
  • Callers should receive a fully valid object without needing to know internal construction steps or the concrete implementation.

Boundaries

  • For an object with a few clear required values, a constructor remains easier to understand and maintain.
  • A factory establishes the valid initial state; persistence, retrieval, and later changes belong to other components.

In practice

  1. Identify the preconditions a new object must satisfy and express them as a business creation rule rather than a collection of technical setters.
  2. Use a factory when an aggregate requires several objects or callers should not know its concrete implementation. A constructor is enough for simple, unambiguous creation.
  3. Return a fully valid object and test invalid input, boundaries and each business precondition. Keep storage access and later state changes outside the creation contract.
Read the detailed explanation

A factory can create one object or a composed aggregate. Its work ends after valid construction; persistence and later state changes belong to other components.

A factory is useful when preconditions or composition need explanation. A simple constructor is preferable when it ensures the same validity directly and clearly.

The factory creates the Aggregate in a valid initial state; a Repository handles storage afterward.

Check your understanding

Test yourself in three stages

  1. ReadingCheck in the picture

    Which inputs does the order factory use?

    View the solution with justification

    Customer, line items and confirmed delivery address.

  2. ApplyingTransferred to a new situation

    A simple immutable value has no preconditions. What construction is enough?

    View the solution with justification

    A direct constructor.

  3. EvaluateWeighing an alternative

    When does a factory provide clear domain value?

    View the solution with justification

    When it groups several initial invariants or gives complex creation a clear business name.

Explore further

These building blocks continue the thoughts of this page.

Continue on the learning path

Next in Domain Modelling & DDD: Repository.

18 Building blocks

Continue withRepository