Skip to content

Commit a3b624d

Browse files
committed
Working pizza factory
1 parent be0e312 commit a3b624d

File tree

1 file changed

+144
-39
lines changed

1 file changed

+144
-39
lines changed

chapter04_factory/pizza_abstract_factory.py

Lines changed: 144 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def __str__(self):
3737

3838

3939
class FreshClams(Clams):
40-
def __str__self():
40+
def __str__(self):
4141
return "Fresh Clams from Long Island Sound"
4242

4343

@@ -114,68 +114,84 @@ def __str__(self) -> str:
114114
class PizzaIngredientFactory(abc.ABC):
115115
def create_dough():
116116
raise NotImplementedError
117+
117118
def create_sauce():
118119
raise NotImplementedError
120+
119121
def create_cheese():
120122
raise NotImplementedError
123+
121124
def create_veggies():
122125
raise NotImplementedError
126+
123127
def create_pepperoni():
124128
raise NotImplementedError
129+
125130
def create_clam():
126131
raise NotImplementedError
127132

128133

129134
class ChicagoPizzaIngredientFactory(PizzaIngredientFactory):
130-
def create_dough():
135+
def create_dough(self):
131136
return ThickCrustDough()
132-
def create_sauce():
137+
138+
def create_sauce(self):
133139
return PlumTomatoSauce()
134-
def create_cheese():
140+
141+
def create_cheese(self):
135142
return MozzarellaCheese()
136-
def create_veggies():
137-
return[BlackOlives(), Spinach(), Eggplant()]
138-
def create_pepperoni():
143+
144+
def create_veggies(self):
145+
return [BlackOlives(), Spinach(), Eggplant()]
146+
147+
def create_pepperoni(self):
139148
return SlicedPepperoni()
140-
def create_clam():
149+
150+
def create_clam(self):
141151
return FrozenClams()
142152

143153

144154
class NYPizzaIngredientFactory(PizzaIngredientFactory):
145-
def create_dough():
155+
def create_dough(self):
146156
return ThinCrustDough()
147-
def create_sauce():
157+
158+
def create_sauce(self):
148159
return MarinaraSauce()
149-
def create_cheese():
160+
161+
def create_cheese(self):
150162
return ReggianoCheese()
151-
def create_veggies():
152-
return[Garlic(), Onion(), Mushroom(), RedPepper()]
153-
def create_pepperoni():
163+
164+
def create_veggies(self):
165+
return [Garlic(), Onion(), Mushroom(), RedPepper()]
166+
167+
def create_pepperoni(self):
154168
return SlicedPepperoni()
155-
def create_clam():
169+
170+
def create_clam(self):
156171
return FreshClams()
157172

158173

159174
class Pizza:
160-
def __init__(self):
175+
def __init__(self, ingredient_factory):
176+
self.ingredient_factory = ingredient_factory
161177
self.name = None
162178
self.dough = None
163179
self.sauce = None
164180
self.cheese = None
165181
self.veggies = []
166182
self.pepperoni = None
167-
self.clams = None
168-
169-
def prepare():
183+
self.clam = None
184+
185+
def prepare(self):
170186
raise NotImplementedError
171187

172-
def bake():
188+
def bake(self):
173189
print("Bake for 25 minutes at 350")
174190

175-
def cut():
191+
def cut(self):
176192
print("Cutting the pizza into diagonal slices")
177193

178-
def box():
194+
def box(self):
179195
print("Place pizza in official PizzaStore box")
180196

181197
def set_name(self, name):
@@ -186,32 +202,62 @@ def get_name(self):
186202

187203
def __str__(self):
188204
result = []
189-
result.append("---- " + name + " ----\n")
205+
result.append("---- " + self.name + " ----")
190206
if self.dough:
191207
result.append(self.dough)
192-
result.append("\n")
193-
208+
194209
if self.sauce:
195210
result.append(self.sauce)
196-
result.append("\n")
197-
211+
198212
if self.cheese:
199213
result.append(self.cheese)
200-
result.append("\n")
201-
214+
202215
if self.veggies:
203-
result.append(", ".join(self.veggies))
204-
result.append("\n")
205-
216+
result.append(", ".join(map(str,self.veggies)))
217+
206218
if self.clam:
207219
result.append(self.clam)
208-
result.append("\n")
209-
220+
210221
if self.pepperoni:
211222
result.append(self.pepperoni)
212-
result.append("\n")
213223

214-
return "".join(result)
224+
return "\n".join(map(str, result))+"\n"
225+
226+
227+
class CheesePizza(Pizza):
228+
def prepare(self):
229+
print(f"Preparing {self.name}")
230+
self.dough = self.ingredient_factory.create_dough()
231+
self.sauce = self.ingredient_factory.create_sauce()
232+
self.cheese = self.ingredient_factory.create_cheese()
233+
234+
235+
class ClamPizza(Pizza):
236+
def prepare(self):
237+
print(f"Preparing {self.name}")
238+
self.dough = self.ingredient_factory.create_dough()
239+
self.sauce = self.ingredient_factory.create_sauce()
240+
self.cheese = self.ingredient_factory.create_cheese()
241+
self.clam = self.ingredient_factory.create_clam()
242+
243+
244+
class VeggiePizza(Pizza):
245+
def prepare(self):
246+
print(f"Preparing {self.name}")
247+
self.dough = self.ingredient_factory.create_dough()
248+
self.sauce = self.ingredient_factory.create_sauce()
249+
self.cheese = self.ingredient_factory.create_cheese()
250+
self.veggies = self.ingredient_factory.create_veggies()
251+
252+
253+
class PepperoniPizza(Pizza):
254+
def prepare(self):
255+
print(f"Preparing {self.name}")
256+
self.dough = self.ingredient_factory.create_dough()
257+
self.sauce = self.ingredient_factory.create_sauce()
258+
self.cheese = self.ingredient_factory.create_cheese()
259+
self.veggies = self.ingredient_factory.create_veggies()
260+
self.pepperoni = self.ingredient_factory.create_pepperoni()
215261

216262

217263
class PizzaStore:
@@ -220,7 +266,7 @@ def create_pizza(self, item):
220266

221267
def order_pizza(self, type):
222268
pizza = self.create_pizza(type)
223-
print(f"--- Making a {pizza.get_name()} ---")
269+
print(f"--- Making a {pizza.name} ---")
224270
pizza.prepare()
225271
pizza.bake()
226272
pizza.cut()
@@ -232,5 +278,64 @@ class ChicagoPizzaStore(PizzaStore):
232278
def create_pizza(self, item):
233279
pizza = None
234280
ingredient_factory = ChicagoPizzaIngredientFactory()
235-
if item == 'cheese':
236-
pizza = new Chee
281+
if item == "cheese":
282+
pizza = CheesePizza(ingredient_factory)
283+
pizza.name = "Chicago Style Cheese Pizza"
284+
elif item == "veggie":
285+
pizza = VeggiePizza(ingredient_factory)
286+
pizza.name = "Chicago Style Veggie Pizza"
287+
elif item == "clam":
288+
pizza = ClamPizza(ingredient_factory)
289+
pizza.name = "Chicago Style Clam Pizza"
290+
elif item == "pepperoni":
291+
pizza = PepperoniPizza(ingredient_factory)
292+
pizza.name = "Chicago Style Pepperoni Pizza"
293+
return pizza
294+
295+
296+
class NYPizzaStore(PizzaStore):
297+
def create_pizza(self, item):
298+
pizza = None
299+
ingredient_factory = NYPizzaIngredientFactory()
300+
if item == "cheese":
301+
pizza = CheesePizza(ingredient_factory)
302+
pizza.name = "New York Style Cheese Pizza"
303+
elif item == "veggie":
304+
pizza = VeggiePizza(ingredient_factory)
305+
pizza.name = "New York Style Veggie Pizza"
306+
elif item == "clam":
307+
pizza = ClamPizza(ingredient_factory)
308+
pizza.name = "New York Style Clam Pizza"
309+
elif item == "pepperoni":
310+
pizza = PepperoniPizza(ingredient_factory)
311+
pizza.name = "New York Style Pepperoni Pizza"
312+
return pizza
313+
314+
315+
def pizza_test_drive():
316+
ny_store = NYPizzaStore()
317+
chicago_store = ChicagoPizzaStore()
318+
319+
pizza = ny_store.order_pizza("cheese")
320+
print(f"Joel ordered a {pizza}")
321+
pizza = chicago_store.order_pizza("cheese")
322+
print(f"Joel ordered a {pizza}")
323+
324+
pizza = ny_store.order_pizza("clam")
325+
print(f"Joel ordered a {pizza}")
326+
pizza = chicago_store.order_pizza("clam")
327+
print(f"Joel ordered a {pizza}")
328+
329+
pizza = ny_store.order_pizza("pepperoni")
330+
print(f"Joel ordered a {pizza}")
331+
pizza = chicago_store.order_pizza("pepperoni")
332+
print(f"Joel ordered a {pizza}")
333+
334+
pizza = ny_store.order_pizza("veggie")
335+
print(f"Joel ordered a {pizza}")
336+
pizza = chicago_store.order_pizza("veggie")
337+
print(f"Joel ordered a {pizza}")
338+
339+
340+
if __name__ == "__main__":
341+
pizza_test_drive()

0 commit comments

Comments
 (0)