M2 · Separating Object CreationSession 4-5CreationalKotlin replaces itOutline

Intent. Separate the construction of a complex object from its representation.

In Kotlin. Named and default arguments cover the common case; @DslMarker builders cover the rest.

Kotlin takes most of this pattern away. Named and default arguments handle the ordinary case; what survives is step-by-step validation, type-safe builder DSLs, and Java interop. Worth studying precisely to see where the language stops helping.

Structure

classDiagram
    class Builder {
        <<interface>>
        +setPartA(v) Builder
        +setPartB(v) Builder
        +build() Product
    }
    class ConcreteBuilder
    class Director {
        +construct(b) Product
    }
    class Product
    Builder <|.. ConcreteBuilder
    Director o--> Builder
    ConcreteBuilder ..> Product : builds

Telling It Apart

Patterns whose structure looks the same but whose intent does not. See the Confusing Pairs reference.

  • Abstract Factory — Create whole families of related objects that must be used together, without naming their concrete classes.