逆战樱之谷地雷怎么躲:Python code, 用 “builder” pattern refactoring 谢谢!

来源:百度文库 编辑:高校问答 时间:2024/04/29 08:12:20
python code, 帮我用builder pattern 原理改造一下谢谢啦!

里面的comments我也没删,希望大家帮忙哈!

class PizzaKitMatic:
def __init__(self, type):
self.type = type
self.ingedients = []

def makePizza(self):
"""This design is inflexible.
The case statement in this method
will forever have to be edited and extended
as new types of pizzas are added to the range.
"""
self.ingedients.append("Soft Crust Base")
# add main topping
if self.type == 'Veggie':
self.ingedients.append("Mushrooms")
elif self.type == 'Chicken':
self.ingedients.append("Chicken")

# add cheese
if self.type == 'Veggie':
self.ingedients.append("Moz")
elif self.type == 'Chicken':
self.ingedients.append("Blue Cheese")

# add additional topping
if self.type == 'Veggie':
self.ingedients.append("Sun Dried Tomatoes & Olives")
elif self.type == 'Chicken':
self.ingedients.append("Capsicums")

return self.ingedients

if __name__ == "__main__":
chickenPizzaMachine = PizzaKitMatic("Chicken")
print chickenPizzaMachine.makePizza()
#['Chicken', 'Blue Cheese', 'Capsicums']

veggiePizzaMachine = PizzaKitMatic("Veggie")
print veggiePizzaMachine.makePizza()
#['Mushrooms', 'Moz', 'Sun Dried Tomatoes & Olives']

"""additional functionality required is
Hawaiian Pizza
which has a Flakey Pastry Base,
Ham as a main ingredient,
Cheddar cheese
and Pineapple and Baccon as the additional topping
"""
没有改格式,不好意思。麻烦啦!!! 最佳答案追加可以追加的最高分~!!!

共 1 条
class PizzaKitMatic:
def __init__(self, type):
self.type = type
self.ingedients = []

def makePizza(self):
"""This design is inflexible.
The case statement in this method
will forever have to be edited and extended
as new types of pizzas are added to the range.
"""
self.ingedients.append("Soft Crust Base")
# add main topping
if self.type == 'Veggie':
self.ingedients.append("Mushrooms")
elif self.type == 'Chicken':
self.ingedients.append("Chicken")

# add cheese
if self.type == 'Veggie':
self.ingedients.append("Moz")
elif self.type == 'Chicken':
self.ingedients.append("Blue Cheese")

# add additional topping
if self.type == 'Veggie':
self.ingedients.append("Sun Dried Tomatoes & Olives")
elif self.type == 'Chicken':
self.ingedients.append("Capsicums")

return self.ingedients

if __name__ == "__main__":
chickenPizzaMachine = PizzaKitMatic("Chicken")
print chickenPizzaMachine.makePizza()
#['Chicken', 'Blue Cheese', 'Capsicums']

veggiePizzaMachine = PizzaKitMatic("Veggie")
print veggiePizzaMachine.makePizza()
#['Mushrooms', 'Moz', 'Sun Dried Tomatoes & Olives']

"""additional functionality required is
Hawaiian Pizza
which has a Flakey Pastry Base,
Ham as a main ingredient,
Cheddar cheese
and Pineapple and Baccon as the additional topping

class PizzaKitMatic:
def __init__(self, type):
self.type = type
self.ingedients = []

def makePizza(self):
"""This design is inflexible.
The case statement in this method
will forever have to be edited and extended
as new types of pizzas are added to the range.
"""
self.ingedients.append("Soft Crust Base")
# add main topping
if self.type == 'Veggie':
self.ingedients.append("Mushrooms")
elif self.type == 'Chicken':
self.ingedients.append("Chicken")

# add cheese
if self.type == 'Veggie':
self.ingedients.append("Moz")
elif self.type == 'Chicken':
self.ingedients.append("Blue Cheese")

# add additional topping
if self.type == 'Veggie':
self.ingedients.append("Sun Dried Tomatoes & Olives")
elif self.type == 'Chicken':
self.ingedients.append("Capsicums")

return self.ingedients

if __name__ == "__main__":
chickenPizzaMachine = PizzaKitMatic("Chicken")
print chickenPizzaMachine.makePizza()
#['Chicken', 'Blue Cheese', 'Capsicums']

veggiePizzaMachine = PizzaKitMatic("Veggie")
print veggiePizzaMachine.makePizza()
#['Mushrooms', 'Moz', 'Sun Dried Tomatoes & Olives']

"""additional functionality required is
Hawaiian Pizza
which has a Flakey Pastry Base,
Ham as a main ingredient,
Cheddar cheese
and Pineapple and Baccon as the additional topping

class PizzaKitMatic:
def __init__(self, type):
self.type = type
self.ingedients = []

def makePizza(self):
"""This design is inflexible.
The case statement in this method
will forever have to be edited and extended
as new types of pizzas are added to the range.
"""
self.ingedients.append("Soft Crust Base")
# add main topping
if self.type == 'Veggie':
self.ingedients.append("Mushrooms")
elif self.type == 'Chicken':
self.ingedients.append("Chicken")

# add cheese
if self.type == 'Veggie':
self.ingedients.append("Moz")
elif self.type == 'Chicken':
self.ingedients.append("Blue Cheese")

# add additional topping
if self.type == 'Veggie':
self.ingedients.append("Sun Dried Tomatoes & Olives")
elif self.type == 'Chicken':
self.ingedients.append("Capsicums")

return self.ingedients

if __name__ == "__main__":
chickenPizzaMachine = PizzaKitMatic("Chicken")
print chickenPizzaMachine.makePizza()
#['Chicken', 'Blue Cheese', 'Capsicums']

veggiePizzaMachine = PizzaKitMatic("Veggie")
print veggiePizzaMachine.makePizza()
#['Mushrooms', 'Moz', 'Sun Dried Tomatoes & Olives']

"""additional functionality required is
Hawaiian Pizza
which has a Flakey Pastry Base,
Ham as a main ingredient,
Cheddar cheese
and Pineapple and Baccon as the additional topping