Skip to content

Real world example of the Builder pattern #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 106 additions & 0 deletions src/builder/real_world/main.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
# EN: Pizza is the product that the builder class is going to be building
#
# RU: Pizza - это продукт, который будет строить класс строителей
#
class Pizza
attr_accessor :toppings

def initialize(crust)
@crust = crust
@toppings = []
end

def present
puts 'This pizza is a'
puts "#{@crust} pizza"
puts 'with:'
puts @toppings.join("\n")
puts '-*-*--*--*--*--*--*-'
end
end

# EN: PizzaBuilder is an abstract interface that allows us to add toppings to
# the pizza.
#
# RU: PizzaBuilder - это абстрактный интерфейс, который позволяет нам добавлять
# начинки в пиццу.
#
class PizzaBuilder
attr_reader :pizza

def add_tomato_sauce
@pizza.toppings << 'Tomato sauce'
self
end

def add_cheese
@pizza.toppings << 'Cheese'
self
end

def add_basil
@pizza.toppings << 'Basil'
self
end

def add_pepperoni
@pizza.toppings << 'Pepperoni'
self
end
end

# EN: ThinCrustPizzaBuilder and StuffedCrustPizzaBuilder are the concrete
# builder classes used to build pizzas with specific crust types.
#
# RU: ThinCrustpizzabuilder и FackedCrustpizzabuilder - это бетонные классы,
# используемые для строительства пиццы с определенными типами коров.
#
class ThinCrustPizzaBuilder < PizzaBuilder
def initialize
@pizza = Pizza.new('Thin crust')
end
end

class StuffedCrustPizzaBuilder < PizzaBuilder
def initialize
@pizza = Pizza.new('Stuffed crust')
end
end

# EN: The Chef class act as the Director using the builder provided to make the
# pizzas that are requested.
#
# RU: Класс Chef -повара действует в качестве директора, использующего
# застройщика, предоставленного для изготовления запрашиваемой пиццы.
#
class Chef
attr_accessor :builder

def make_margherita_pizza
@builder.add_tomato_sauce
.add_cheese
.add_basil
end

def make_pepperoni_pizza
@builder.add_tomato_sauce
.add_cheese
.add_pepperoni
end
end

# EN: This is an example in the real world
#
# RU: Это пример в реальном мире
#
chef = Chef.new

thin_crust_builder = ThinCrustPizzaBuilder.new
chef.builder = thin_crust_builder
chef.make_margherita_pizza
thin_crust_builder.pizza.present

stuffed_crust_builder = StuffedCrustPizzaBuilder.new
chef.builder = stuffed_crust_builder
chef.make_pepperoni_pizza
stuffed_crust_builder.pizza.present
14 changes: 14 additions & 0 deletions src/builder/real_world/output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
This pizza is a
Thin crust pizza
with:
Tomato sauce
Cheese
Basil
-*-*--*--*--*--*--*-
This pizza is a
Stuffed crust pizza
with:
Tomato sauce
Cheese
Pepperoni
-*-*--*--*--*--*--*-