Skip to main content

What It Is

The Builder pattern constructs a complex object step by step. Instead of one constructor with a long, confusing argument list, the builder offers small named steps — and a final build() that returns the finished object.

Each step returns the builder itself, so calls chain into a readable sentence.


Example: Building a Pizza

Compare PizzaBuilder("large").cheese().topping("mushrooms") with a constructor call like Pizza("large", True, ["mushrooms"]) — the builder says what every value means, and optional parts are simply skipped.

Steps that chain

Every step is named and optional: the small pizza skips them all. No True, None, False argument soup.

Prompt
Write a Python program with a PizzaBuilder: it takes the size, cheese and topping steps return the builder for chaining, and build returns the finished Pizza dataclass. Build one large pizza with cheese and two toppings and one plain small pizza, printing both.
Output: complete Python program

When to Use It

Use it when an object has many optional parts, or when construction has real steps that deserve names. Requests, queries, and configurations are classic builder territory.

Skip it for simple objects — a call with keyword arguments, Pizza(size="large"), is already a fine little builder for the easy cases.


Pros and Cons

Pros: readable construction; optional parts without giant argument lists; the object can be validated once, in build().

Cons: a second class to write and maintain; overkill where keyword arguments already read well.