DELAPARRA
Index
Color
NL
Category: Design patterns

Builder

Explained by Updated

What is Builder?

Builder: Separate the construction of a complex object from its representation, allowing the same process to create different types. The Builder pattern constructs a complex object step by step. Instead of one constructor with twelve parameters, you call clear methods one at a time and finish with build(). Code like Query::table('orders')->where('status', 'paid')->orderBy('created_at')->get() is a builder. Each step reads like a sentence, optional parts can be left out, and the final call creates the result.

Definition source Coders Lingo, CC BY 4.0

Frequently asked questions about Builder

What should you keep in mind with Builder?

Builders shine when objects have many optional settings, such as emails, HTTP requests, reports or test data. Laravel's query builder and model factories are everyday examples. For simple objects with two or three required values, a normal constructor is clearer. A builder adds a class, so use it where the readability gain is real.

At a glance

CategoryDesign patterns
Explained by
Updated

More Design patterns terms

All terms starting with B

Skills

Download all skills (zip)

Manuals

Related services

Need any help?

Stuck on one of these in your own project? Tell me what you are working on and I will tell you how I would approach it.