设计模式:工厂方法模式
2012-05-26 15:40:37 来源:WEB开发网核心提示:工厂方法模式(Factory Method Pattern):定义了一个创建对象的接口,但由子类决定要实例化类的哪一个,设计模式:工厂方法模式,工厂方法让类把实例化推迟到子类,原则:要依赖抽象,也就是将生产权限下放,将生产需要的数据和方法全部交给具体工厂实现,不要依赖具体类,案例:Pizza有不同的风味
工厂方法模式(Factory Method Pattern):
定义了一个创建对象的接口,但由子类决定要实例化类的哪一个。工厂方法让类把实例化推迟到子类。
原则:
要依赖抽象,不要依赖具体类。
案例:
Pizza有不同的风味,但都是由面团(dough)、酱料(sauce)和洒在上面的佐料(topping)加工而成。如果用户订购,需要准备材料、烘烤、切片和包装。
一般的方法是创建不同的Pizza子类实现上述所有数据和方法,然后根据用户订购的类型来初始化不同的类。但这样会在主代码中有大量的条件判断语句,而且类型的每次变更都需要修改代码。
一个Pizza店可能有多个加盟店,分布在不同城市,且风味不同(比如Chicago和NewYork的cheese风味就不同),但是又要由总部提供统一的订购接口给客户,那么我们可以将子类初始化的判断放到各地工厂去实现,也就是将生产权限下放,将生产需要的数据和方法全部交给具体工厂实现。总部只需要根据客户的需要委托相应工厂生产即可。
python
Preparing NY Style Cheese Pizza dough: NY Dough sauce: NY Sauce add toppings: NY toopping A NY toopping B Bake for 25 minutes at 350. Cutting into diagonal slices. Put into official box. Mike ordered a NY Style Cheese Pizza. Preparing Chicago Style Clam Pizza dough: Chicago Dough sauce: Chicago Sauce add toppings: NY toopping A NY toopping B Chicago toopping A Bake for 25 minutes at 350. Cutting into square slices. Put into official box. John ordered a Chicago Style Clam Pizza.
代码:
#!/usr/bin/python class Pizza: name = "" dough = "" sauce = "" toppings = [] def prepare(self): print "Preparing %s" % self.name print " dough: %s" % self.dough print " sauce: %s" % self.sauce print " add toppings:" for n in self.toppings: print " %s" % n def bake(self): print "Bake for 25 minutes at 350." def cut(self): print "Cutting into diagonal slices." def box(self): print "Put into official box." def get_name(self): return self.name class PizzaStore: def order_pizza(self, pizza_type): self.pizza = self.create_pizza(pizza_type) self.pizza.prepare() self.pizza.bake() self.pizza.cut() self.pizza.box() return self.pizza def create_pizza(self, pizza_type): pass class NYStyleCheesePizza(Pizza): def __init__(self): self.name = "NY Style Cheese Pizza" self.dough = "NY Dough" self.sauce = "NY Sauce" self.toppings.append("NY toopping A") self.toppings.append("NY toopping B") class ChicagoStyleCheesePizza(Pizza): def __init__(self): self.name = "Chicago Style Cheese Pizza" self.dough = "Chicago Dough" self.sauce = "Chicago Sauce" sefl.toppings.append("Chicago toopping A") def cut(self): print "Cutting into square slices." class NYStyleClamPizza(Pizza): def __init__(self): self.name = "NY Style Clam Pizza" self.dough = "NY Dough" self.sauce = "NY Sauce" self.toppings.append("NY toopping A") self.toppings.append("NY toopping B") class ChicagoStyleClamPizza(Pizza): def __init__(self): self.name = "Chicago Style Clam Pizza" self.dough = "Chicago Dough" self.sauce = "Chicago Sauce" self.toppings.append("Chicago toopping A") def cut(self): print "Cutting into square slices." class NYPizzaStore(PizzaStore): def create_pizza(self, pizza_type): if pizza_type == "cheese": return NYStyleCheesePizza() elif pizza_type == "clam": return NYStyleClamPizza() else: return None class ChicagoPizzaStore(PizzaStore): def create_pizza(self, pizza_type): if pizza_type == "cheese": return ChicagoStyleCheesePizza() elif pizza_type == "clam": return ChicagoStyleClamPizza() else: return None if __name__ == "__main__": ny_store = NYPizzaStore() chicago_store = ChicagoPizzaStore() pizza = ny_store.order_pizza("cheese") print "Mike ordered a %s." % pizza.get_name() print pizza = chicago_store.order_pizza("clam") print "John ordered a %s." % pizza.get_name() print
输出:plain
更多精彩
赞助商链接