forked from dancergraham/HeadFirstDesignPatterns_python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpizza_abstract_factory.py
341 lines (238 loc) · 8.18 KB
/
pizza_abstract_factory.py
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
import abc
class Cheese(abc.ABC):
def __str__(self):
raise NotImplementedError
class MozzarellaCheese(Cheese):
def __str__(self):
return "Shredded Mozzarella"
class ReggianoCheese(Cheese):
def __str__(self):
return "Reggiano Cheese"
class Dough(abc.ABC):
def __str__(self):
raise NotImplementedError
class ThickCrustDough(Dough):
def __str__(self):
return "ThickCrust style extra thick crust dough"
class ThinCrustDough(Dough):
def __str__(self):
return "Thin Crust Dough"
class Clams(abc.ABC):
def __str__(self):
raise NotImplementedError
class FreshClams(Clams):
def __str__(self):
return "Fresh Clams from Long Island Sound"
class FrozenClams(Clams):
def __str__(self):
return "Frozen Clams from Chesapeake Bay"
class Sauce(abc.ABC):
def __str__(self):
raise NotImplementedError
class MarinaraSauce(Sauce):
def __str__(self):
return "Marinara Sauce"
class PlumTomatoSauce(Sauce):
def __str__(self):
return "Tomato sauce with plum tomatoes"
class Veggies(abc.ABC):
def __str__(self):
raise NotImplementedError
class Spinach(Veggies):
def __str__(self):
return "Spinach"
class BlackOlives(Veggies):
def __str__(self):
return "Black Olives"
class Eggplant(Veggies):
def __str__(self):
return "Eggplant"
class Garlic(Veggies):
def __str__(self):
return "Garlic"
class Mushroom(Veggies):
def __str__(self):
return "Mushrooms"
class Onion(Veggies):
def __str__(self):
return "Onion"
class RedPepper(Veggies):
def __str__(self):
return "Red Pepper"
class Pepperoni(abc.ABC):
def __str__(self) -> str:
raise NotImplementedError
class SlicedPepperoni(Pepperoni):
def __str__(self) -> str:
return "Sliced Pepperoni"
class PizzaIngredientFactory(abc.ABC):
def create_dough():
raise NotImplementedError
def create_sauce():
raise NotImplementedError
def create_cheese():
raise NotImplementedError
def create_veggies():
raise NotImplementedError
def create_pepperoni():
raise NotImplementedError
def create_clam():
raise NotImplementedError
class ChicagoPizzaIngredientFactory(PizzaIngredientFactory):
def create_dough(self):
return ThickCrustDough()
def create_sauce(self):
return PlumTomatoSauce()
def create_cheese(self):
return MozzarellaCheese()
def create_veggies(self):
return [BlackOlives(), Spinach(), Eggplant()]
def create_pepperoni(self):
return SlicedPepperoni()
def create_clam(self):
return FrozenClams()
class NYPizzaIngredientFactory(PizzaIngredientFactory):
def create_dough(self):
return ThinCrustDough()
def create_sauce(self):
return MarinaraSauce()
def create_cheese(self):
return ReggianoCheese()
def create_veggies(self):
return [Garlic(), Onion(), Mushroom(), RedPepper()]
def create_pepperoni(self):
return SlicedPepperoni()
def create_clam(self):
return FreshClams()
class Pizza:
def __init__(self, ingredient_factory):
self.ingredient_factory = ingredient_factory
self.name = None
self.dough = None
self.sauce = None
self.cheese = None
self.veggies = []
self.pepperoni = None
self.clam = None
def prepare(self):
raise NotImplementedError
def bake(self):
print("Bake for 25 minutes at 350")
def cut(self):
print("Cutting the pizza into diagonal slices")
def box(self):
print("Place pizza in official PizzaStore box")
def set_name(self, name):
self.name = name
def get_name(self):
return self.name
def __str__(self):
result = []
result.append("---- " + self.name + " ----")
if self.dough:
result.append(self.dough)
if self.sauce:
result.append(self.sauce)
if self.cheese:
result.append(self.cheese)
if self.veggies:
result.append(", ".join(map(str, self.veggies)))
if self.clam:
result.append(self.clam)
if self.pepperoni:
result.append(self.pepperoni)
return "\n".join(map(str, result)) + "\n"
class CheesePizza(Pizza):
def prepare(self):
print(f"Preparing {self.name}")
self.dough = self.ingredient_factory.create_dough()
self.sauce = self.ingredient_factory.create_sauce()
self.cheese = self.ingredient_factory.create_cheese()
class ClamPizza(Pizza):
def prepare(self):
print(f"Preparing {self.name}")
self.dough = self.ingredient_factory.create_dough()
self.sauce = self.ingredient_factory.create_sauce()
self.cheese = self.ingredient_factory.create_cheese()
self.clam = self.ingredient_factory.create_clam()
class VeggiePizza(Pizza):
def prepare(self):
print(f"Preparing {self.name}")
self.dough = self.ingredient_factory.create_dough()
self.sauce = self.ingredient_factory.create_sauce()
self.cheese = self.ingredient_factory.create_cheese()
self.veggies = self.ingredient_factory.create_veggies()
class PepperoniPizza(Pizza):
def prepare(self):
print(f"Preparing {self.name}")
self.dough = self.ingredient_factory.create_dough()
self.sauce = self.ingredient_factory.create_sauce()
self.cheese = self.ingredient_factory.create_cheese()
self.veggies = self.ingredient_factory.create_veggies()
self.pepperoni = self.ingredient_factory.create_pepperoni()
class PizzaStore:
def create_pizza(self, item):
raise NotImplementedError
def order_pizza(self, type):
pizza = self.create_pizza(type)
print(f"--- Making a {pizza.name} ---")
pizza.prepare()
pizza.bake()
pizza.cut()
pizza.box()
return pizza
class ChicagoPizzaStore(PizzaStore):
def create_pizza(self, item):
pizza = None
ingredient_factory = ChicagoPizzaIngredientFactory()
if item == "cheese":
pizza = CheesePizza(ingredient_factory)
pizza.name = "Chicago Style Cheese Pizza"
elif item == "veggie":
pizza = VeggiePizza(ingredient_factory)
pizza.name = "Chicago Style Veggie Pizza"
elif item == "clam":
pizza = ClamPizza(ingredient_factory)
pizza.name = "Chicago Style Clam Pizza"
elif item == "pepperoni":
pizza = PepperoniPizza(ingredient_factory)
pizza.name = "Chicago Style Pepperoni Pizza"
return pizza
class NYPizzaStore(PizzaStore):
def create_pizza(self, item):
pizza = None
ingredient_factory = NYPizzaIngredientFactory()
if item == "cheese":
pizza = CheesePizza(ingredient_factory)
pizza.name = "New York Style Cheese Pizza"
elif item == "veggie":
pizza = VeggiePizza(ingredient_factory)
pizza.name = "New York Style Veggie Pizza"
elif item == "clam":
pizza = ClamPizza(ingredient_factory)
pizza.name = "New York Style Clam Pizza"
elif item == "pepperoni":
pizza = PepperoniPizza(ingredient_factory)
pizza.name = "New York Style Pepperoni Pizza"
return pizza
def pizza_test_drive():
ny_store = NYPizzaStore()
chicago_store = ChicagoPizzaStore()
pizza = ny_store.order_pizza("cheese")
print(f"Joel ordered a {pizza}")
pizza = chicago_store.order_pizza("cheese")
print(f"Joel ordered a {pizza}")
pizza = ny_store.order_pizza("clam")
print(f"Joel ordered a {pizza}")
pizza = chicago_store.order_pizza("clam")
print(f"Joel ordered a {pizza}")
pizza = ny_store.order_pizza("pepperoni")
print(f"Joel ordered a {pizza}")
pizza = chicago_store.order_pizza("pepperoni")
print(f"Joel ordered a {pizza}")
pizza = ny_store.order_pizza("veggie")
print(f"Joel ordered a {pizza}")
pizza = chicago_store.order_pizza("veggie")
print(f"Joel ordered a {pizza}")
if __name__ == "__main__":
pizza_test_drive()