DEV Community

rubyhub.io
rubyhub.io

Posted on • Originally published at Medium

Ruby Design Patterns | BUILDER ⚒️

Overview

Builder is a creational design pattern that allows create complex objects step by step.

Introduction

When you hear the word builder, you probably think of a construction worker however, in this case, we should think of a builder as an object that is able to build new objects based on multiple elements. Imagine a restaurant that sells Tortillas. The owner had the idea that each customer would be able to compose their own tortillas and gave the customers several options to choose from:

  • Meat: chicken or mutton
  • Vegetables: cucumbers, tomatoes, and onions
  • Sauce: mild or spicy
  • Spicy sauce has 3 levels: medium, hot, and extra hot

Image description

Code Example

So let's create a Tortilla class that will have attributes such as meat, sauce, and vegetables that will be an array.

class Tortilla
  attr_accessor :meat, :vegetables, :sauce

  def initialize
    @vegetables = []
  end
end
Enter fullscreen mode Exit fullscreen mode

In addition, we will create a few empty classes that will be represented by individual ingredients.

class Chicken; end
class Mutton; end
class Cucumber; end
class Onion; end
class Tomato; end
class MildSauce; end
Enter fullscreen mode Exit fullscreen mode

We will also add an attribute to a spicy sauce that will determine its level of spiciness.

class SpicySauce
  attr_reader :level

  def initialize(level)
    @level = level
  end
end
Enter fullscreen mode Exit fullscreen mode

Now we need to prepare a builder class that will help us create our tortilla.

class TortillaBuilder
  attr_reader :tortilla

  def initialize(tortilla)
    @tortilla = tortilla
  end

  def select_chicken
    tortilla.meat = Chicken.new
  end

  def select_mutton
    tortilla.meat = Mutton.new
  end

  def add_cucumber
    tortilla.vegetables << Cucumber.new
  end

  def add_tomato
    tortilla.vegetables << Tomato.new
  end

  def add_onion
    tortilla.vegetables << Onion.new
  end

  def select_garlic_sauce
    tortilla.sauce = GarlicSauce.new
  end

  def select_spicy_sauce(level = :hot)
    tortilla.sauce = SpicySauce.new(level)
  end
end
Enter fullscreen mode Exit fullscreen mode

Now, using the builder class, we want to create a tortilla that will contain:

  • Chicken meat
  • Two cucumbers
  • One Tomato
  • Extra spicy sauce
builder = TortillaBuilder.new(Tortilla.new)

builder.select_chicken
builder.add_cucumber
builder.add_cucumber
builder.add_tomato
builder.select_spicy_sauce(level: :extra_hot)
Enter fullscreen mode Exit fullscreen mode

At this point, we have created an interface that allows us to quickly and easily create a new object with freely chosen properties.

Top comments (0)