Builder Design Pattern: Crafting Objects with Style

Imagine you're customizing your dream burger at a fancy restaurant. You get to choose the bun, patty, toppings, and sauces. The Builder Design Pattern is like having a skilled chef build your burger – it allows for the step-by-step construction of complex objects, ensuring a well-assembled and customizable end result.

Concept:

  • Builder: Abstract interface for creating parts of a complex object.
  • ConcreteBuilder: Implements the Builder interface, constructing and assembling parts of the object.
  • Director: Guides the construction process using a builder.
  • Product: Represents the complex object being constructed.
  • Client: Initiates the construction process through a director.

Example:

Let's dive into a delicious example by building customizable sandwiches using the Builder Pattern.

Builder Interface:

# Builder Interface
class SandwichBuilder
  def build_bun; end

  def build_patty; end

  def build_toppings; end

  def build_sauce; end

  def sandwich
    raise NotImplementedError, 'Subclasses must implement the sandwich method'
  end
end

ConcreteBuilder:

# ConcreteBuilder: VeggieSandwichBuilder
class VeggieSandwichBuilder < SandwichBuilder
  def initialize
    @sandwich = Sandwich.new
  end

  def build_bun
    @sandwich.add('Whole Grain Bun')
  end

  def build_patty
    @sandwich.add('Veggie Patty')
  end

  def build_toppings
    @sandwich.add('Lettuce, Tomato, Onion')
  end

  def build_sauce
    @sandwich.add('Mayo, Mustard')
  end

  def sandwich
    @sandwich
  end
end

Director:

# Director
class SandwichMaker
  def make_sandwich(builder)
    builder.build_bun
    builder.build_patty
    builder.build_toppings
    builder.build_sauce
  end
end

Product:

# Product: Sandwich
class Sandwich
  def initialize
    @parts = []
  end

  def add(part)
    @parts << part
  end

  def display
    puts 'Sandwich Parts:'
    @parts.each { |part| puts "- #{part}" }
  end
end

Client Code:

# Client code using the Builder Pattern
veggie_builder = VeggieSandwichBuilder.new
sandwich_maker = SandwichMaker.new

sandwich_maker.make_sandwich(veggie_builder)
veggie_sandwich = veggie_builder.sandwich

veggie_sandwich.display
# Outputs:
# Sandwich Parts:
# - Whole Grain Bun
# - Veggie Patty
# - Lettuce, Tomato, Onion
# - Mayo, Mustard

In this sandwich-making extravaganza:

  • SandwichBuilder is the builder interface with methods for building different parts of a sandwich.
  • VeggieSandwichBuilder is a concrete builder implementing the builder interface to construct a veggie sandwich.
  • SandwichMaker is the director guiding the construction process using a builder.
  • Sandwich is the product representing the final complex object.

Just like customizing your dream sandwich, the Builder Pattern allows you to create objects step by step with different options. It's perfect for constructing complex objects in a flexible and organized manner. So, go ahead and build your objects with style – the builder way!