Skip to content

Commit 0f18e99

Browse files
committed
Update factory_method.py
the old one is too simple.
1 parent cf9cc12 commit 0f18e99

File tree

1 file changed

+112
-40
lines changed

1 file changed

+112
-40
lines changed

factory_method.py

Lines changed: 112 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,43 +3,115 @@
33

44
"""http://ginstrom.com/scribbles/2007/10/08/design-patterns-python-style/"""
55

6-
7-
class GreekGetter:
8-
9-
"""A simple localizer a la gettext"""
10-
11-
def __init__(self):
12-
self.trans = dict(dog="σκύλος", cat="γάτα")
13-
14-
def get(self, msgid):
15-
"""We'll punt if we don't have a translation"""
16-
try:
17-
return self.trans[msgid]
18-
except KeyError:
19-
return str(msgid)
20-
21-
22-
class EnglishGetter:
23-
24-
"""Simply echoes the msg ids"""
25-
26-
def get(self, msgid):
27-
return str(msgid)
28-
29-
30-
def get_localizer(language="English"):
31-
"""The factory method"""
32-
languages = dict(English=EnglishGetter, Greek=GreekGetter)
33-
return languages[language]()
34-
35-
# Create our localizers
36-
e, g = get_localizer(language="English"), get_localizer(language="Greek")
37-
# Localize some text
38-
for msgid in "dog parrot cat bear".split():
39-
print(e.get(msgid), g.get(msgid))
40-
41-
### OUTPUT ###
42-
# dog σκύλος
43-
# parrot parrot
44-
# cat γάτα
45-
# bear bear
6+
#!/usr/bin/python
7+
8+
9+
class Pizza:
10+
name = ""
11+
dough = ""
12+
sauce = ""
13+
toppings = []
14+
15+
def prepare(self):
16+
print "Preparing %s" % self.name
17+
print " dough: %s" % self.dough
18+
print " sauce: %s" % self.sauce
19+
print " add toppings:"
20+
for n in self.toppings:
21+
print " %s" % n
22+
23+
def bake(self):
24+
print "Bake for 25 minutes at 350."
25+
26+
def cut(self):
27+
print "Cutting into diagonal slices."
28+
29+
def box(self):
30+
print "Put into official box."
31+
32+
def get_name(self):
33+
return self.name
34+
35+
36+
class PizzaStore:
37+
def order_pizza(self, pizza_type):
38+
self.pizza = self.create_pizza(pizza_type)
39+
self.pizza.prepare()
40+
self.pizza.bake()
41+
self.pizza.cut()
42+
self.pizza.box()
43+
return self.pizza
44+
45+
def create_pizza(self, pizza_type):
46+
pass
47+
48+
49+
class NYStyleCheesePizza(Pizza):
50+
def __init__(self):
51+
self.name = "NY Style Cheese Pizza"
52+
self.dough = "NY Dough"
53+
self.sauce = "NY Sauce"
54+
self.toppings.append("NY toopping A")
55+
self.toppings.append("NY toopping B")
56+
57+
58+
class ChicagoStyleCheesePizza(Pizza):
59+
def __init__(self):
60+
self.name = "Chicago Style Cheese Pizza"
61+
self.dough = "Chicago Dough"
62+
self.sauce = "Chicago Sauce"
63+
sefl.toppings.append("Chicago toopping A")
64+
65+
def cut(self):
66+
print "Cutting into square slices."
67+
68+
69+
class NYStyleClamPizza(Pizza):
70+
def __init__(self):
71+
self.name = "NY Style Clam Pizza"
72+
self.dough = "NY Dough"
73+
self.sauce = "NY Sauce"
74+
self.toppings.append("NY toopping A")
75+
self.toppings.append("NY toopping B")
76+
77+
78+
class ChicagoStyleClamPizza(Pizza):
79+
def __init__(self):
80+
self.name = "Chicago Style Clam Pizza"
81+
self.dough = "Chicago Dough"
82+
self.sauce = "Chicago Sauce"
83+
self.toppings.append("Chicago toopping A")
84+
85+
def cut(self):
86+
print "Cutting into square slices."
87+
88+
89+
class NYPizzaStore(PizzaStore):
90+
def create_pizza(self, pizza_type):
91+
if pizza_type == "cheese":
92+
return NYStyleCheesePizza()
93+
elif pizza_type == "clam":
94+
return NYStyleClamPizza()
95+
else:
96+
return None
97+
98+
99+
class ChicagoPizzaStore(PizzaStore):
100+
def create_pizza(self, pizza_type):
101+
if pizza_type == "cheese":
102+
return ChicagoStyleCheesePizza()
103+
elif pizza_type == "clam":
104+
return ChicagoStyleClamPizza()
105+
else:
106+
return None
107+
108+
if __name__ == "__main__":
109+
ny_store = NYPizzaStore()
110+
chicago_store = ChicagoPizzaStore()
111+
112+
pizza = ny_store.order_pizza("cheese")
113+
print "Mike ordered a %s." % pizza.get_name()
114+
print
115+
116+
pizza = chicago_store.order_pizza("clam")
117+
print "John ordered a %s." % pizza.get_name()

0 commit comments

Comments
 (0)