codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🎭

Design Patterns

9 / 100 topics
5Introduction to Creational Patterns6Singleton Pattern7Factory Method Pattern8Abstract Factory Pattern9Builder Pattern10Prototype Pattern31Practical Exercises for Creational Patterns
Tutorials/Design Patterns/Builder Pattern
🎭Design Patterns

Builder Pattern

Updated 2026-05-15
10 min read

Builder Pattern

Introduction

The Builder design pattern is one of the creational patterns in software design. It's particularly useful when creating complex objects that require a lot of parameters, many of which are optional. The pattern separates the construction of an object from its representation, allowing the same construction process to create different representations.

In this tutorial, we'll explore how the Builder pattern works and provide practical examples to help you understand and implement it in your projects.

Concept

The main idea behind the Builder pattern is to encapsulate the construction logic into a separate builder class. This allows for more flexible object creation, especially when dealing with complex objects that have multiple optional parameters or require different configurations.

Key Components

  1. Product: The final complex object that needs to be constructed.
  2. Builder: An interface or abstract class defining methods for constructing the product's parts.
  3. Concrete Builder: Implements the builder interface and provides specific implementations for building the product.
  4. Director: Optional component that constructs an object using a builder. It defines the algorithm for assembling the product.

Benefits

  • Encapsulation of Construction Logic: The construction logic is separated from the representation, making it easier to manage and extend.
  • Flexibility: Allows different representations of the same product to be constructed using the same process.
  • Simplified Client Code: Clients only need to interact with the director or builder interface, not with the complex object's construction details.

Examples

Let's dive into some examples to illustrate how the Builder pattern can be implemented in practice.

Example 1: Building a House

Suppose we are building a house and want to use the Builder pattern to encapsulate the construction process. We'll define a House class, a HouseBuilder interface, a concrete builder, and a director.

Step 1: Define the Product

class House {
  constructor(walls, roof, doors, windows) {
    this.walls = walls;
    this.roof = roof;
    this.doors = doors;
    this.windows = windows;
  }

  toString() {
    return `House with ${this.walls} walls, ${this.roof} roof, ${this.doors} door(s), and ${this.windows} window(s).`;
  }
}

#### Step 2: Define the Builder Interface

```jsx
class HouseBuilder {
  buildWalls() {}
  buildRoof() {}
  addDoors() {}
  addWindows() {}
  getResult() {}
}

Step 3: Implement a Concrete Builder

class SimpleHouseBuilder extends HouseBuilder {
  constructor() {
    this.reset();
  }

  reset() {
    this.house = new House(0, '', 0, 0);
  }

  buildWalls(wallCount) {
    this.house.walls = wallCount;
  }

  buildRoof(type) {
    this.house.roof = type;
  }

  addDoors(doorCount) {
    this.house.doors = doorCount;
  }

  addWindows(windowCount) {
    this.house.windows = windowCount;
  }

  getResult() {
    const result = this.house;
    this.reset();
    return result;
  }
}

Step 4: Define a Director (Optional)

class HouseDirector {
  constructor(builder) {
    this.builder = builder;
  }

  constructBasicHouse() {
    this.builder.buildWalls(4);
    this.builder.buildRoof('gable');
    this.builder.addDoors(1);
    this.builder.addWindows(2);
  }
}

Step 5: Use the Builder

const builder = new SimpleHouseBuilder();
const director = new HouseDirector(builder);

director.constructBasicHouse();
const house = builder.getResult();

console.log(house.toString());
// Output: House with 4 walls, gable roof, 1 door(s), and 2 window(s).

Example 2: Building a Pizza

Let's consider another example where we build a pizza using the Builder pattern.

Step 1: Define the Product

class Pizza {
  constructor(size, crust, toppings) {
    this.size = size;
    this.crust = crust;
    this.toppings = toppings;
  }

  toString() {
    return `Pizza with ${this.size} size, ${this.crust} crust, and toppings: ${this.toppings.join(', ')}.`;
  }
}

Step 2: Define the Builder Interface

class PizzaBuilder {
  setSize(size) {}
  setCrust(crust) {}
  addTopping(topping) {}
  getResult() {}
}

Step 3: Implement a Concrete Builder

class MargheritaPizzaBuilder extends PizzaBuilder {
  constructor() {
    this.reset();
  }

  reset() {
    this.pizza = new Pizza('medium', 'thin', []);
  }

  setSize(size) {
    this.pizza.size = size;
  }

  setCrust(crust) {
    this.pizza.crust = crust;
  }

  addTopping(topping) {
    this.pizza.toppings.push(topping);
  }

  getResult() {
    const result = this.pizza;
    this.reset();
    return result;
  }
}

Step 4: Use the Builder

const builder = new MargheritaPizzaBuilder();

builder.setSize('large');
builder.setCrust('thick');
builder.addTopping('mozzarella');
builder.addTopping('basil');

const pizza = builder.getResult();
console.log(pizza.toString());
// Output: Pizza with large size, thick crust, and toppings: mozzarella, basil.

What's Next?

In the next section, we'll explore another creational pattern called the Prototype Pattern. The Prototype pattern allows you to create new objects by copying existing ones, which can be particularly useful when object creation is complex or resource-intensive.

Stay tuned for more insights into design patterns and how they can help you write better software!


PreviousAbstract Factory PatternNext Prototype Pattern

Recommended Gear

Abstract Factory PatternPrototype Pattern