Implementing the Builder Pattern in Ruby

Implementing the Builder Pattern in Ruby

Welcome back! So far, we've covered various creational design patterns like the Singleton Pattern and the Factory Method Pattern. These patterns have helped you control and simplify object creation in your programs. Today, we are delving into another powerful creational pattern — the Builder Pattern. This pattern allows you to construct complex objects step by step, making the creation process more manageable and modular.

What You'll Learn

In this lesson, you will learn how to implement the Builder Pattern in Ruby. Specifically, you'll understand how to:

  1. Define the Builder Pattern: Recognize the core components and when to use this pattern.
  2. Implement a Concrete Builder: See how to create concrete builders for constructing complex objects.
  3. Use a Director: Use a Director class to manage the construction process.

Here's a snippet of the code you'll work with:

The House.rb file defines the House class — the product being constructed:

Ruby
# Product House that will be constructed using builder pattern
class House
  attr_accessor :foundation, :structure, :roof

  # Display the house properties
  def show_house
    puts "House with #{foundation}, #{structure}, and #{roof}."
  end
end

The house_builder.rb file defines the HouseBuilder interface:

Ruby
# Abstract class for HouseBuilder with methods to build foundation, structure, and roof
class HouseBuilder
  def build_foundation; end
  def build_structure; end
  def build_roof; end
  def get_house; end
end

The concrete_house_builder.rb file implements the HouseBuilder interface:

Ruby
# Concrete builder class for constructing a house with concrete foundation, structure, and roof
class ConcreteHouseBuilder < HouseBuilder
  def initialize
    @house = House.new
  end

  def build_foundation
    @house.foundation = "Concrete Foundation"
  end

  def build_structure
    @house.structure = "Concrete Structure"
  end

  def build_roof
    @house.roof = "Concrete Roof"
  end

  def get_house
    @house
  end
end

The wooden_house_builder.rb file implements the HouseBuilder interface:

Ruby
# Concrete builder class for constructing a house with wooden foundation, structure, and roof
class WoodenHouseBuilder < HouseBuilder
  def initialize
    @house = House.new
  end

  def build_foundation
    @house.foundation = "Wooden Foundation"
  end

  def build_structure
    @house.structure = "Wooden Structure"
  end

  def build_roof
    @house.roof = "Wooden Roof"
  end

  def get_house
    @house
  end
end

The brick_house_builder.rb file implements the HouseBuilder interface:

Ruby
# Concrete builder class for constructing a house with brick foundation, structure, and roof
class BrickHouseBuilder < HouseBuilder
  def initialize
    @house = House.new
  end

  def build_foundation
    @house.foundation = "Brick Foundation"
  end

  def build_structure
    @house.structure = "Brick Structure"
  end

  def build_roof
    @house.roof = "Brick Roof"
  end

  def get_house
    @house
  end
end

The main.rb file demonstrates the Builder Pattern:

Ruby
# Director class to manage the construction process by encapsulating the steps within the `construct_house` method
class Director
  attr_accessor :builder

  def construct_house
    builder.build_foundation
    builder.build_structure
    builder.build_roof
    builder.get_house
  end
end

# Execute the builder pattern
director = Director.new  # Director to manage the construction process
builder = BrickHouseBuilder.new  # Concrete builder to build a brick house
director.builder = builder  # Set the builder to construct a brick house
house = director.construct_house  # Construct the house
house.show_house  # Display the constructed house

This example demonstrates how to use the Builder Pattern to create a House object step by step.

The Builder Pattern has the following components:

  • Product: The object being constructed. In this example, House is the product.
  • Builder: An abstract class that defines the steps for constructing the product. In this example, HouseBuilder is the builder.
  • Concrete Builder: A concrete class that implements the builder interface to construct the product. In this example, ConcreteHouseBuilder, WoodenHouseBuilder, and BrickHouseBuilder are concrete builders. In real-world scenarios, the builder method (the construct_house in this example) can be more complex and have multiple steps and conditions to build the actual product, but for simplicity, we just call the three methods to build the product.

The Director class manages the construction process by using a builder to create the product. In this example, the Director class sets the builder to BrickHouseBuilder and constructs a House object.

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal