-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathmain.rb
More file actions
106 lines (91 loc) · 2.6 KB
/
main.rb
File metadata and controls
106 lines (91 loc) · 2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
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