-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuilder_pattern.py
More file actions
116 lines (87 loc) · 2.63 KB
/
builder_pattern.py
File metadata and controls
116 lines (87 loc) · 2.63 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
107
108
109
110
111
112
113
114
115
116
from abc import *
from typing import Self
class Pizza():
def __init__(self):
self.parts = []
def add(self, param) -> None:
self.parts.append(param)
def show(self) -> None:
print(f"\033[92myour pizza with {self.parts}\033[0m")
class Builder(ABC):
@abstractmethod
def reset(self): ...
@abstractmethod
def prepare_dough(self): ...
@abstractmethod
def spread_tomato_sauce(self): ...
@abstractmethod
def sprinkle_cheese(self): ...
@abstractmethod
def add_topping(self): ...
@abstractmethod
def grill(self): ...
class PotatoPizzaBuilder(Builder):
def __init__(self):
self.pizza = Pizza()
def reset(self) -> Self:
self.pizza = Pizza()
return self
def prepare_dough(self) -> Self:
self.pizza.add('thin dough')
self.pizza.show()
return self
def spread_tomato_sauce(self) -> Self:
self.pizza.add('tomato sauce')
self.pizza.show()
return self
def sprinkle_cheese(self) -> Self:
self.pizza.add('cheese')
self.pizza.show()
return self
def add_topping(self) -> Self:
self.pizza.add('potato')
self.pizza.add('bacon')
self.pizza.add('mayo')
self.pizza.show()
return self
def grill(self) -> Pizza:
print("your pizza is here!")
return self.pizza
class PeperoniPizzaBuilder(Builder):
def __init__(self):
self.pizza = Pizza()
def reset(self) -> Self:
self.pizza = Pizza()
return self
def prepare_dough(self) -> Self:
self.pizza.add('thin dough')
self.pizza.show()
return self
def spread_tomato_sauce(self) -> Self:
self.pizza.add('tomato sauce')
self.pizza.show()
return self
def sprinkle_cheese(self) -> Self:
self.pizza.add('cheese')
self.pizza.show()
return self
def add_topping(self) -> Self:
self.pizza.add('peperoni')
self.pizza.add('more peperoni')
self.pizza.add('moooooore peperoni')
self.pizza.show()
return self
def grill(self) -> Pizza:
print("your pizza is here!")
return self.pizza
def main():
potato_pizza = (PotatoPizzaBuilder()
.prepare_dough()
.spread_tomato_sauce()
.sprinkle_cheese()
.add_topping()
.grill()
)
pepr_pizza_without_dough = (PeperoniPizzaBuilder().add_topping().grill())
if __name__ == '__main__':
main()